When main thread working the Whole Form Freezes
-
How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:
-
How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:
-
How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:
If you use a worker thread, make sure you don't try to update your progress bar directly from that thread; invoke a delegate method for updating your progress bar instead. -- I've killed again, haven't I?
-
If you use a worker thread, make sure you don't try to update your progress bar directly from that thread; invoke a delegate method for updating your progress bar instead. -- I've killed again, haven't I?
-
How can i prevent my form from freezing when doing a large fucntion. For instance: I read a 20,000 line file line by line. after each line i do something with the data. This could take a long time. So i would use a progress bar to show progress. The progress bar starts but after a couple of seconds the progress bar does not move and the form freezes up until the process is finished. How can i prevent this from happening. Do i use another Thread? :rose:
I've run into this before. I ended up using creating a single delegate function called ThreadEvent which does everything and derived my own class called ThreadEventArgs from EventArgs to handle communicating what the thread is reporting. Here's my delegate that handles whenever the thread sends any signal:
private void ThreadEventDelegate (object sender, ThreadEventArgs e) if (<control>.InvokeRequired) { this.BeginInvoke(new ThreadEvent (ThreadEventDelegate), new Object[] { sender, e }); } else { <handle the event properly here> }
This is needed because the thread cannot change a control. What this does is it asks the control what unique thread number it is under. Whatever current thread is running is compared and if they are the same, then it doesn't need to invoke and it's safe to change the control. If they are different, the thread calls BeginInvoke to execute a new delegate asynchronously with the same data it was passed inside the thread it should have been.