How to restrict to one method call at a time?

c# lock vs mutex
lock c# best practices
lock vs mutex vs semaphore vs monitor c#
semaphoreslim vs lock
c# task lock

I'd like to know how to implement the following restriction: One method in my Windows Service should not be called again before the earlier call has been finished. The method in question goes thru couple of database tables and it's very important that this process won't be called again before it's finished. I have a setting that defines how often my service will activate and under normal circumstances it never activates before the earlier call has been finished (because whole process should not take more than couple of minutes and the interval is set to 10 minutes) but thats not sure enough. I guess.

How to implement this?

You can use a named Mutex or a named Semaphore to ensure that only one holder of the Mutex/Semaphore is executing at once. As a commenter pointed out, keep in mind you must be careful not to abandon a mutex or improperly acquire/release a semaphore.

set_time_limit - Manual, When called, set_time_limit() restarts the timeout counter from zero. In other words, if If set to zero, no time limit is imposed. This function has no effect when PHP is running in safe mode. There is i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i​=9 i=10. You can create a lock table in DB and lock a specific defined record in it for update first thing in the transaction. This will prevent other transactions created by your application (in the same code area) to even get to the table you updating. And second call will only proceed after the first one is done. Just a tip.

One way would be to use locking:

private readonly object myLock = new object();

private void MyMethod()
      //code goes here

This ensures that this method can never be running more that once at a time.

How do I ensure a piece of code runs only once?, You'd use a similar approach, but reset the flag after a certain amount of time. also do an atomic test_set operation prior to calling or not calling the function. Periodic notification: Allow you to select repeated notification alert and allow you to set the repetition time Another function which is quite good and handy is that the application allows you to select the phone number in your contact list to limit call duration. Go to Advance Setting -> and select Specific Number.

I second the Mutex suggestion, but you might also want to take a look at transactions. Wrap your entire code in a transaction (this requires a using System.Transactions):

using(TransactionScope scope = new TransactionScope())
        /* ... your current code here */
    catch (Exception e)
        /* Any appropriate error handling/logging here */

A transactionscope automatically locks all related tables. You can reduce the restrictions and allow other processes to read, but not write to the data that your process is touching. You do this by passing options to the TransactionsScope constructor.

Cooperative asynchronous JavaScript: Timeouts and intervals , This tutorial looks at the traditional methods JavaScript has available for block of code repeatedly with a fixed time delay between each call. If you are IO-bound, then there are any number of ways to do this; the preferred would be to use the inbuilt async methods of whatever you are talking to, so you can use IOCP rather than regular threads. So for a NetworkStream, you would use BeginRead() etc. Then you need to join everything together.

Well if all the code is localized you can set a boolean and check the boolean before executing the method, otherwise you can IPC and request the state before execution.

lock statement, where x is an expression of a reference type. Since the code uses a tryfinally block, the lock is released even if an exception is thrown within the body of a Hold a lock for as short time as possible to reduce lock contention. by two threads attempting to call the Debit or Credit methods simultaneously. After that ask if elapsed time is more than 3 seconds, and if yes (true) stop or reset the Stopwatch, and use the return keyword to stop the recursion loop, very good start in function, if your function lasts long time due mainly recursion more than loops. That it is.

Some alternatives:

  1. You can put a check in the call to check some flag or call Monitor.TryEnter and return with an error/do nothing if negative.
  2. You can queue up calls (if you need this method to execute more than once) and only invoke when Monitor has been signaled.
  3. If you don't mind blocking, and the method is on a separate thread, you can join the thread of the method you want to wait.

I'm sure there are others.

Create a call queue, With serial routing, the first call in the queue rings all call agents one by one. call wait time, and call time-out options with any of the above methods. When this limit is reached, the call is handled in the way you set on the  Maybe this is a workaround. Use the System.Runtime.CompilerServices and then you can either check the Name of the calling function and/or the file, in which the calling function is defined. If you have a class per file, the filename might be a substitude for the class name. Check it and block the call.

.one(), A function to execute at the time the event is triggered. If the selector is null or omitted, the handler is always called when it reaches the selected element. data. Method A: This uses lock on an object. Each invocation of this method accesses the threading primitives implemented by the lock. Then: Only one method A can call the statements protected by the lock at a single time, regardless of the thread count. Info: Method A is invoked 10 times.

Method and Block Synchronization in Java, If one thread is executing the synchronized method, all others thread that invoke synchronized on the same Object at same time without synchronization. Restricting access to non-action methods. To restrict access to non-action method you must use NonActionAttribute to notify MVC framework that given controller method is not action. The code is here: [NonAction] public void DoInternalStuff()

Object level lock vs Class level lock in Java, When ever a thread enters into Java synchronized method or block it in nature it means if a synchronized method calls another synchronized method which That point of time only one thread gets lock & will execute synchronized block. According to the Skype Fair Usage Policy, there are definitely call time limits applied to subscriptions and calls placed using credit. Likewise, Skype would see a Skype-to-Skype call of 24 hours as one that likely was left engaged unintentionally, and would disengage the call (hang up).

  • Do you want subsequent calls to be skipped, or queued?
  • I like your answer better
  • I'd rather use a named semaphore, no risk of getting an abandoned mutex then so no need to have to deal with the additional possibility of experiencing the exception or having to think about ownership.
  • +1 Peter Morris, I'll add a blurb about the possibility of a named Semaphore, however, they too have problems with improper releasing, etc
  • don't forget to make myLock static - the question actually implies multythreaded situation.
  • I believe it is actually a multi-process situation.
  • Odd, all of that message didn't get sent to me the first time, bug in the system?
  • Glitch in my typing. I accidentaly submitted my reply before finishing the complete post.
  • A transaction alone cannot be used to synchronize code. It can only be used to consistently access data. How tables are locked, or if at all, depends on the isolation-level of the transaction. That is totally different than using a mutex (things like SQL*Server's sp_get_applock() set aside).