c# - Delegate to an instance method cannot have null Intermittent on runtime -


i got error , cannot understand error. use win form , .net 3.5. problem is, can compiled , intermittent. shown today, guess occur rare (maybe once after 5000 run). want know makes error thrown, , possible workaround. here example of how implemented code.

my application multithread, , method singleton.


exception type: system.argumentexception exception message: delegate instance method cannot have null 'this'. exception stack trace:     @ system.multicastdelegate.thrownullthisindelegatetoinstance()    @ system.multicastdelegate.ctorclosed(object target, intptr methodptr) 

  class caller   {     private classa thea;     public caller()     {       thea = new classa();     }      public void button_click()     {       thea.execute(false);     }     public void button2_click()     {       thea.execute( true );     }   }    interface iclassa   {     void actionminus();   }   class classa   {     public int variablea = 0;     public void execute( bool wait )     {       classb instanceb = new classb( );       thread thread = new thread( instanceb.action ) // error in here       {          name = "executor",         priority = threadpriority.highest       };        thread.start();       if( wait )         thread.join();     }     public void actionminus()     {       //someaction1       variablea -= 2;       //someaction2     }   }    class classb   {     private readonly classa instancea;     public classb( classa instance )     {       instancea = instance;     }     public void action()     {       //some other action3       instancea.variablea += 5;       //some other action4       instancea.actionminus();       //some other action5     }   } 

it looks works me.

what context of program?

i wrote simple empty shell containing code, program end before thread gets kicked off.

i had add console.readkey() method.

screenshot

here whole code used:

using system; using system.collections.generic; using system.linq; using system.text; using system.threading;  namespace delegatetoinstance {    class program {     static void main(string[] args) {       var obj = new caller();       obj.button_click();       console.readkey();     }   }    class caller   {     private classa thea;     public caller()     {       thea = new classa();     }      public void button_click()     {       thea.execute(false);     }     public void button2_click()     {       thea.execute( true );     }   }    interface iclassa   {     void actionminus();   }    class classa   {     public int variablea = 0;     public void execute( bool wait )     {       classb instanceb = new classb(this);       thread thread = new thread( instanceb.action ) // error in here       {          name = "executor",         priority = threadpriority.highest       };        thread.start();       if( wait )         thread.join();     }     public void actionminus()     {       //someaction1       variablea -= 2;       //someaction2     }   }    class classb   {     private readonly classa instancea;     public classb( classa instance )     {       instancea = instance;     }     public void action()     {       //some other action3       instancea.variablea += 5;       //some other action4       instancea.actionminus();       //some other action5     }   }  } 

i hate downvoters, gave upvote.


Comments