bucz

In my application, I receive some values from two threads; I have main class of main form, that is one thread TH1 that receives some events like button click and second thread TH2 that works in loop generates some values. When some value or action is received from TH1 or TH2, they both need to be displayed in a control in main form.

If I try to change control from TH2, get an exception about cross-threading operation. I have solved that in the way that TH2 doesnt change controls but only properties in main form and in main form I have timer that every 1ms updates labels from properties. Works, no cross-therading troubles.

Although I don't like this timer that works all the time, I wanted to make maybe some asynchronous event running - that TH2 would just fire some event in main therad and it would update interface. But I dont know how to do this. How

When I tried to make some BackgroundWorker that would br run every time a property changes, it didn't change interface, just has thrown

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll


How to force main window to update its controls in its own thread
(I think that I just don't know how to implement those events)



Re: Visual C++ General Asunchronous UI updating - other way?

Viorel.

Some of operations on controls can be performed only by threads that created the controls. In case of multithreading you should apply approaches described in "How to: Make Thread-Safe Calls to Windows Forms Controls" article [http://msdn2.microsoft.com/en-us/library/ms171728.aspx].

Translated to C++ and Visual Studio 2005, you have to execute something like this. Instead of

label->Text = "some text";

you have to add a delegate and a function:

class Form1

{

. . .

delegate void SetTextDelegate(System:: String ^ s);

void SetText(System:: String ^ s)

{

label->Text = s;

}

. . .

};

Then, in your background thread, set the label¡¯s text like this:

array< System:: Object^ > params = { "some text" };

Invoke(gcnew SetTextDelegate(this, &Form1:: SetText), params);

I hope this works.





Re: Visual C++ General Asunchronous UI updating - other way?

bucz

Thanks.

Let say, my "child" window is formChild. now, when I do Invoke somewhere in main form's code, is there a difference between:

this->Invoke(...)

and

formChild->Invoke(...)