473,395 Members | 2,151 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.

fire an event in main thread context

Hi,

I create a class whitch creates a thread. When thread finish it has to fire
event. But I want to fire this event in the context of the thread that
created the class (usually main thread). For this I create a control in
context of the creator thread. But are there better techniques,like maybe
create a hidden window or something else ? this is code snippets i use now:

public class CDelay
{
Control Ctrl;

public delegate void RequestDone(object sender, RequestDoneArgs e);
public event RequestDone OnRequestDone;
public class RequestDoneArgs: EventArgs
{
Int32 FThreadId;

public RequestDoneArgs(Int32 ThreadId)
{
FThreadId = ThreadId;
}

public Int32 ThreadId
{
get { return FThreadId; }
}
}

public CDelay(Control Parent)
{
Ctrl = new Control();
Ctrl.Parent = Parent;

So in constructor Ctrl is created in thread context of the creator (Parent).
When I have to call event I use the normal Invokething:

RequestDoneArgs e = new
RequestDoneArgs(Thread.CurrentThread.ManagedThread Id);
if(OnRequestDone != null)
Ctrl.BeginInvoke(OnRequestDone, new object[] { this, e });

It works as a glance, but if there are better things ZI like to know of
course.

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #1
2 6488
Unless you are willing to reinvent the wheel by coding what's happening when
calling Invoke, there aren't.
Not sure why you need to execute the eventhandler on the thread that created
the class, in general it's considered bad practice to affinitize handlers
with specific threads?

Willy.

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.com> wrote in
message news:71**********************************@microsof t.com...
Hi,

I create a class whitch creates a thread. When thread finish it has to
fire
event. But I want to fire this event in the context of the thread that
created the class (usually main thread). For this I create a control in
context of the creator thread. But are there better techniques,like maybe
create a hidden window or something else ? this is code snippets i use
now:

public class CDelay
{
Control Ctrl;

public delegate void RequestDone(object sender, RequestDoneArgs e);
public event RequestDone OnRequestDone;
public class RequestDoneArgs: EventArgs
{
Int32 FThreadId;

public RequestDoneArgs(Int32 ThreadId)
{
FThreadId = ThreadId;
}

public Int32 ThreadId
{
get { return FThreadId; }
}
}

public CDelay(Control Parent)
{
Ctrl = new Control();
Ctrl.Parent = Parent;

So in constructor Ctrl is created in thread context of the creator
(Parent).
When I have to call event I use the normal Invokething:

RequestDoneArgs e = new
RequestDoneArgs(Thread.CurrentThread.ManagedThread Id);
if(OnRequestDone != null)
Ctrl.BeginInvoke(OnRequestDone, new object[] { this, e });

It works as a glance, but if there are better things ZI like to know of
course.

--
rgds, Wilfried
http://www.mestdagh.biz

Nov 16 '05 #2
Hi,
Not sure why you need to execute the eventhandler on the thread that created
the class, in general it's considered bad practice to affinitize handlers
with specific threads?


I'm just starting to play around with .NET once a while, so I dont know
mutch about thread safe programming yet, but I should think if I use a
class/component, and I create it in main thread then I dont expect that his
events will fire in a different thread context. Or am I wrong here ?

On the other hand I see mutch of things in NET is threading and blocking, so
maybe it is just a 'hangover' from me from the win32 programming :)

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #3

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

Similar topics

1
by: Sweta | last post by:
How do I post a message to the main thread from another thread in the catch block? I am trying to do this: try { //generate an exception from another thread } catch() { //fire the event to...
1
by: John | last post by:
Hi, I have a worker thread that receives messages from a socket and puts them in a queue (it sits inside a "while true") loop. Each time it adds a message to the queue, I need the main thread...
5
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only...
7
by: Gordon Smith | last post by:
I have four (4) ASP.NET Web applications/Web sites on a IIS/6 - Windows Server 2003 production server. 3 of them work fine. I just installed the 4th one and it's Application_Start event is not...
5
by: Nathan Sokalski | last post by:
I am trying to learn how to use the System.Timers.Timer control to perform an action every certain amount of time. However, the Elapsed event doesn't want to fire, but I can't figure out why. I...
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: Ronald S. Cook | last post by:
In my Win app, I'm needing to load a media file into my Windows Media Player control, and THEN read an attribute from the file. The thing is, the file has to be playing for a split second before...
4
by: Sin Jeong-hun | last post by:
This is what I've always been wondered. Suppose I've created a class named Agent, and the Agent does some lengthy job. Of course I don't want to block the main window, so the Agent does the job in...
1
by: Lila Godel | last post by:
My VB.NET 2008 application is setup with a Sub Main and no forms. At run time a NotifyIcon is created with one context menu choice (Close which terminates app). I have no trouble running the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.