471,330 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Is there any easy scheme for handling event from another thread thanthe UI?

Hello.

Event handlers for events from an object of another thread than the UI
thread cannot access UI components, as you know. If the object has
many events this precedure is painstaking. Plus the source code gets
longer and dirtier.

Note :The code below is NOT the actual code, but an example. (I say
this, because sometimes people points out that the sample code won't
complie, even if it's just an example to explain the matter. ... means
there are more codes but are omitted because those are unnecessary.)

public delegate void MessageDelegate(Worker sender, Data d);
class ObjectFromAnotherThread
{
public event MessageDelegate MessageReceived;
....
MessageReceived(this, d);
....
}
class MainForm:Form
{
....
void OnMessage(Worker sender, Data d)
{
if (InvokeRequired)
{
OnMessageDelegate d = new OnMessageDelegate(OnMessage);
this.BeginInvoke(d, new object[] { sender, d });
}
else
{
TheLabel.Text=d.ToString();
}
}
....
}

For example, if there are three events I have to write codes like
above three times. Am I using a wrong scheme? Is there any nicer,
cleaner way? I hope that I could hear some advices from you. Thank you.
Jun 27 '08 #1
4 1378
On Jun 5, 5:48 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Event handlers for events from an object of another thread than the UI
thread cannot access UI components, as you know. If the object has
many events this precedure is painstaking. Plus the source code gets
longer and dirtier.
As Peter Duniho has pointed out various times, there's little cost in
calling Invoke or BeginInvoke when you don't have to. Now, combine
that fact with the way that often your logic doesn't actually *need*
the sender (or logically always casts) it - and anonymous methods or
lambda expressions. Change the event subscription code to:

ObjectFromAnotherThread.MessageReceived += (sender, d) =>
BeginInvoke(OnMessage(d));

void OnMessage(Data d)
{
TheLabel.Text=d.ToString();
}

Nice and straightforward :)

It's slightly messier in C# 2 with anonymous methods, but still not
too bad.

Jon
Jun 27 '08 #2
On Jun 6, 1:59*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 5, 5:48 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Event handlers for events from an object of another thread than the UI
thread cannot access UI components, as you know. If the object has
many events this precedure is painstaking. Plus the source code gets
longer and dirtier.

As Peter Duniho has pointed out various times, there's little cost in
calling Invoke or BeginInvoke when you don't have to. Now, combine
that fact with the way that often your logic doesn't actually *need*
the sender (or logically always casts) it - and anonymous methods or
lambda expressions. Change the event subscription code to:

ObjectFromAnotherThread.MessageReceived += (sender, d) =>
BeginInvoke(OnMessage(d));

void OnMessage(Data d)
{
* * *TheLabel.Text=d.ToString();

}

Nice and straightforward :)

It's slightly messier in C# 2 with anonymous methods, but still not
too bad.

Jon
Thank you. Could you please tell me how to write the code above in C#
2.0 syntax? Because I mostly use C# 2.0 (I'ld like to use the latest
version but there are more chances that .NET Framework 2.0 is
installed on the client system than 3.0 is.)
Jun 27 '08 #3
Something like:

ObjectFromAnotherThread.MessageReceived += delegate
(object sender, Data d) {BeginInvoke(OnMessage(d));};

Marc
Jun 27 '08 #4
On Jun 6, 2:06 pm, Sin Jeong-hun <typing...@gmail.comwrote:
It's slightly messier in C# 2 with anonymous methods, but still not
too bad.

Thank you. Could you please tell me how to write the code above in C#
2.0 syntax? Because I mostly use C# 2.0 (I'ld like to use the latest
version but there are more chances that .NET Framework 2.0 is
installed on the client system than 3.0 is.)
Marc has provided the C# 2 syntax, but it's important to understand
that you can still use C# 3 but target .NET 2.0. It's really nice to
have lambda expression goodness (and the rest) but still target the
more widely deployed framework.

See http://csharpindepth.com/Articles/Ch.../Versions.aspx for more
details.

Jon
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael Zhang | last post: by
4 posts views Thread by michaeltorus | last post: by
4 posts views Thread by dave_wurtz | last post: by
12 posts views Thread by scsharma | last post: by
12 posts views Thread by Jack Russell | last post: by
2 posts views Thread by Paul E. Orman | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.