2012-11-05 94 views
2

我有一个快速连续调用的函数,它有一个开放的数据库连接。 我的问题是,在关闭一个数据库连接之前,会调用该函数的另一个实例,并且可能会在数据库中收到死锁。如何等待线程在C#中完成执行?

我曾尝试:

private static WaitHandle[] waitHandles = new WaitHandle[] 
    { 
     new AutoResetEvent(false) 
    }; 

protected override void Broadcast(Data data, string updatedBy) 
    { 
     Action newAction = new Action(() => 
     { 
     DataManagerFactory.PerformWithDataManager( 
      dataManager => 
      { 
       // Update status and broadcast the changes 
       data.UpdateModifiedColumns(dataManager, updatedBy); 

       BroadcastManager.Instance().PerformBroadcast(
        data, 
        BroadcastAction.Update, 
        Feature.None); 
      }, 
      e => m_log.Error(ServerLog.ConfigIdlingRequestHandler_UpdateFailed() + e.Message)); 
      } 
     ); 

     Thread workerThread = new Thread(new ThreadStart(newAction)); 
     ThreadPool.QueueUserWorkItem(workerThread.Start, waitHandles[0]); 
     WaitHandle.WaitAll(waitHandles); 
    } 

但是我收到一个线程错误,程序冻结。这与线程启动功能没有任何参数有关。

感谢您的任何帮助

+0

在'QueueUserWorkItem'第二个目的是参数。请参阅[MSDN](http://msdn.microsoft.com/en-us/library/4yd16hza.aspx) – climbage

+0

通常,您不会将'ThreadPool'上的新线程的'Start'方法排队。这种打击的目的。只需排队'newAction'。您应该锁定您身边的任何方法('PerformBroadcast'?),这可能会导致服务器端发生死锁。 –

+0

ThreadPool.QueueUserWorkItem(newAction,null);不工作。 ThreadPool.QueueUserWorkItem(newAction,waitHandles [0]);不工作。 –

回答

0

这是如何做到的。创建的类,它的工作:

public class MyAsyncClass 
{ 

    public delegate void NotifyComplete(string message); 
    public event NotifyComplete NotifyCompleteEvent; 

    //Starts async thread... 
    public void Start() 
    { 
     System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(DoSomeJob)); 
     t.Start(); 
    } 

    void DoSomeJob() 
    { 
     //just wait 5 sec for nothing special... 
     System.Threading.Thread.Sleep(5000); 
     if (NotifyCompleteEvent != null) 
     { 
      NotifyCompleteEvent("My job is completed!"); 
     } 
    } 
} 

现在,这是从另一个类的代码,调用第一种:

MyAsyncClass myClass = null; 


    private void button2_Click(object sender, EventArgs e) 
    { 
     myClass = new MyAsyncClass(); 
     myClass.NotifyCompleteEvent += new MyAsyncClass.NotifyComplete(myClass_NotifyCompleteEvent); 
     //here I start the job inside working class... 
     myClass.Start(); 
    } 

    //here my class is notified from working class when job is completed... 
    delegate void myClassDelegate(string message); 
    void myClass_NotifyCompleteEvent(string message) 
    { 
     if (this.InvokeRequired) 
     { 
      Delegate d = new myClassDelegate(myClass_NotifyCompleteEvent); 
      this.Invoke(d, new object[] { message }); 
     } 
     else 
     { 
      MessageBox.Show(message); 
     } 
    } 

让我知道如果我要解释一些细节。

替代这是BackgroudWorker

+0

我只是等待线程结束。我不想有一个自定义类,我不想使用Action Fired方法等待。有任何提示只是等待一个线程?我没有任何行动,我需要做后,我只需要停止执行父线程,直到子线程完成,以便没有更多的子线程可以执行所述代码。锁定代码块不能与公共静态只读变量一起工作 –

+0

最接近于此的是后台工作者。但是你必须写几行。检查此链接的更多细节:http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx –

+0

这也不会工作,因为在我的行动我需要自定义数据没有可用事件参数 –