2012-11-05 93 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

這也不會工作,因爲在我的行動我需要自定義數據沒有可用事件參數 –