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