0

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! :)

aChaLop
  • 31
  • 5
  • Why use `Invoke` to raise event? – Lei Yang Nov 22 '16 at 01:16
  • Please check this question http://stackoverflow.com/questions/2367718/automating-the-invokerequired-code-pattern – Jaime Macias Nov 22 '16 at 01:22
  • I need to invoke the event while InvokeRequired = true because this is running in a secondary thread. If I try to access to form control (rtbData, for instance) in this section of code, it gives me the following error: Cross-thread operation not valid: Control 'rtbData' accessed from a thread other than the thread it was created on. – aChaLop Nov 22 '16 at 01:24
  • I thing that [link](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) is not the same. – aChaLop Nov 22 '16 at 01:34
  • What is `ExecFinished`? Can you debug through there and find the null reference? Really, it looks like a debugging problem and should be closed as a duplicate which explains *how* to debug and solve the problem. – Rob Nov 22 '16 at 01:36
  • I debug and none of the variables is null. I think the problem is not as trivial as a null variable. `ExecFinished` is a event. – aChaLop Nov 22 '16 at 01:41
  • Is `ExecFinished` null? – Rob Nov 22 '16 at 01:41
  • `ExecFinished` is not null. Is a custom event of Windows.Form instance. – aChaLop Nov 22 '16 at 01:44
  • It can be an event *and* be null. Are you *absolutely sure*? What happens if you wrap the code in `if (ExecFinished != null)`? Null reference exceptions don't appear from nowhere. *Something* is null, and I bet it's because you haven't attached any listeners to the event. – Rob Nov 22 '16 at 01:45
  • `if(ExecFinished != null)` gives the following syntax error: _Cannot apply operator != to operands of type method group and null_. – aChaLop Nov 22 '16 at 01:51
  • This is not duplicated. Please check this. – aChaLop Nov 22 '16 at 02:05
  • Get the exception to fire again, [copy the exception details](https://blogs.msdn.microsoft.com/saraford/2008/08/07/did-you-know-you-can-copy-the-exception-details-with-one-click-from-the-exception-assistant-276/) and add them to your question. You will at minimum need to provide the full exception details if you want your question reopened. – Scott Chamberlain Nov 22 '16 at 02:12
  • Thank you for the advice @ScottChamberlain. Are the details enough now? – aChaLop Nov 22 '16 at 02:30
  • You likely have a exception being thrown in your `else` block, you are only seeing the exception when the invoke returns. Enable [break when exception thrown](https://msdn.microsoft.com/en-us/library/x85tt0dd.aspx) and see if you can find the real location of the exception. – Scott Chamberlain Nov 22 '16 at 02:41
  • Also, is it possible that the constructor for `this` was run on a non UI thread? That can cause problems with invoking too. – Scott Chamberlain Nov 22 '16 at 02:42
  • The exception was occurring in the else block. Thank you very much!!! – aChaLop Nov 22 '16 at 03:04

0 Answers0