Threading ???
-
Hi all, I am writing an application in which i have a loop to write some outputs over the text box .After i run the application, it works fine as long as i stick to the form and do not click anywhere. As soon as i open any other window to work on and then go back to that output form..It freezes the application and doesnt respond. The code for instance is as shown below...
//On BUTTON CLICK the word "Message" repeats in every line until loop ends.... while (true) { if(count > 1000) { break; } Thread.Sleep(1000) TextBoxA.Text += "Message" + Environment.NewLine; TextBoxA.Refresh(); count++; }//end while
I guess its something to do with threading...But i am not too sure about it. Pls point me the right direction.... Thanks SS -
Hi all, I am writing an application in which i have a loop to write some outputs over the text box .After i run the application, it works fine as long as i stick to the form and do not click anywhere. As soon as i open any other window to work on and then go back to that output form..It freezes the application and doesnt respond. The code for instance is as shown below...
//On BUTTON CLICK the word "Message" repeats in every line until loop ends.... while (true) { if(count > 1000) { break; } Thread.Sleep(1000) TextBoxA.Text += "Message" + Environment.NewLine; TextBoxA.Refresh(); count++; }//end while
I guess its something to do with threading...But i am not too sure about it. Pls point me the right direction.... Thanks SSFor windows forms in .net 2.0 you can use the BackgroundWorker[^] control. You place it on the form and then put your code into the Do Work event. If you need to do this in .Net 1.1 or outside of win forms then you can use the Thread Class[^] directly. To do this you make a new method which takes no parameters and has no return value: private void TestThread() { //DO STUFF } Then you create a new thread and start it: System.Threading.Thread testThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.TestThread)); testThread.Start(); You can also pass things to the thread but I'll leave that for you to read up on ;) One last thing to note, if you are working with windows forms then you'll need to use the Invoke method on any controls you access across threads. http://www.codeproject.com/csharp/threadsafeforms.asp[^]
-
For windows forms in .net 2.0 you can use the BackgroundWorker[^] control. You place it on the form and then put your code into the Do Work event. If you need to do this in .Net 1.1 or outside of win forms then you can use the Thread Class[^] directly. To do this you make a new method which takes no parameters and has no return value: private void TestThread() { //DO STUFF } Then you create a new thread and start it: System.Threading.Thread testThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.TestThread)); testThread.Start(); You can also pass things to the thread but I'll leave that for you to read up on ;) One last thing to note, if you are working with windows forms then you'll need to use the Invoke method on any controls you access across threads. http://www.codeproject.com/csharp/threadsafeforms.asp[^]
hey thanks for your reply... I have added this backgroundWorker and copied the code to backgroundWorker1_DoWork event. Now how can i call this backgroundWorker1_DoWork under btn pressed event to execute the loop...?? Thanks
-
hey thanks for your reply... I have added this backgroundWorker and copied the code to backgroundWorker1_DoWork event. Now how can i call this backgroundWorker1_DoWork under btn pressed event to execute the loop...?? Thanks
Generally, it works like this:
System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();
bw.DoWork +=new System.ComponentModel.DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();But why not look at the MSDN example?
Standards are great! Everybody should have one!
-
Generally, it works like this:
System.ComponentModel.BackgroundWorker bw = new System.ComponentModel.BackgroundWorker();
bw.DoWork +=new System.ComponentModel.DoWorkEventHandler(bw_DoWork);
bw.RunWorkerAsync();But why not look at the MSDN example?
Standards are great! Everybody should have one!
Dont know ..its bit confusing at the moment. Well i am getting exception Cross-threaded operation not valid.. I am trying to achieve simple operation that is execution of the code i mentioned in my first post. I just have one textbox and a button over the form. Now i added backgroundWorker component over the form. Double clicked over DoWork event. Now i have a startbtn click and DoWork event. So do i need ProgressChanged or RunWorkerCompleted event or no..?? If not then do i have to just paste the first post code under DoWork event and add some extra code under start button event to execute the loop or is there any different approach. If its true then what would be that extra code that i have to add under start button event so that it performs what i am trying to... I dont think it works with backgroundWorker1.RunWorkerAsync(); .........? Well in MSDN they have given some differnt example and i am doing something really simple... Thanks
-
Dont know ..its bit confusing at the moment. Well i am getting exception Cross-threaded operation not valid.. I am trying to achieve simple operation that is execution of the code i mentioned in my first post. I just have one textbox and a button over the form. Now i added backgroundWorker component over the form. Double clicked over DoWork event. Now i have a startbtn click and DoWork event. So do i need ProgressChanged or RunWorkerCompleted event or no..?? If not then do i have to just paste the first post code under DoWork event and add some extra code under start button event to execute the loop or is there any different approach. If its true then what would be that extra code that i have to add under start button event so that it performs what i am trying to... I dont think it works with backgroundWorker1.RunWorkerAsync(); .........? Well in MSDN they have given some differnt example and i am doing something really simple... Thanks
So do i need ProgressChanged or RunWorkerCompleted event or no..?? Yes. You're getting the Crossthreading exceptions because you're trying to do an operation on a thread other than the thread the UI control was created on. This isn't allowed in forms. You can use ProgressChanged and RunWorkerCompleted to avoid this. Use the
ReportProgress
together with theProgressChanged
event to achieve this:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();bw.WorkerReportsProgress = true; bw.DoWork += new DoWorkEventHandler(bw\_DoWork); bw.ProgressChanged += new ProgressChangedEventHandler(bw\_ProgressChanged); bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw\_RunWorkerCompleted); bw.RunWorkerAsync(); } void bw\_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.Text = "Looped " + e.ProgressPercentage + " times"; } void bw\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.Close(); } void bw\_DoWork(object sender, DoWorkEventArgs e) { int i = 0; while (i++ < 100) { bw.ReportProgress(i); Thread.Sleep(100); } } }
}
Standards are great! Everybody should have one!
-
Dont know ..its bit confusing at the moment. Well i am getting exception Cross-threaded operation not valid.. I am trying to achieve simple operation that is execution of the code i mentioned in my first post. I just have one textbox and a button over the form. Now i added backgroundWorker component over the form. Double clicked over DoWork event. Now i have a startbtn click and DoWork event. So do i need ProgressChanged or RunWorkerCompleted event or no..?? If not then do i have to just paste the first post code under DoWork event and add some extra code under start button event to execute the loop or is there any different approach. If its true then what would be that extra code that i have to add under start button event so that it performs what i am trying to... I dont think it works with backgroundWorker1.RunWorkerAsync(); .........? Well in MSDN they have given some differnt example and i am doing something really simple... Thanks
cross-threaded operation etc. is caused by controls being updated by any thread other than the one that created them. The background worker control should stop this. Otherwise you need to call begininvoke on the control that you are updating. There are loads of examples of this on CP and MSDN. Russ