473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1432
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.