472,993 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 1423
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate toplevel window) for displaying images and another...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
4
by: michaeltorus | last post by:
Hi I've written a custom Error handler thingy majig. I create it at the ASP level, and pass it into my business components which catch all errors below that. When an error occurs, my error handler...
4
by: dave_wurtz | last post by:
All, First a little information... I have a class library that was developed for a GUI application that I am trying to get to work on my ASP.NET application. The library essentially is used to...
12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
2
by: Jordan | last post by:
I need to handle UI events in a worker thread instead of the primary UI thread. In C#, is the normal UI event handling behavior to run in a context thread on the thread pool or are events always...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.