[英]ManualResetEvent throws NullReferenceException: Object reference not set to an instance of an object
[英]ManualResetEvent.WaitOne() throws NullReferenceException: Object reference not set to an instance of an object
處理ManualResetEvent.WaitOne()時,我有一個奇怪的隨機NPE錯誤。 這是我的代碼。
我有一個創建ManualResetEvent對象的方法,然后將其作為依賴參數(manualResetEvent)之一傳遞給Windows Workflow Foundation(WWF)工作流實例,然后我進入manualResetEvent.WaitOne()API。
ManualResetEvent manResetEvt = new ManualResetEvent(false);
Dictionary<String, Object> wfArgs = new Dictionary<string, object>();
wfArgs["manualResetEvent"] = manResetEvt;
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(MyWWFProcess), wfArgs);
instance.Start();
manResetEvt.WaitOne();
當作業在WWF中完成時,我只需調用manualResetEvent.set()。
if (this.manualResetEvent != null)
{
this.manualResetEvent.Set();
}
所有這些編譯好並且在運行時,我看到流程按預期進入WWF,並且調用者也在等待WaitOne()調用。
WWF調用manualResetEvent.Set()來通知調用者的那一刻,我看到調用者不是WWF的NPE異常。
System.NullReferenceException:未將對象引用設置為對象的實例。
我真的不知道這個例外的起源。 當我在VS IDE中調試此代碼時,一切正常,但只有當應用程序處於發布模式時,才會看到此異常。
我在這做錯了什么?
我找到了問題的答案。 基於James Thorpe的建議,我從異常中打印了堆棧跟蹤,結果發現應用程序在調用此方法時發送了一個NULL數據點,並且WaitOne()沒有導致NPE。 當我從VS IDE調試器運行單元測試時,我傳遞了一個很好的值,因此沒有發現異常。 修復調用方法后,一切正常。 感謝所有提出想法解決此問題的人。
您需要將對象添加到字典中
Dictionary<String, Object> wfArgs = new Dictionary<string, object>();
wfArgs.Add("manualResetEvent", manResetEvt);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.