D
Dilip
(e-mail address removed) (John Torjo) wrote in message > And by the way, could you please point me to how .net + threading +
This is probably OT.
I still don't understand completely what you are doing by seeing your
sample program but as far as .NET is concerned you cannot access any
UI element from a method executing in a non-UI thread. The correct
way to do it is to check if a thread-switch is required and if yes,
the call needs to be marshalled to the UI thread. Like so:
private delegate void MethodExecDelegate(string somevalue);
private void method_executing_on_non_ui_thread(string somevaluetoset)
{
if (this.InvokeRequired)
this.BeginInvoke(
new MethodExecDelegate(method_executing_on_non_ui_thread),
null);
else
some_ui_control.Text = somevaluetoset;
}
The above method is part of a class derived from System.Windows.Form.
This has been true since the days Windows threading support. As Glen
Low points out in another post, worker threads need to use the
PostMessage API to update controls created by the UI thread. Also
using SendMessage API will cause a blocking call -- therefore if the
UI is busy for some reason and can't handle the call right away, the
worker thread will be needlessly blocking.
thanks
--Dilip
GUI interact?
Since it seems to me that they interact kind of badly...
On my small C# project I made, it seems that if I do something like
static void other_thread1() {
g_form.txt.Text = "other thread -1";
}
and, make sure I catch the text_changed event:
private void changed_txt(object sender, System.EventArgs e)
{
int i = 0;
}
I catch this in the context of the other thread, as opposed to the
main thread (which is what I would expect). Thus, I need to make my
whole class thread-safe, just because I want to manipulate one of its
controls from another thread.
This strikes me as very bad, compared to Win32 programming, where all
messages are sent on the context of the thread that created the
control, not on the context of the caller.
This is probably OT.
I still don't understand completely what you are doing by seeing your
sample program but as far as .NET is concerned you cannot access any
UI element from a method executing in a non-UI thread. The correct
way to do it is to check if a thread-switch is required and if yes,
the call needs to be marshalled to the UI thread. Like so:
private delegate void MethodExecDelegate(string somevalue);
private void method_executing_on_non_ui_thread(string somevaluetoset)
{
if (this.InvokeRequired)
this.BeginInvoke(
new MethodExecDelegate(method_executing_on_non_ui_thread),
null);
else
some_ui_control.Text = somevaluetoset;
}
The above method is part of a class derived from System.Windows.Form.
This has been true since the days Windows threading support. As Glen
Low points out in another post, worker threads need to use the
PostMessage API to update controls created by the UI thread. Also
using SendMessage API will cause a blocking call -- therefore if the
UI is busy for some reason and can't handle the call right away, the
worker thread will be needlessly blocking.
thanks
--Dilip