简体   繁体   中英

ManualResetEvent.WaitOne() throws NullReferenceException: Object reference not set to an instance of an object

I have a weird random NPE error when dealing with ManualResetEvent.WaitOne(). Here's my code.

I have a method that creates ManualResetEvent object and then it passes it down to the Windows Workflow Foundation (WWF) workflow instance as one of the dependency parameter (manualResetEvent) and then I go into 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();

When the job is done within WWF, I simply call manualResetEvent.set().

if (this.manualResetEvent != null)
{
    this.manualResetEvent.Set();
}

All these compile well and while running, I see that the flow gets into the WWF as expected and the caller does wait on WaitOne() call too.

The moment the WWF invokes manualResetEvent.Set() to notify the caller, I see an NPE exception with the caller NOT WWF.

System.NullReferenceException: Object reference not set to an instance of an object.

I really don't know where this exception arises from. When I debug this code in VS IDE, all works well but only when with the application in the Release mode, I see this exception.

What am I doing wrong here?

I found the answer to my question. Based on James Thorpe suggestion, I printed the stack-trace from the exception and it turns out the application is sending a NULL data point when invoking this method and WaitOne() is not causing the NPE. When I ran the unit-test from VS IDE debugger, I passed in a good value hence no exception found. After fixing the caller method, all is working well. Thanks everyone who pitched in ideas to resolve this issue.

You need to add the object to the dictionary

Dictionary<String, Object> wfArgs = new Dictionary<string, object>();
wfArgs.Add("manualResetEvent", manResetEvt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM