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

c# lock

Hi,
in C#... I have a method that need to be executed by only one thread at a
time.
And I want the the following threads to return without entering the blocked
code section.
( if some other thread is already doing the work, I do not have to do the
work)

Something like this....

if ( myObject is locked)
{
return alreadyRunning;
}

lock(myObject)
{
\\ do work
}
return didWork;

Any direction is deeply appreciated
Thanks
Nalaka
Jun 12 '07 #1
2 7823
Nalaka wrote:
in C#... I have a method that need to be executed by only one thread at a
time.
And I want the the following threads to return without entering the blocked
code section.
( if some other thread is already doing the work, I do not have to do the
work)

Something like this....

if ( myObject is locked)
{
return alreadyRunning;
}

lock(myObject)
{
\\ do work
}
return didWork;

Any direction is deeply appreciated
Try look at Monitor.TryEnter !

Arne
Jun 12 '07 #2
Nalaka,

Instead of using lock, you will want to call the TryEnter method.
Because you will acquire the lock manually, you have to be sure to release
it as well. You will want something like this:

// Was the lock acquired?
bool lockAcquired = false;

// Enter try/finally.
try
{
// Try to acquire the lock.
if (!(lockAcquired = Monitor.TryEnter(myObject)))
{
// Get out.
return;
}

// Do your work here.
}
finally
{
// If the lock was acquired, then exit.
if (lockAcquired)
{
// Release the lock.
Monitor.Exit(myObject);
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nalaka" <na******@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,
in C#... I have a method that need to be executed by only one thread at a
time.
And I want the the following threads to return without entering the
blocked code section.
( if some other thread is already doing the work, I do not have to do the
work)

Something like this....

if ( myObject is locked)
{
return alreadyRunning;
}

lock(myObject)
{
\\ do work
}
return didWork;

Any direction is deeply appreciated
Thanks
Nalaka

Jun 12 '07 #3

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

Similar topics

3
by: xixi | last post by:
can someone explain to me what is internal p lock, internal s lock, internal v lock? when i have IS lock or IX lock , i always have these internal locks together for the application handle ...
0
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in...
7
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock...
3
by: Invalid | last post by:
I launch a worker thread that periodically reads a volatile bool abortRequested, which could be set to true by my main form. IOW, there is one thread that can read that bool and one different...
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
1
by: charlies224 | last post by:
Hi, I am writting a software that requires me to make sure the Num Lock is always on and Caps Lock is always off. First, I know how to detect if Num Lock or Caps Lock is on or off (if...
3
by: Raj | last post by:
I created a refresh deferred MQT, and during full refresh there were 4 or 5 lock waits, all waiting on a 'S' lock on Internal Catalog Cache ? Can some one explain how to prevent this from happening?
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
190
by: blangela | last post by:
If you had asked me 5 years ago about the future of C++, I would have told you that its future was assured for many years to come. Recently, I have been starting to wonder. I have been teaching...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
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
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
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...

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.