NullReferenceException on BeginInvoke?

  • Thread starter Thread starter Jan Hudecek
  • Start date Start date
J

Jan Hudecek

Hello,

I've the following code:

private void DoneVisual(object o)
{
MessageBox.Show("Done");
pbProgress.Value = 0;
}

private void Finished()
{
this.BeginInvoke(new System.Threading.WaitCallback
(DoneVisual), null);
}

The method Finished gets executed from a worker thread. It finished
ok. But then instead of DoneVisual being called from the main thread
an exception gets thrown at:

System.NullReferenceException: Object reference not set to an instance
of an object.
System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallbackDo
(System.Windows.Forms.Control.ThreadMethodEntry tme) + 0x1a5 bytes
System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(object obj)
+ 0x69 bytes
mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object
userData) + 0x51 bytes
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.Threading.ExecutionContext.RunInternal
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, object state) + 0x67 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, object state) + 0x45 bytes
System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallback
(System.Windows.Forms.Control.ThreadMethodEntry tme) + 0x69 bytes
System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallbacks() + 0x105 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref
System.Windows.Forms.Message m) + 0x12f bytes
System.Windows.Forms.dll!
System.Windows.Forms.ScrollableControl.WndProc(ref
System.Windows.Forms.Message m) + 0x2a bytes
System.Windows.Forms.dll!
System.Windows.Forms.ContainerControl.WndProc(ref
System.Windows.Forms.Message m) + 0x10 bytes
System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref
System.Windows.Forms.Message m) + 0x40 bytes
System.Windows.Forms.dll!
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref
System.Windows.Forms.Message m) + 0x10 bytes
System.Windows.Forms.dll!
System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref
System.Windows.Forms.Message m) + 0x31 bytes
System.Windows.Forms.dll!
System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr
hWnd, int msg = 49527, System.IntPtr wparam, System.IntPtr lparam) +
0x57 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
(int dwComponentID, int reason = -1, int pvLoopData = 0) + 0x24e
bytes
System.Windows.Forms.dll!
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int
reason = -1, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.ApplicationContext}) + 0x177 bytes
System.Windows.Forms.dll!
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int
reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run
(System.Windows.Forms.Form mainForm) + 0x31 bytes


Any idea what's wrong?

Thanks,
Jan
 
Hi again,

I've solved it problem was with passing null as the parameter. Looks
like a bug in framework...

Cheers,
Jan

Hello,

I've the following code:

        private void DoneVisual(object o)
        {
            MessageBox.Show("Done");
            pbProgress.Value = 0;
        }

        private void Finished()
        {
            this.BeginInvoke(new System.Threading.WaitCallback
(DoneVisual), null);
        }

The method Finished gets executed from a worker thread. It finished
ok. But then instead of DoneVisual being called from the main thread
an exception gets thrown at:

System.NullReferenceException: Object reference not set to an instance
of an object.
        System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallbackDo
(System.Windows.Forms.Control.ThreadMethodEntry tme) + 0x1a5 bytes
        System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(object obj)
+ 0x69 bytes
        mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object
userData) + 0x51 bytes
        [Native to Managed Transition]
        [Managed to Native Transition]
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, object state) + 0x67 bytes
        mscorlib.dll!System.Threading.ExecutionContext.Run
(System.Threading.ExecutionContext executionContext,
System.Threading.ContextCallback callback, object state) + 0x45 bytes
        System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallback
(System.Windows.Forms.Control.ThreadMethodEntry tme) + 0x69 bytes
        System.Windows.Forms.dll!
System.Windows.Forms.Control.InvokeMarshaledCallbacks() + 0x105 bytes
        System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref
System.Windows.Forms.Message m) + 0x12f bytes
        System.Windows.Forms.dll!
System.Windows.Forms.ScrollableControl.WndProc(ref
System.Windows.Forms.Message m) + 0x2a bytes
        System.Windows.Forms.dll!
System.Windows.Forms.ContainerControl.WndProc(ref
System.Windows.Forms.Message m) + 0x10 bytes
        System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref
System.Windows.Forms.Message m) + 0x40 bytes
        System.Windows.Forms.dll!
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref
System.Windows.Forms.Message m) + 0x10 bytes
        System.Windows.Forms.dll!
System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref
System.Windows.Forms.Message m) + 0x31 bytes
        System.Windows.Forms.dll!
System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr
hWnd, int msg = 49527, System.IntPtr wparam, System.IntPtr lparam) +
0x57 bytes
        [Native to Managed Transition]
        [Managed to Native Transition]
        System.Windows.Forms.dll!
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.Unsa­feNativeMethods.IMsoComponentManager.FPushMessageLoop
(int dwComponentID, int reason = -1, int pvLoopData = 0) + 0x24e
bytes
        System.Windows.Forms.dll!
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int
reason = -1, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.ApplicationContext}) + 0x177 bytes
        System.Windows.Forms.dll!
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int
reason, System.Windows.Forms.ApplicationContext context) + 0x61 bytes
        System.Windows.Forms.dll!System.Windows.Forms.Application..Run
(System.Windows.Forms.Form mainForm) + 0x31 bytes

Any idea what's wrong?

Thanks,
Jan
 
Back
Top