|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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...
|
| Sign In·View Thread·PermaLink | 3.00/5 (2 votes) |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | 2.67/5 (3 votes) |
|
|
|
 |
|
|
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; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Nicely done. Very helpful. Short and to the point particularly with follow up comments and considering it was written a while back. Thanks.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
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"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Try the BackgroundWorker class: http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Cheers,
Manuel
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Isn't BackgroundWorker basically the same thing as the code in this article, with additional support for wiring up events rather than manually creating and setting up delegates?
I think that the answer to the problem is that the operations need to be made more granular. That is, if you want to fill a combo box, you should add them one (or a few) at a time rather than filling the entire box.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
I see... maybe you could try creating a queue and then update the controls (dequeue and update) on the Idle event for the application. You can update a combo an item at a time (don't forget to call CurrencyManager.Refresh() when you want to see the changes).
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Don't you think that the piece of code:
// wait when thread will stop or finish while (m_WorkerThread.IsAlive) { // We cannot use here infinite wait because our thread // makes syncronous calls to main form, this will cause deadlock. // Instead of this we wait for event some appropriate time // (and by the way give time to worker thread) and // process events. These events may contain Invoke calls. if ( WaitHandle.WaitAll( (new ManualResetEvent[] {m_EventThreadStopped}), 100, true) ) { break; }
Application.DoEvents(); }
is a kind of busy waiting? Is there any other way to do it, in a more elegant way?
Regards, Adam
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
More elegant way is using BeginInvoke instead of Invoke in a worker thread. In this case we can use infinite wait without deadlock. If worker thread uses Invoke for some reason (for example, it needs return value of Invoke operation), busy waiting is only way.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
you can use the asyncresult with the begininvoke and endinvoke to get what you need back from the other thread
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Being somewhat new to .NET and C#, I appreciate very much this clearly documented, well written example. I have tried to slog thru many other examples on the web. This one is hands-down the best I've found. It just works great! Thank You! Thank You!
W.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
This example covers almost the whole stuff of thread programming on Windows Forms. And it doesnt directly result into an unhandled Exception.
You deed a great job.
Thanks Raymond
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
I've been scouring the web for an example of how to stop a worker thread from the main form and how to tell the main form when an exception is thrown in the worker thread. Your code here is the closest I've seen to something that would work well, but there's one major difference. You're starting the worker thread with Thread.Start, while I'm using Delegate.BeginInvoke.
I need to pass parameters to one of the asynchronus methods, and I need to be able to specify an AsynchCallback method to run when the thread completes. From what I've seen, I can do neither of these things using Thread.Start.
I've looked at the object model, and I can't figure out how to get a handle to the actual thread that I started using BeginInvoke. Is there something that I'm missing? I haven't even been able to stop the thread from the main thread using EndInvoke, and I don't know any other way to even try to stop it.
Thanks.
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
  
   Hello , i can't create a new control in the function that related with thread
e.g
while(true) { ................... ..................... ................. .........
label a=new label(); ..... . ... .... . . . . . }
..
this give an error message at run time and said that you cant create a new control
    
Thanks
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Add function to the form class which creates new control. Call this function from the worker thread using Invoke or BeginInvoke, by the same way as AddString function is called in the article sample.
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
this is the erorr message :
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: Controls created on one thread cannot be parented to a control on a different thread.
Thanx
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi to all and sorry for my english, I need to add a control (my custom row) with a separated thread that read the data from database and then create the new row that have to be add in my custom grid(like outlook when receive a new mail). it's all ok until I invoke the add method in my grid but the system say me the following error: "The controls created on a thread cannot have like element father a control on a thread various" (this is a traslate... my .net is italian version).
This is a sample code that simulate my problem.... thanks!!!
Public Class Form1 Inherits System.Windows.Forms.Form
#Region " Codice generato da Progettazione Windows Form "
Public Sub New() MyBase.New()
'Chiamata richiesta da Progettazione Windows Form. InitializeComponent()
'Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent()
End Sub
'Form esegue l'override del metodo Dispose per pulire l'elenco dei componenti. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Richiesto da Progettazione Windows Form Private components As System.ComponentModel.IContainer
'NOTA: la procedura che segue è richiesta da Progettazione Windows Form. 'Può essere modificata in Progettazione Windows Form. 'Non modificarla nell'editor del codice. Private Sub InitializeComponent() ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(396, 238) Me.Name = "Form1" Me.Text = "Form1"
End Sub
#End Region
Private WithEvents Thr As MyThread Private xThread As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Thr = New MyThread(Me)
Dim myThreadStart As New System.Threading.ThreadStart(AddressOf Thr.StartThread) xThread = New System.Threading.Thread(myThreadStart)
xThread.Start()
End Sub
Private Sub Thr_ButtonAdded() Handles Thr.ButtonAdded MsgBox("new button created!!!") End Sub End Class
Public Class MyThread
Public Event ButtonAdded()
Private m_Form As System.Windows.Forms.Form
Sub New(ByVal F As System.Windows.Forms.Form) m_Form = F End Sub
Public Sub StartThread() Beep() Dim myInvoker As New MethodInvoker(AddressOf NewButton) myInvoker.Invoke() End Sub
Private Sub NewButton() Dim newbtn As New System.Windows.Forms.Button m_Form.Controls.Add(newbtn)
RaiseEvent ButtonAdded()
End Sub
End Class
|
| Sign In·View Thread·PermaLink | 1.33/5 (2 votes) |
|
|
|
 |