Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,
I am struggling With issue since three days.
I am using progress bar in Splash window. That Progress bar Run Correctly if i set await Task.Delay(1000) after one second my dll are going to load because Program wait for completing Delay time. After 1 sec my Progress bar stopped and my dll getting load.That make program lazy and its take much time to load application
i want both work simultaneously . My Progress bar Should run continuous and
My dll should run in back ground. After that it will open Login Window so please help me
Below is My Code
I created Progress bar in Xaml.

<ProgressBar Minimum="0" Maximum="100" Height="10" Background="Transparent" Width="100" Foreground="Black" Name="pbStatus" IsIndeterminate="True" Grid.Column="1" Margin="4,29,-4,20" />


My Splash Window Code In App.Xaml.cs
C#
var splashScreen = new SplashWindow();
            //this.MainWindow = splashScreen;
            //this.MainWindow.Close();
            splashScreen.Show();
           // System.Threading.Thread th = new System.Threading.Thread();
            //in order to ensure the UI stays responsive, we need to
            //do the work on a different thread
            Task.Factory.StartNew(() =>
            {
                //simulate some work being done
                System.Threading.Thread.Sleep(5000);

                //since we're not on the UI thread
                //once we're done we need to use the Dispatcher
                //to create and show the main window
                this.Dispatcher.Invoke(() =>
                {
                    //initialize the main window, set it as the application main window
                    //and close the splash screen
                    var mainWindow = new Views.Common.LogIn();
                    this.MainWindow = mainWindow;
                        mainWindow.Show();
                    splashScreen.Close();
                });
            });



How can i over come with this issue

What I have tried:

C#
this.Dispatcher.Invoke(() =>
                {
                    //initialize the main window, set it as the application main window
                    //and close the splash screen
                    var mainWindow = new Views.Common.LogIn();
                    this.MainWindow = mainWindow;
                        mainWindow.Show();
                    splashScreen.Close();
                });
            });


C#
<pre>Task.Run(() =>
        {
             var mainWindow = new Views.Common.LogIn();
                    this.MainWindow = mainWindow;
                        mainWindow.Show();
                    splashScreen.Close();
        }
Posted
Updated 27-Sep-17 23:56pm

1 solution

This MSDN Magazine article will show you how to do what you want: Asynchronous Programming - Async from the Start[^]

UPDATE: In your comments below you indicate that the UI still appears to lock up. This is usually because you are trying to run too much on the UI thread on start up.

Multithreading is not simple if you are not familiar with how it, the SynchronizationContext, and the UI message pump works. This is explained in a lot of detail in the article. Work with the downloadable sample code and modify it until you understand it, then modify the code in your app.
 
Share this answer
 
v2
Comments
Member 12069642 28-Sep-17 5:58am    
I tried this and in Given Url they are discussing about Window Form and I am working on WPF. If my using another solution or run two thread simultaneously then Exception were coming The calling thread cannot access this object because a different thread owns it
Graeme_Grant 28-Sep-17 6:06am    
Keep reading, both Winform & WPF are covered. 3rd paragraph:

"I’m going to show you over the course of several refactorings of the default UI startup code, both for Windows Forms and Windows Presentation Foundation (WPF), transforming the UI boilerplate into an object-oriented design and adding support for async/await."

There are also C# & VB source code samples download links at the top of the page, so you can try out the code and add it to your project.
Member 12069642 28-Sep-17 6:45am    
Can you please tell me in my code where i can change and accomplish my task
Graeme_Grant 28-Sep-17 7:02am    
The splash screen used in the article is just another form. Put an indeterminate ProgressBar on the form and it will do what you want.
Member 12069642 28-Sep-17 7:28am    
I did Indeterminate as you can see in my code but it has been stopped after some times

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900