background Worker Class
-
Hi all, Well i am trying to get message over textbox which is in a loop and contains many messages. But then the problem was that my form was not responding if i switch to any other application n return back to it. So it use to hang. So some1 told me to use BackGroundWorker Class. And i did that and the below code is working absolutely fine...just hv a look below..
namespace BackgroundThread { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private BackgroundWorker worker = null; private void Form1_Load(object sender, EventArgs e) { worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerAsync(); } void worker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 100000; i++) { if (InvokeRequired) Invoke(new Change(OnChange), i); } private void OnChange(int i) { textBox1.Text += i.ToString() + ","; Application.DoEvents(); Thread.Sleep(100); } private delegate void Change(int i); } }
Now comes the problem part. Suppose now i have a button over form and after clicking that it should write messages over the text box after the aboce code finish executing. But i dont know how to include that in a thread...just have a look below...private void startBtn_Click(Object sender, EventArgs e) { int Count = 0; while(true) { if(count > 1000) { break; } Thread.Sleep(1000); textBox1.Text += "Message" + Environment.NewLine; textBox1.Refresh(); count++; } The above code again makes the form not responding after i press the start button. Well i know its a threading problem and i dont have much experience in threading. Well actually the above code is just an example. My real application is different and would give message over text box from different parts of application. So how to solve this case..if there is any easy way out like to refresh the form every time...etc... Any help would be greatly appreiated.. Thanks
-
Hi all, Well i am trying to get message over textbox which is in a loop and contains many messages. But then the problem was that my form was not responding if i switch to any other application n return back to it. So it use to hang. So some1 told me to use BackGroundWorker Class. And i did that and the below code is working absolutely fine...just hv a look below..
namespace BackgroundThread { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private BackgroundWorker worker = null; private void Form1_Load(object sender, EventArgs e) { worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerAsync(); } void worker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 100000; i++) { if (InvokeRequired) Invoke(new Change(OnChange), i); } private void OnChange(int i) { textBox1.Text += i.ToString() + ","; Application.DoEvents(); Thread.Sleep(100); } private delegate void Change(int i); } }
Now comes the problem part. Suppose now i have a button over form and after clicking that it should write messages over the text box after the aboce code finish executing. But i dont know how to include that in a thread...just have a look below...private void startBtn_Click(Object sender, EventArgs e) { int Count = 0; while(true) { if(count > 1000) { break; } Thread.Sleep(1000); textBox1.Text += "Message" + Environment.NewLine; textBox1.Refresh(); count++; } The above code again makes the form not responding after i press the start button. Well i know its a threading problem and i dont have much experience in threading. Well actually the above code is just an example. My real application is different and would give message over text box from different parts of application. So how to solve this case..if there is any easy way out like to refresh the form every time...etc... Any help would be greatly appreiated.. Thanks
Software_Specialist wrote:
The above code again makes the form not responding after i press the start button.
Thats probably because the while loop at the beginning of the start button click event will be infinite (assuming Count and count are the same variable, you just typed differently) But a couple of points in your code.. If you are using BackGroundWorker you should not do Application.DoEvents. That can cause problems. Also you are doing a Thread.Sleep in OnChange, which is being called from the GUI thread. So I guess this will hold up your GUI thread? So basically, no DoEvents and no sleeping on the GUI thread should make it better.
-
Hi all, Well i am trying to get message over textbox which is in a loop and contains many messages. But then the problem was that my form was not responding if i switch to any other application n return back to it. So it use to hang. So some1 told me to use BackGroundWorker Class. And i did that and the below code is working absolutely fine...just hv a look below..
namespace BackgroundThread { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private BackgroundWorker worker = null; private void Form1_Load(object sender, EventArgs e) { worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerAsync(); } void worker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < 100000; i++) { if (InvokeRequired) Invoke(new Change(OnChange), i); } private void OnChange(int i) { textBox1.Text += i.ToString() + ","; Application.DoEvents(); Thread.Sleep(100); } private delegate void Change(int i); } }
Now comes the problem part. Suppose now i have a button over form and after clicking that it should write messages over the text box after the aboce code finish executing. But i dont know how to include that in a thread...just have a look below...private void startBtn_Click(Object sender, EventArgs e) { int Count = 0; while(true) { if(count > 1000) { break; } Thread.Sleep(1000); textBox1.Text += "Message" + Environment.NewLine; textBox1.Refresh(); count++; } The above code again makes the form not responding after i press the start button. Well i know its a threading problem and i dont have much experience in threading. Well actually the above code is just an example. My real application is different and would give message over text box from different parts of application. So how to solve this case..if there is any easy way out like to refresh the form every time...etc... Any help would be greatly appreiated.. Thanks
Actually your app is probably not responding because you're calling Thread.Sleep(1000) for 1000 times from the main thread your application is running on. After a thousand seconds it'll be responsive again. You should run your code from a separate thread. Again, you should use a background worker for your loop and update your controls using the report progress mechanism of the background worker or something similar. I showed you the code for this some days ago I think here
Standards are great! Everybody should have one!