Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
My progressbar get stuck when i call
Dispatcher.Invoke
.

Please consider following code snippet.

C#
private void DoWork()
        {
            Progressbar.show();//Displays progress bar
            System.Windows.Application.Current.Dispatcher.Invoke((Action)delegate
            {
                Function1();//Takes 1 min to execute
            });
            Progressbar.close();//Closes progress bar
        }




I have DoWork() method which displays progressbar and after that there is Dispatcher.Invoke method for modifying UI control.
As finction Function1() inside Dispatcher.Invoke takes long time to finish. So while executing Dispatcher.Invoke, it stops progress of progressbar.
Please tell me how I shall show progress bar so that it will not stop while executing Dispatcher.Invoke
Posted
Updated 27-May-15 17:58pm
v2
Comments
Sergey Alexandrovich Kryukov 28-May-15 0:46am    
Totally wrong. This is not what dispatcher is used for. Dispatcher is used to notify UI thread. Long method should be just called in the non-UI thread, and each update to the UI should be under Invoke.
—SA
RhishikeshLathe 28-May-15 1:02am    
Actually Invoke is filling the datagrid from database so it is taking long time.

Please see my comment to the question. I have no gut to explain how wrong is what you do and how irrelevant is the "solution" Solution 1.

Just see my past answer and the answers referenced there: .NET event on main thread[^].

You are lost so much that I cannot be sure that you can understand my explanations, but please just try, ask some follow-up questions in case of doubt.

—SA
 
Share this answer
 
Instead of Use Background Worker when u have to show progressbar and do some work in background.


See link
https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
 
Share this answer
 
Hi Try this

C#
Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
            {   
                string text = textbox1.Text;
            });
 
Share this answer
 

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