In my windows application i have a usercontrol, which in turn host few other usercontrols.
Just before the end of the main user control’s constructor, i try to create a thread… but it does not appear to be created:
mainUserControl() { var t=new Thread(ThreadJob); t.IsBackground=true; t.Start(); } private void ThreadJob() { //Thread.Sleep(120000); //if(txtStatus.InvokeRequired) { // txtStatus.Invoke(new MethodInvoker(delegate { txtStatus.Text='Thread started'; })); //} txtStatus.Text='sample'; }
This code does not work: i take this as evidence that the thread is not spawned, as if it were then txtStatus.Text='sample'; would have thrown an exception…. right?
So what’s happening here? Why isn’t my thread being created?
My guess is, since you took out your delay, the thread is spawning, setting the value, and shutting down.
Accessing
txtStatus.Textfrom the wrong thread is not guaranteed to throw, though. There are situations where you can access a property on a control from a background thread and it will not throw. (You still shouldn’t do it, though!)If you still believe the thread is not starting, you could try setting a breakpoint there – I’m fairly certain you’ll see that it’s hitting that point.