TextReader() crashed by Timer_Tick() + this.ActiveControl?

I have child form that has a TextReader to load some values on being called. It keeps crashing the moment I summon the form. This form has a Timer that updates the text of a TextBox this way:

textbox.Text = this.ActiveControl.Name;

I notice that disabling the timer during the loading operation stops the crashing from happening. Am I right to say that the TextReader is interfered by this.ActiveControl and causes the crash?

The VS Just-in-Timer debugger gives out differe [xxxx] error code each time. Trying to debug with VS gets "Unable to attach crashing process. A debugger is already attached."

Answers


It is likely that you have a race condition: if the ActiveControl is not yet initialized when the timer is called (and tries to access it), you will get a crash. As you mentioned you need to ensure that the timer does not access the invalid ActiveControl until it is fully intialized (by either disabling it until then or protecting the access by some other means (e.g. mutex/semaphore))


Thank you everyone for your help. I overlooked Hans Passant's reply. Now when I nest my TextBox text update inside an if this way:

if (this.ActiveControl != null) { tbTest.Text = this.ActiveControl.Name; }

no more crashing occurs, even if I do not disable the timer. Kaboom, Hans nailed the error! ^^

Thanks to Attila for giving me the pointer to race condition. Very useful in the knowledge toolbox.


Need Your Help

Error with wait conditions

hardware vhdl xilinx spartan

I am a beginner to VHDL, and I am trying to make a multiplier, but the code I have to use from the book is not compiling right with the xilinx software. The code is:

Does A Default Sort Order in DAL Violate Separation of Concerns

sql sorting data-access-layer n-tier-architecture bll

I have an n-tier application which among other things includes a Data Access Layer (DAL) AND a Business Logic Layer (BLL). I use SQL queries and stored procs in the DAL which I use to fill my objec...