C# - Background Thread / Task(www.stev.org)

submitted by mistralolmistralol(283) 7 months, 26 days ago

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 |category: |Views: 49

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by dpetersondpeterson(4397) 7 months, 25 days ago +1

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.

Reply

posted by mistralolmistralol(283) 7 months, 24 days ago 0

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 :)

Reply

information Login or create an account to comment on this story