C# - Background Thread / Task

added by mistralol
9/30/2011 8:42:49 AM

351 Views

The basic idea for this is to create a class which will control the start / stop and running state of the background thread. The reasons for this will become clear in the following posts. For this example I am using the class called ThdExample.


2 comments

dpeterson
9/30/2011 8:42:45 AM
If you aren't the author, then ignore the rest of this ;-)

Seems like a nice start, a couple of suggestions on the code though:

- Probably not necessary to throw an exception if the thread is already started, just log that it happened and drop out of the function.
- Never, ever, ever use a try{} catch{} and leave the catch empty. Even if you are going to allow the program to continue it should at least be logged, and depending on the type of error you may not want the program to continue. I know this is example code, but it's better to just leave the try{} catch{} out if you're not going to catch.
- It's no longer necessary to use ParameterizedThreadStart as the VB and C# compilers infer it based upon the signature of the Start function. So you can just initialize a new Thread and call Start(myParams). Not a criticism, just letting you know that option is available.

Thanks for sharing, keep posting the articles as you continue the series. I'd like to see where this goes.

mistralol
10/1/2011 11:16:16 AM
Hi,

Thanks for the response. Yup I am the author. It is a series of posts the try / catch will be fixed later. If you want to see where its going to next have a look at http://www.stev.org/post/2011/10/01/C-Abstract-Background-Thread-Task.aspx

There the real interesting stuff is going to start coming out over the next week or so involving generic queues and various other things :)

I didn't know that the ParameterizedThreadStart isn't required any more. Thanks :)