Reputation: 20004
I'm creating a new thread and within the background thread method I do work and then call another method to do work.
myThread = new Thread(new ThreadStart(doWork));
myThread.Start();
The problem is that when I leave the background worker method to go to another method and execute this :
browser.SelectList(Find.ById("selStartYear")).SelectByValue(startYear);
I get an InvalidCastException
.
When my background worker method is finished do I need to do something with the thread? I see that I started the thread, but calling abort on it in the new method it calls just suspends the program.
Edit: I'm using WakiN and created new IE
in the global scope:
IE browser = new IE("http://www.website.com/");
My worker method references this as does the failing method.
Upvotes: 0
Views: 100
Reputation: 30857
Adam provided a complete answer on threading issue. I just another hint. Your thread (as I see in sample code) is not a background thread. Also I think all multi-thread applications needs a plan for a graceful exit (consider a system shutdown).
To find out what's the source of casting error, I suggest breaking that line of code into 3 lines, since one of the parameters is not in the right type.
Upvotes: 0
Reputation: 185643
No, you do not need to do any cleanup on a thread that has finished executing. You should actually strive never to call Abort
, as that's a destructive method and providing a more "polite" means of signaling the thread that it should exit immediately is preferred to ending it violently with Abort
.
Also, if your job is not particularly long-running, then you should probably be using either the new Task
class available in System.Threading.Tasks
or using System.Threading.ThreadPool.QueueUserWorkItem()
instead of spinning up your own thread.
That being said, you aren't providing enough information to answer your InvalidCastException
issue. What is the cast it's trying? What is the relation (if any) between the body of doWork
and the values being used in your failing statement?
Upvotes: 2