Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
I am using following code to update the progress bar <pre lang="c#"> private void btnSearch_Click(object sender, EventArgs e)
        {


            worker.DoWork += worker_DoWork;
            worker.WorkerReportsProgress = true;
            worker.ProgressChanged += worker_ProgressChanged;
            worker.RunWorkerAsync();

        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            updateUI();
        }


        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

           myfunction();
            Thread.Sleep(100);
            worker.ReportProgress(paths.currentProgress);


        }


        public void updateUI(){
            pBar.Minimum = 0;
            pBar.Maximum = 100;
            pBar.Step = paths.currentProgress; // a value I get from myfunction()
            pBar.PerformStep();
        }</pre>  it do not update the UI(progressbar).

then I tried it this way

<pre lang="c#">private void btnSearch_Click(object sender, EventArgs e)
        {


            worker.DoWork += worker_DoWork;
            worker.RunWorkerAsync();

        }


        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

           myfunction();
            Thread.Sleep(100);
           updateUI();



        }


        public void updateUI(){
            pBar.Minimum = 0;
            pBar.Maximum = 100;
            pBar.Step = paths.currentProgress; // a value I get from myfunction()
            pBar.PerformStep();
        }</pre>

this time it gives me the error
<pre>An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Cross-thread operation not valid: Control 'pBar' accessed from a thread other than the thread it was created on.</pre>

please help me how do I feed the UI from the function I have called in back ground worker.
Posted
Comments
[no name] 10-Apr-14 7:48am    
Try taking the "updateUI();" out of your DoWork method in your second example. Try not calling worker.ReportProgress(paths.currentProgress); in your first. You can't or should not access UI controls from non UI threads. That is what your ProgressChanged event is for.
z3m 10-Apr-14 9:12am    
I have tried both ways: as you suggested, it didn't worked. here are the changes I have made to my code.


private void btnSearch_Click(object sender, EventArgs e)
{


worker.DoWork += worker_DoWork;
worker.WorkerReportsProgress = true;
// worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
updateUI();
}
[no name] 10-Apr-14 9:18am    
"it didn't worked" tells me absolutely nothing. I can't see your screen or read your mind. All you have done here is take your progress display completely out of your background worker.
z3m 10-Apr-14 9:31am    
I am sorry for short rely, I wanted to say it didn't updated the progress bar not even gave me the exception. Every thing is going good but it don't update the progress bar.
[no name] 10-Apr-14 9:41am    
Which is exactly what you told it to do. Why can't you simply update your progress from the ProgressChanged event like it is designed for? I am not sure why this is such a problem.

1 solution

The error is because you are accessing /modifying the UI control which is running on another thread .

//add this line in your constructor
Control.CheckForIllegalCrossThreadCalls = false;

but remember this is not thread safe . for more details follow this Link

thanks
 
Share this answer
 
v2
Comments
[no name] 10-Apr-14 7:55am    
OMG no!
johannesnestler 10-Apr-14 9:01am    
Don't Do this!
z3m 10-Apr-14 9:17am    
As for now this thing worked for me.
Control.CheckForIllegalCrossThreadCalls = false;

but after this I have to call multiple threads then this " Control.CheckForIllegalCrossThreadCalls = false;" code line may cause problems for me.
Please suggest any other method.
[no name] 10-Apr-14 9:43am    
No no no. Never, ever do this. Ignoring exceptions do not make them go away. You are getting an exception for a reason. Fix your code instead of ignoring the problem and hope that it goes away.

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