473,397 Members | 2,116 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,397 software developers and data experts.

thread safe events

i have a class that contains a method (not static) that is called on an
event by child threads of the class. i assume it is not threadsafe.
is there an attribute that i can set on the method that would cause the
runtime to handle the method in a thread safe way? or do i need to put in my
own locks?
thanks
m
Nov 15 '05 #1
2 1836
M.

You can apply the MethodImpl attribute with a value of
MethodImplOptions.Synchronized on the method. However, this is bad for a
number of reasons. What I would recommend doing is using a lock section in
the method itself, locking on an object that is stored on the class level.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"M. Raab" <ra**@biconix.com> wrote in message
news:OC*************@TK2MSFTNGP11.phx.gbl...
i have a class that contains a method (not static) that is called on an
event by child threads of the class. i assume it is not threadsafe.
is there an attribute that i can set on the method that would cause the
runtime to handle the method in a thread safe way? or do i need to put in my own locks?
thanks
m

Nov 15 '05 #2

Hi,

Just as Nicholas said, MethodImplOptions.Synchronized is a choice for you.
Also you can apply the sychronization yourself. This mainly due to your
lock granularity.
The MethodImplOptions.Synchronized may only apply on a whole method, while
this may not be a need. For example, your method is big, and there are only
serveral sentences should be synchronized. At this time, you can apply
yourself mutex only to these sentences.
For self synchronization, there are a lot of ways in .Net Framework, such
as mutex class, event etc. You can find more information in
System.Threading namespace.

Btw: You can't use the [MethodImpl(MethodImplOptions.Synchronized)]
attribute on methods of a struct type (if you call the method you get a
runtime TypeLoadException). Something like this:

using System.Runtime.CompilerServices;
class Indirect
{
[MethodImpl(MethodImplOptions.Synchronized)] // compiles and runs ok
public void Method()
{
//...
}
}

struct Direct
{
[MethodImpl(MethodImplOptions.Synchronized)] // compiles ok, runtime
TypeLoadException
public void Method()
{
//...
}
}

Hope this helps, if you still have anything unclear, please feel free to
let me know
Have a nice day.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "M. Raab" <ra**@biconix.com>
| Subject: thread safe events
| Date: Wed, 22 Oct 2003 14:14:50 -0400
| Lines: 9
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <OC*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 198-143-226-150.dsl.btitelecom.net 198.143.226.150
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:193271
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| i have a class that contains a method (not static) that is called on an
| event by child threads of the class. i assume it is not threadsafe.
| is there an attribute that i can set on the method that would cause the
| runtime to handle the method in a thread safe way? or do i need to put in
my
| own locks?
| thanks
| m
|
|
|

Nov 15 '05 #3

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

Similar topics

0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
3
by: James Dunkerley | last post by:
Hi, I am trying to create a class which downloads a web page in the background and then raises an event in the original thread in which it was created. This class is going to be the basis of a...
3
by: BoloBaby | last post by:
All, I believe I am having a threading problem. Class "BELights" is part of a larger DLL that is used by my main application. A user control (of type BESeat) within the main application raises...
1
by: Tom | last post by:
First of all, is it 'legal' (i.e. thread-safe) to pass events back from a worker thread to the main thread? I.E. Can one put PUBLIC EVENT XYZ in their worker thread and then raise that event for...
5
by: Emrys | last post by:
Hi. I have two threads and an event. The first thread registers a callback with the event. That callback plays with variables that only the first thread should touch. The second thread...
5
by: Dan Tallent | last post by:
I am trying to learn how to create an application that uses worker threads to prevent the UI from freezing up. My question is: Is the following code considered "thread safe"? If not, what am I...
13
by: Jordan | last post by:
All, I have a UI form calling a class object that contains a timer that routinely draws intensive information to the screen (~30 fps). The drawing is invoked on the main UI thread. I need the...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
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: 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?
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:
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
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...
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.