Here is a simplified version of what I'm doing:
delegate void ValueDelegate(object sender, EventArgs e);
private void ExecFinished(object sender, EventArgs e)
{
if (InvokeRequired)
{
var pp = new ValueDelegate(ExecFinished);
this.Invoke(pp, sender, e);
}
else
{
//do something
}
}
The exception that is thrown is a NullReferenceException in this.Invoke(pp, sender, e);
This code is running on a second thread and when I try to access a form control it gives the following error: Cross-thread operation not valid: Control 'control_name' accessed from a thread other than the thread it was created on.
None of the variables is null in the debugging process.
Exception details:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at SpectrometryAnalysis.TraditionalDataForm.ExecFinished(Object sender, EventArgs e) in D:\...\TraditionalDataForm.cs:line 393
at SpectrometryAnalysis.Model.JavaMachineLearningCaller.<>c__DisplayClass1_0.<Redirect>b__0(Object a) in D:\...\Model\JavaMachineLearningCaller.cs:line 84
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
InnerException:
Any idea? Thanks! :)