 |
|
 |
Excellent article. Easy to understand and a good example.
|
|
|
|
 |
|
|
 |
|
 |
In first place I want to thank you for this awesome article!
But I've got a recommendation, just make the Add String method thread safe, so you don't need to do the invoke and the delegate call within the "LongProcess". This is nice because think about a class member function which is using AddString() lets name it "foo()". Now if you want to call foo() from within an thread function you need either to adjust the foo() function to do the invoke on the call of AddString() or you just adjust you AddString() to be thread safe.
private delegate void addStringDelegate(string s);
private void AddString(string s)
{
if (listBox1.InvokeRequired)
{
listBox1.Invoke(new addLineDelegate(AddString), new object[] { s });
}
else
{
listBox1.Items.Add(s);
}
}
|
|
|
|
 |
|
 |
It would help to see the workerthread implemented with code. Please post an example and/or help me out with some issues I'm having w/ implementation @ http://stackoverflow.com/questions/5394585/c-winforms-application-workerthread-null-error-how-to-assign-values-and-use
Thanks!
Brian @ http://flseodesign.com
|
|
|
|
 |
|
 |
Click download link in the beginning of the article.
|
|
|
|
 |
|
 |
Good Template but Need Example - I'm not sure what I would put in some sections. It would help to see this implemented with code. Please post an example or a link to an example and/or help me out with some issues I'm having w/ implementation @ http://stackoverflow.com/questions/5394585/c-winforms-application-workerthread-null-error-how-to-assign-values-and-use
|
|
|
|
 |
|
|
 |
|
 |
Brilliant article, very easy to follow, concise, explains all lines with great comments.
Probably the best threading example I've read to date.
|
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
This is a awesome article, and every beginner who wants to know about threads and delegates in c# should read and run this.
Thanks a lot
Karmendra and Chandresh
|
|
|
|
 |
|
 |
Hi Alex, thank you for this article!
Im writing a compact application, which is able to change its skin, language and etc..
Until the execution of changing is ending, I would like to show a message, like please wait, and play an animation (for example a spinning clock).
I run the execution of changing form another thread, but I have to use Invoke or BeginInvoke due to the controls, which style I would like to change. At first the animation did not move, and after that I used many Application.DoEvents() function (which is not so elegant), so the animation run discursively..
I tried to run this animation from a new form, but as I experienced, it is called from the main thread too, and it did not help me..
Is it possible somehow to give enough process time to this animation? Have you got any other idea?
Best Regards.
Jeno Cs.
|
|
|
|
 |
|
 |
Being somewhat new to .NET and C#, I appreciate very much this clearly documented, well written example. Thanks a lot.
I have similar requirements and have decided to design and implement a similer functionality in a client demanding project.
I would like to clarify some doubts:
1. I have ~10 text boxes, which needs to be shown data (relevant to each text box) byte by byte as being received by a worker thread from serial port.
ForEx: I am receiving a strem of 10 characters for textBox1 I need to show the byte by byte display.
appearing on the form text box
I have also to some time do validations on the received data with already shown data by the text box.
2.I am deciding to implement delegate to append data for each byte received for each control text box.
In this way I am calling Invoke for each byte for each control?Is it sensible?
Please suggest me if there is any better approach then this?
I am not finding as how to get the text of the text box for validation in worker thread.
Could you guide me as how to achieve this?
Something like calling delegete to return string from worker thread?Is it possible?
Please suggest.
Once again I thank you very much for this article.
Best Regards.
Amar.
|
|
|
|
 |
|
 |
Number of bytes added by every Invoke call depends on communication logic. For example, if serial port receives 10 bytes at once, add them to a textbox in one Invoke operation.
Since this article was written long time ago, there are some things that can be improved. First, I recommend to use BeginInvoke instead of Invoke. Asynchronous operation should be always default choice. Second, use Thread.Join instead of waiting for "thread stopped" event - there is no need in this event.
To get control text from a worker thread, add function returning string to a form, and invoke this function from a thread with appropriate delegate.
|
|
|
|
 |
|
 |
Thanks Alex for the guidance.
summing up what you said:
1. Use Asynchronous call to update Form from worker thread as and when data received and keep it appending to form's text box.
Ex:ABCDEFG is received call deligate 7 times to append all 7 bytes one by one to text box.
2. Add a deligate which return's the string after calling form's function.
Please comment.
Thanks and warm Regards.
Amarjeet.
|
|
|
|
 |
|
 |
I have enough c# to be dangerous, and I wanted to be able to interupt or abort a long process run from a form. This code looked straight forward enough, so I tried to mush it in around the code I had. My issues are: I can't figure where m_form comes from, and I get an error 'No overload for method 'LongProcess' I have a 75 page treatise on Threads, but I'm hoping to avoid reading it to figure his out...
|
|
|
|
 |
|
 |
Doh. I finally actually downloaded the demo and added the LongProcess function and the three members (m_XXX) to the LongProcess class.
Especially since this is marked for beginners, it would be helpful I think if these were not left out of the sample code (by all means hiding all the windows form junk is a good idea).
Otherwise this was such a great article.
I am just not a down loader by nature (I just want to go straight to modifying my own code). I guess if I had done that first, I'd have had no problem, but those few elements in the code sample would make it completely usable without the download.
Ashley
|
|
|
|
 |
|
 |
for anyone else having this issue, it's
public class LongProcess
{
#region Members
// Main thread sets this event to stop worker thread:
ManualResetEvent m_EventStop;
// Worker thread sets this event when it is stopped:
ManualResetEvent m_EventStopped;
// Reference to main form used to make syncronous user interface calls:
MainForm m_form;
#endregion
public LongProcess(ManualResetEvent eventStop,
ManualResetEvent eventStopped,
MainForm form)
{
m_EventStop = eventStop;
m_EventStopped = eventStopped;
m_form = form;
}
|
|
|
|
 |
|
|
 |
|
 |
Nicely done. Very helpful.
Short and to the point particularly with follow up comments and considering it was written a while back.
Thanks.
|
|
|
|
 |
|
 |
Dear Sir,
I have a project : build web describe Quck sort, But I dont know to use Thread which will make chart changing visual, please help me
Thanks and best regards,
Openit
|
|
|
|
 |
|
 |
I first thought that your article was answer to my problem:
In your solution, form keeps responsing user interaction while background process executes, but freezes while updating its controls. What I need is, my form should not freeze while updating its controls, which takes a long time. For example, textboxes should allow editing while combo boxes are being filled. I actually am not even sure if it is possible. Have any ideas? Thanks...
asan
"Much that once was is lost for none now live who remember it"
|
|
|
|
 |
|
 |
Try the BackgroundWorker class:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Cheers,
Manuel
|
|
|
|
 |