0
我正在使用Asp.net MVC 3.任何我無法在MVC中創建線程過程的單個實例的原因?我試圖一次只允許一個工作進程的單個實例。但是它一次允許多個實例。ASP中的信號量和鎖定MVC3
我跟着這個例子: http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx
這是在我的控制器代碼:
private static Semaphore _pool;
public ActionResult StartBots(int id)
{
_pool = new Semaphore(0, 1);
Thread t = new Thread(SingletonWorker);
t.Start();
_pool.Release(1);
return RedirectToAction("index", new { id = id });
}
我用鎖也試過了這個例子: http://msdn.microsoft.com/en-us/library/c5kehkcz(v=VS.80).aspx
private Object thisLock = new Object();
public ActionResult StartBots(int id)
{
Thread t = new Thread(SingletonWorker);
lock (thisLock)
{
t.Start();
}
return RedirectToAction("index", new { id = id });
}
- ------------------------------工人------------------- -----------------
private static void SingletonWorker()
{
_pool.WaitOne(); <== this only applies to the Semaphore example.
// Do something here.
Thread.Sleep(rand.Next(4) * 200 + 1000);
// Do something else here.
}
你會怎麼寫呢? – Mario
我已經用示例更新了我的答案 – sternr
好吧,我把工作進程中的鎖移動了,它工作正常。謝謝! – Mario