one back ground thread processing!

  • Thread starter Thread starter DBC User
  • Start date Start date
D

DBC User

I have a process that I am writing, which receives the requests through
filewatcher. Once I get the request, I need to start a thread asyn. I
shouldn't start the thread again until the running thread is completed
even if there are more requests pending. Here is how I precive it and
please correct me if I am wrong or going to be bad on performance.

1. On file add/delete on the file watcher, I run a method called
processrequests.
2. In this I will check and see if any requests pending.
3. If any requests pending,
3.1 I will check and see if the thread is running (I still don't know
how to do this)
3.2 If the thread is not running start the thread to process the
request.
3.3 if the thread is already running get out of the method. Since the
request will be processed when the step 4 completes.

4. When thread completes the request, it removes the requested file
thus calling processrequests and I go into loop here.

My questions are
1. How can I create only one thread that is visible to processrequest
method.
2. How can I make sure if that thread is running (I remember looking at
the state of the thread but I heard some issues with it, even though
the thread is running, it come back with the status of completed, I may
be wrong)

Thanks.
 
DBC said:
I have a process that I am writing, which receives the requests through
filewatcher. Once I get the request, I need to start a thread asyn. I
shouldn't start the thread again until the running thread is completed
even if there are more requests pending. Here is how I precive it and
please correct me if I am wrong or going to be bad on performance.

1. On file add/delete on the file watcher, I run a method called
processrequests.
2. In this I will check and see if any requests pending.
3. If any requests pending,
3.1 I will check and see if the thread is running (I still don't know
how to do this)
3.2 If the thread is not running start the thread to process the
request.
3.3 if the thread is already running get out of the method. Since the
request will be processed when the step 4 completes.

4. When thread completes the request, it removes the requested file
thus calling processrequests and I go into loop here.

My questions are
1. How can I create only one thread that is visible to processrequest
method.
2. How can I make sure if that thread is running (I remember looking at
the state of the thread but I heard some issues with it, even though
the thread is running, it come back with the status of completed, I may
be wrong)

What you want, it seems is a background worker thread that consumes
requests off a queue, and a foreground thread that waits for an
external stimulus (file watcher) and in response to that queues
requests to be processed.

You might want to take a look at Jon Skeet's article on
multi-threading. In that article he gives precisely this example.

http://www.yoda.arachsys.com/csharp/threads/
 
Back
Top