This is a migrated thread and some comments may be shown as answers.

SplashScreen not showing until process ends

6 Answers 669 Views
SplashScreen
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 28 Jan 2020, 12:15 PM

     Good day,

 

Following the instruction in your "Getting Started" section, after just updating the UI for WPF modules, I created a simple MenuItem under my Menu to test it out. 

The code is rather simple:

private void Loadingtest(object sender, RoutedEventArgs e)
        {
            var dataContext = (SplashScreenDataContext)RadSplashScreenManager.SplashScreenDataContext;
            dataContext.ImagePath = "";
            dataContext.Content = "Loading";
            dataContext.Footer = "Please wait, this might take several minutes";
            dataContext.MouseCursor = Cursors.Wait;
            dataContext.IsProgressBarVisible = true;
            dataContext.IsIndeterminate = true;
            if (!RadSplashScreenManager.IsSplashScreenActive) {

                RadSplashScreenManager.Show();
            Thread.Sleep(7000);
            RadSplashScreenManager.Close();
        }

 

Problem is whenever I click that menuitem it performs the 7 second wait and then flashes the loading screen for a moment before closing it again.

Tried it as well in WindowLoaded event as well as a Rendered event, both do the same things.

Tried to do Dispatcher.Invoke, Dispatcher.BeginInvoke as well as a new Tread with a run. All goes the same way. Both in Debug and Release modes. Also, trying to do something like changing the title in between throws an exception about the running thread not being able to access it, as if the whole execution were moved to a new thread after the SplashScreenManager.Show();

 

Example of the one that causes an error:

 

private void Loadingtest(object sender, RoutedEventArgs e)
        {
            var dataContext = (SplashScreenDataContext)RadSplashScreenManager.SplashScreenDataContext;
            dataContext.ImagePath = "";
            dataContext.Content = "Loading";
            dataContext.Footer = "Please wait, this might take several minutes";
            dataContext.MouseCursor = Cursors.Wait;
            dataContext.IsProgressBarVisible = true;
            dataContext.IsIndeterminate = true;
            if (!RadSplashScreenManager.IsSplashScreenActive) {
                RadSplashScreenManager.Show();

            this.Title = "Title Test";
            Thread.Sleep(7000);
            RadSplashScreenManager.Close();
        }

 

Thanks a bunch!

6 Answers, 1 is accepted

Sort by
0
n/a
Top achievements
Rank 1
answered on 28 Jan 2020, 05:14 PM

For some reason it won't let me edit the main post; there's also a problem with the timing of it all and the access to IsSplashScreenActive. The following code throws an error that says "InvalidOperationException: You cannot show more than one instance of the splash screen"

 

if (!RadSplashScreenManager.IsSplashScreenActive)
                RadSplashScreenManager.Show();
            RadSplashScreenManager.Close();
            var dataContext = (SplashScreenDataContext)RadSplashScreenManager.SplashScreenDataContext;
            dataContext.ImagePath = "";
            dataContext.Content = "Loading";
            dataContext.Footer = "Please wait, this might take several minutes";
            dataContext.MouseCursor = Cursors.Wait;
            dataContext.IsProgressBarVisible = true;
            dataContext.IsIndeterminate = true;
            if (!RadSplashScreenManager.IsSplashScreenActive)
                RadSplashScreenManager.Show();
            MessageBox.Show("TEST");
            RadSplashScreenManager.Close();

 

If I remove the one above, it would show "TEST" then show and close the splash screen.

0
Vladimir Stoyanov
Telerik team
answered on 31 Jan 2020, 10:38 AM

Hi Andrei,

Thank you for the provided code snippets. 

I tested the described scenario on my end, however I was not able to replicate the described behavior. That is why I am attaching the sample project, which I used for testing purposes. Can you check it out and let me know, if I am missing something? 

I am looking forward to your reply.

Regards,
Vladimir Stoyanov
Progress Telerik

Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Avi
Top achievements
Rank 1
answered on 01 Jun 2020, 09:46 PM
Setting ImagePath to "" causes an error to be thrown when the window is displayed
0
Avi
Top achievements
Rank 1
answered on 01 Jun 2020, 10:12 PM

this is not a good example as you are using sleep.

 

In a multithreaded app, the splashscreen does not open until the UI frees (hit the next await).  It similarly does not close when the the Close method is called but at some point later.  In my app, the next line after the close, is another condition that may want to open the splashWindow.  I added a line to check if the SplashWindow is active which prevents the already active error but  the result is the Splash Window is not displayed.

 

This control seems is a good idea but seems half baked

0
Avi
Top achievements
Rank 1
answered on 02 Jun 2020, 02:37 PM

I created a class that deal with threading issue which care causing the timing issue.

 public class MySplashScreen {
        private static readonly SplashScreenDataContext radSplashScreen = (SplashScreenDataContext)RadSplashScreenManager.SplashScreenDataContext;

        static OaSplashScreen() {
            radSplashScreen.Footer = "Footer text";
        }

        public static async Task ShowAsync(string message) {
            radSplashScreen.Content = message;
            RadSplashScreenManager.Show();
            while (!RadSplashScreenManager.IsSplashScreenActive) await Task.Delay(TimeSpan.FromMilliseconds(200));
        }

        public static async Task CloseAsync() {
            if (RadSplashScreenManager.IsSplashScreenActive) {
                RadSplashScreenManager.Close();
                while (RadSplashScreenManager.IsSplashScreenActive) await Task.Delay(TimeSpan.FromMilliseconds(200));
            }
        }
    }

0
Vladimir Stoyanov
Telerik team
answered on 04 Jun 2020, 11:16 AM

Hello Avi,

With regards to the ImagePath, it can be set to null, instead of an empty string to avoid issues with the default ImageSourceConverter. Thank you for pointing this out.

To elaborate a bit on the RadSplashScreen, it is displayed in a separate background thread. That is why it takes some time for the splashscreen to be shown/closed. Generally, it is designed to be shown during the application startup to indicate that the application is loading. 

That said, the approach that you have adopted seems reasonable for assuring that the control is displayed/closed. 

Regards,
Vladimir Stoyanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
SplashScreen
Asked by
n/a
Top achievements
Rank 1
Answers by
n/a
Top achievements
Rank 1
Vladimir Stoyanov
Telerik team
Avi
Top achievements
Rank 1
Share this question
or