473,659 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EngineMess age(msg);
}
}

If I subscribe to this event in the Form and tries to set the message
to a TextBox, this causes InvalidOperatio nExeption since this message
is from another thread. I can use this.InvokeRequ red 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 9146
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.EngineMess age(msg);
}
}

If I subscribe to this event in the Form and tries to set the message
to a TextBox, this causes InvalidOperatio nExeption since this message
is from another thread. I can use this.InvokeRequ red 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_EngineMe ssage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((Me thodInvoker)del egate { 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_EngineMe ssage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((Me thodInvoker)del egate { 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_EngineMe ssage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((Me thodInvoker)del egate { 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
10513
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
3217
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 want thread 'B' to fire event of 'O' BUT MUST THAT EVENT DONE BY THREAD 'A' not 'B'!!
2
2060
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 delagate in thread B that executes is 1) running under thread A based on the debug threading window and 2) does not seem to be aware of any instantiations of thread B in its declarations section. See **** for what fails. Class B (which is thread...
4
9809
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 raise an event on the main thread instead? TIA Charles
12
2937
by: Vittorio Pavesi | last post by:
Hello, is it possible to manually raise the event Elapsed of the timer object ? Vittorio
12
2915
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 thread running that isn't my form thread and I want it to raise a custom event and continue going without waiting for that event to be handled... Is this at all possible?
3
2133
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 private member and how do I raise the event? -- RW
5
2006
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 event called AccountGotLowAmount and passes AccountLow’s EventArgs along with the new event.
11
4020
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 the event and continue working( like fire and forget - do not wait for the event handler in client to finish.) I have gone through alot of the articles on internet and MSDN and most talk about delegates (or BackGroundWorker in 2.0 which I cannot
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.