D
Derrick Tan
Hi There!
I'm working on my first app in C# and I have a slight problem which is
somewhat inconsistent.
Here's the code:
===============================================================
public delegate void BoolEventHandler( bool boolean );
public void ResignReply( bool clientAgreed )
{
if (this.InvokeRequired)
{
BoolEventHandler bHandler = new BoolEventHandler( this.ResignReply );
this.Invoke( bHandler, new object[] { clientAgreed } );
return;
}
// Code that changes controls in the main window form.
//
}
===============================================================
Basically I'm calling this method from a thread that has not created the
controls I want to change, so I have to call Invoke. The problem is I
sometimes get a NullReferenceException on the line with:
this.Invoke( . . . );
An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object.
Call Stack:
system.windows.forms.dll!System.Windows.Forms.Control::MarshaledInvoke(System.Windows.Forms.Control
caller = {Backgammon.BackgammonForm}, System.Delegate method =
{Backgammon.BoolEventHandler}, System.Object[] args = {Length=1}, bool
synchronous = true) + 0x330 bytes
system.windows.forms.dll!System.Windows.Forms.Control::Invoke(System.Delegate
method = {Backgammon.BoolEventHandler}, System.Object[] args =
{Length=1}) + 0x30 bytes
Backgammon.exe!Backgammon.BackgammonForm.ResignReply(bool clientAgreed =
true) Line 1442 + 0xe bytes
I was hoping someone could explain to me why this is happening. On a
side note, I have almost identical code for a different method which
doesn't seem to throw the exception. . .
If you need more information, please let me know. . .
Many Thanks,
Derrick
I'm working on my first app in C# and I have a slight problem which is
somewhat inconsistent.
Here's the code:
===============================================================
public delegate void BoolEventHandler( bool boolean );
public void ResignReply( bool clientAgreed )
{
if (this.InvokeRequired)
{
BoolEventHandler bHandler = new BoolEventHandler( this.ResignReply );
this.Invoke( bHandler, new object[] { clientAgreed } );
return;
}
// Code that changes controls in the main window form.
//
}
===============================================================
Basically I'm calling this method from a thread that has not created the
controls I want to change, so I have to call Invoke. The problem is I
sometimes get a NullReferenceException on the line with:
this.Invoke( . . . );
An unhandled exception of type 'System.NullReferenceException' occurred
in system.windows.forms.dll
Additional information: Object reference not set to an instance of an
object.
Call Stack:
system.windows.forms.dll!System.Windows.Forms.Control::MarshaledInvoke(System.Windows.Forms.Control
caller = {Backgammon.BackgammonForm}, System.Delegate method =
{Backgammon.BoolEventHandler}, System.Object[] args = {Length=1}, bool
synchronous = true) + 0x330 bytes
system.windows.forms.dll!System.Windows.Forms.Control::Invoke(System.Delegate
method = {Backgammon.BoolEventHandler}, System.Object[] args =
{Length=1}) + 0x30 bytes
Backgammon.exe!Backgammon.BackgammonForm.ResignReply(bool clientAgreed =
true) Line 1442 + 0xe bytes
I was hoping someone could explain to me why this is happening. On a
side note, I have almost identical code for a different method which
doesn't seem to throw the exception. . .
If you need more information, please let me know. . .
Many Thanks,
Derrick