I have a method that should be executed in an exclusive way. This is basically a multi-threaded application in which the method is called periodically by a timer, but which can also be started manually using a user action.
Take an example:
The timer expires, so the method is called. The task may take several seconds.
Immediately after this, the user clicks on, which should cause the same task: BAM. It does nothing since this method is already running.
I used the following solution:
public void DoRecurentJob() { if(!Monitor.TryEnter(this.lockObject)) { return; } try { // Do work } finally { Monitor.Exit(this.lockObject); } }
Where lockObjectdeclared as follows:
lockObject
private readonly object lockObject = new object();
Change . There will be only one instance of an object that supports this method, so I updated the lock object so that it is non-static.
? , , - ?
, , . , , , , Monitor.Exit().
static static .
Mutex Semaphore, , ( ), , , .
Mutex
Semaphore
, , , , .
: lockObject , "this.lockObject" . ( , , ), , , . , , ?
? , . , , , - , , - , , .
, , . , , , . ( "if" "try", , - , CLR.)
, , . , , , .
, , .
public class MyClass { public void AccessResource() { OneAtATime(this); } private static void OneAtATime(MyClass instance) { if( !Monitor.TryEnter(lockObject) ) // ...
, Microsoft lock Monitor . .
public class MyClass { // Used as a lock context private readonly object myLock = new object(); public void DoSomeWork() { lock (myLock) { // Critical code section } } }
MyClass, :
private static readonly object myLock = new object();
, . , . , , , . .
, , , . , , .
- MethodImplOptions.Synchronized , :
[MethodImpl(MethodImplOptions.Synchronized)] public void OneAtATime() { }
, . , . Java synchronized - , .
synchronized
, , , , . :
https://codereview.stackexchange.com/questions/16150/singleton-task-running-using-tasks-await-peer-review-challenge
private queued = false; private running = false; private object thislock = new object(); void Enqueue() { queued = true; while (Dequeue()) { try { // do work } finally { running = false; } } } bool Dequeue() { lock (thislock) { if (running || !queued) { return false; } else { queued = false; running = true; return true; } } }
Source: https://habr.com/ru/post/1697659/More articles:SBCL on Vista crashes. Do you know how to make it work? - windows-vistaWhat is the best way to get the name of a folder that does not exist? - c #What is the best way to list a member and all of his descendants in MDX? - mdxhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1697657/html-jquery-method-bizzare-bug-resolves-to-empty-space-locally-but-not-on-production&usg=ALkJrhg43inBBKc8_R0nSRhBSOjee8I5uwDoes different users have good practice for different types of queries? - mysqlenable / disable asp.net web service extension via script - asp.netСтранная ошибка компоновщика MFC/VС++ (std:: list уже определен) - visual-studio-2008Integrate jQuery into an existing ASP.NET web application? - jqueryLOAD SQL Table from a flat file - sqlEclipse c ++ pretty prints? - c ++All Articles