473,547 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Raising and event

Hi,

I have an app with the main thread and one additional thread.

In the main thread I defined an event handler MyHandler, the function for
raisin this event OnMyEvent and the function for processing this event MyFunc.

I want to raise this event from the second thread. Is it thread safe to call
the method OnMyEvent from this second thread?

In Win32 with C++ I waas just sending WM messages between threads. Does work
OnMyEvent overloaded method the same way, so I could call it from different
threads?

public event MyEventHandler MyEvent;
MyEvent += new MyEventHandler (MyFunc);

protected virtual void OnMyEvent( EventsArgs e)
{
MyEventHandler handler = MyEvent;
if (handler != null)
{
handler(this,e) ;
}
}

void MyFunc( object sender, EventArgs e)
{
....
}

Thanks,

Lubomir
Mar 28 '07 #1
3 1824
On Tue, 27 Mar 2007 19:07:24 -0700, Lubomir
<Lu*****@discus sions.microsoft .comwrote:
[...]
I want to raise this event from the second thread. Is it thread safe to
call the method OnMyEvent from this second thread?
That depends on what you're actually doing in the event handler.

There's nothing fundamentally wrong about raising an event from a
secondary thread. However, a) AFAIK events are just like any other
variable with respect to thread-safeness, which means that if there's a
chance one thread may be modifying the event while another thread attempts
to access it, you need to provide synchronization for that, and b) you do
need to be aware of the restriction against calling Control methods from a
thread other than the one in which the Control was created (use Invoke or
BeginInvoke to get around that).

The main thing to remember is that the event handler will be executed on
the thread where the event was raised, even if it belongs to a Form or
Control created in another thread. All the general rules about
cross-thread execution apply, but as far as I know there's nothing
particularly special about the situation.
In Win32 with C++ I waas just sending WM messages between threads. Does
work OnMyEvent overloaded method the same way, so I could call it from
different threads?
I'm not sure what you mean by "just sending WM messages between threads",
but note that if you were actually calling SendMessage() from one thread
using a window handle from a window created on a different thread, that
was actually against the rules and not reliable. In fact, that
restriction is the reason behind the .NET restriction against calling
Control methods from a thread other than the one on which the Control was
created.

Pete
Mar 28 '07 #2
Lubomir <Lu*****@discus sions.microsoft .comwrote:
I have an app with the main thread and one additional thread.

In the main thread I defined an event handler MyHandler, the function for
raisin this event OnMyEvent and the function for processing this event MyFunc.

I want to raise this event from the second thread. Is it thread safe to call
the method OnMyEvent from this second thread?
Not the way you're doing it at the moment. See
http://pobox.com/~skeet/csharp/threads/lockchoice.shtml for a thread-
safe event pattern.
In Win32 with C++ I waas just sending WM messages between threads. Does work
OnMyEvent overloaded method the same way, so I could call it from different
threads?
No, there's no messages being passed here - events are just a way of
combining/removing delegates, effectively.

See http://pobox.com/~skeet/csharp/events.html for more information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 28 '07 #3
Thanks for explanation.

Lubomir
"Lubomir" wrote:
Hi,

I have an app with the main thread and one additional thread.

In the main thread I defined an event handler MyHandler, the function for
raisin this event OnMyEvent and the function for processing this event MyFunc.

I want to raise this event from the second thread. Is it thread safe to call
the method OnMyEvent from this second thread?

In Win32 with C++ I waas just sending WM messages between threads. Does work
OnMyEvent overloaded method the same way, so I could call it from different
threads?

public event MyEventHandler MyEvent;
MyEvent += new MyEventHandler (MyFunc);

protected virtual void OnMyEvent( EventsArgs e)
{
MyEventHandler handler = MyEvent;
if (handler != null)
{
handler(this,e) ;
}
}

void MyFunc( object sender, EventArgs e)
{
...
}

Thanks,

Lubomir
Mar 28 '07 #4

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

Similar topics

5
1564
by: Santhoshi | last post by:
I have an event declared in class1 Now i want to raise the event in the my class2 or anywhere other than class1 Is it possible? If so how Thank you
1
1853
by: Rajesh.V | last post by:
I have made a custom control in asp.net using webcontrols, and am raising my event. I find that if no control is hooked into the event it goes for a toss. I tried to find length of the delegate array returned by ButtonClicked.GetInvocationList method event that is raising an exception. Presently i am enclosing in try catch block and...
6
2842
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things,...
4
17255
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not maintain the reference to any module.
0
1436
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException (0x80041003): Exception from HRESULT: 0x80041003. at Microsoft.EnterpriseInstrumentation.EventSinks.WmiEventSink ..Write(Object eventToRaise) at
4
1584
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written the way my component raises events to follow the Framework Design Guides verbatim; ie (object sender, EventArgs (or some subclass there of) e). ...
2
1687
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on the parent form. This is fine. The problem occurs when I want to raise an event for a user clicking on one of the clsRecords which are on the...
4
1531
by: sloan | last post by:
I"m trying to figure out what concept I'm missing here, or if its not a good idea .. or what. Here is my example.. code is below. I have an employee class. It has an event that can be raised. In the constructor, I check the to see if the employees birthday is today. If it is today, then I raise an event.
2
1381
by: =?Utf-8?B?Um9i?= | last post by:
I'm new to event processing in vs 2005 - what I'm trying to achieve is broadly a network simulation of two workstations sending packets to each other . I have defined a class 'Workstation' and I was hoping to define events where in instance A (or workstation) I could raise an event 'Send' (which takes in the destination instance B (or...
7
2861
by: Christian Cambier | last post by:
Hi, I have a textbox in a web user control. I then add the usercontrol to a web form. I also add a label in the web form. Now, when I press a key in the textbox, i want to display some text in the label. I tried by raising events as used in Winforms but it is not as easy as
0
7510
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...
0
7437
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7703
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7463
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7797
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6032
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
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...
0
3493
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...
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.