473,395 Members | 1,668 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,395 software developers and data experts.

How to raise an event in UI thread?

Suppose class Engine do something in another thread and raise events.

class Engine
{
Thread Worker;
public event ... EngineMessage;
public void Start()
{
Worker=new Thread(new ThreadStart(Run));
Worker.Start();
}
private Run()
{
..Do some length work....
under some condition, fires
this.EngineMessage(msg);
}
}

If I subscribe to this event in the Form and tries to set the message
to a TextBox, this causes InvalidOperationExeption since this message
is from another thread. I can use this.InvokeRequred at the form, but
can't I do this at the Engine class so that at the Form, I can handle
the message without using delegates? I mean in the Engine class , can
I somehow raise the event in the same thread as the one called Start()?

Aug 11 '07 #1
2 9126
Hi Sin,

Sin Jeong-hun wrote:
Suppose class Engine do something in another thread and raise events.

class Engine
{
Thread Worker;
public event ... EngineMessage;
public void Start()
{
Worker=new Thread(new ThreadStart(Run));
Worker.Start();
}
private Run()
{
..Do some length work....
under some condition, fires
this.EngineMessage(msg);
}
}

If I subscribe to this event in the Form and tries to set the message
to a TextBox, this causes InvalidOperationExeption since this message
is from another thread. I can use this.InvokeRequred at the form, but
can't I do this at the Engine class so that at the Form, I can handle
the message without using delegates? I mean in the Engine class , can
I somehow raise the event in the same thread as the one called Start()?
I am not sure whether you can raise the event on the UI thread without a reference to the UI, but you can do the following in your form that processes the EngineMessage event without explicitly defining so many delegates:

class MyForm : Form
{
// Process EngineMessage here
protected void Engine_EngineMessage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((MethodInvoker)delegate { SetText(msg); });
}

private void SetText(string txt)
{
this.myTextBox.Text = txt;
}
}

Hope it helps,

Marc.
Aug 11 '07 #2
Marc Bartsch wrote:
I am not sure whether you can raise the event on the UI thread without a
reference to the UI, but you can do the following in your form that
processes the EngineMessage event without explicitly defining so many
delegates:

class MyForm : Form
{
// Process EngineMessage here
protected void Engine_EngineMessage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((MethodInvoker)delegate { SetText(msg); });
}
Or, if it's really a simple method like that (it's not always, but in
this example obviously it is :) ):

// Process EngineMessage here
protected void Engine_EngineMessage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((MethodInvoker)delegate { this.myTextBox.Text = msg });
}

Actually, for that matter, unless I needed to use the same code
somewhere else by calling it as a method, I would probably just put the
whole invoke-necessary code in the anonymous delegate. It's just that
as the anonymous delegate gets longer, the odds that it's doing
something that needs to be shared by non-invoking code goes up, thus
justifying putting it into a shareable method. :)

Pete
Aug 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dan Cimpoiesu | last post by:
I have a remoting object, derived from MarshalByRefComponent, that I instantiate on the client side, with Activator.GetObject. Can I receive events fired on the server, on the client? How?
5
by: Waleed AlRashoud | last post by:
Hi, I hope u can help me I asked this question before but I didn't explain it very well. Let say I hvae a thread 'A', creates object 'O', and start thread 'B' to do some work on 'O', OK? I...
2
by: Bob Day | last post by:
Using VS 2003, Vb, MSDE... Option 1 ------------------------------------------- Thread A and B are instantiations of 2 different classes. If thread A raises an event caught by thread B, the...
4
by: Charles Law | last post by:
Suppose a worker thread needs to signal to the main thread that an event has occurred. Ordinarily, any event raised by the worker thread will be on its own thread. How can the worker thread...
12
by: Vittorio Pavesi | last post by:
Hello, is it possible to manually raise the event Elapsed of the timer object ? Vittorio
12
by: Benny Raymond | last post by:
I understand that you would normally want to raise an event on the same thread so that your code blocks until the event has been dealt with... however i've run into a situation where I have a...
3
by: =?Utf-8?B?Ulc=?= | last post by:
I constructed a new Class with some private members. I would like an event to be raised on the moment the value of one of those private members is changed. How do I define an event for that...
5
by: Mike | last post by:
Hi group; Let say I have an object called Account, that raises an event called AccountLow with its owns EventArgs, and when this event gets raised, I will like to raise another custom...
11
by: nadeem_far | last post by:
Hello, I am working on a c# project using framework 1.1. Since its a console application,no windows forms are being used. I am trying to implement event driven classes that just want to raise...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.