Sunday 15 January 2012

multithreading - C# Invoke button control on separate thread -


I have seen many different questions about how the control on the C # form can be edited with a different thread, but Someone does not understand me much I think you can not change any UI from the main thread with any other thread. To make this work, you have to use Aqua and edit the controls safely from there?

I have a button that starts to write in a file and when you press the button the button is disabled so that you can not start a lot of threads that do exactly the same. . When writing is done, I want the button to be available again but I can not work on this other thread.

I have this form as a Generate_Click event from the form.

  Private Zero Generate_Click (Object Sender, EventArgs e) {Generate.Enabled = false; Int x = 512; Int y = 512; MBrot mbrot = new MBrot (x, y); PB_Update lb = New PB_Update (0, y, Generator_PB, Generate, Ambrot, this); Lb.Start (); }  

and it is in the PB_Update.cs Threadwork () function, when the loop is done then the file is written to be written and such a thread ends and the message box With the given "finished" now, the last button needs to be enabled.

  Public Zero Threadwork () {While breaking (true) {if (currValue_> = maxValue_); ThreadTick (); } Mb_.StopBrot (); T_.Interrupt (); MessageBox.Show ("Finished!"); Generate_.Enabled = true; }  

For WinForms you can execute directly on the thread through the control The method was created, you can use the control. See also, Control.BeginInvoke is preferred for UI operation.

  Public Zero Threadwork () {While breaking (if true) {if (currValue_> = maxValue_); ThreadTick (); } Mb_.StopBrot (); T_.Interrupt (); MessageBox.Show ("Finished!"); Generate_.BeginInvoke ((Action) representative () {Generate_.Enabled = true;}); }  

No comments:

Post a Comment