flavour404
flavour404

Reputation: 6312

Threading, how to instantiate multiple threads without using a class structure

What I have is a loop reading some data and when a set of circumstances are met I need to instantiate a thread. However, the created thread might not complete before the loop criteria is met and I need to create another thread doing the same thing. This is part of an ocr app and I haven't done much thread work before.

while loop
  if(criteria)
  {
    observer = new BackgroundWorker();
    observer.DoWork += new DoWorkEventHandler(observer_DoObserving);
    observer.RunWorkerAsync();
  }

the observer_DoObserving function calls the ocr app, waits for a response and then processes it appropriately and sets observer = null at the end. So how would I create multiple instances of the 'observer' thread. Of course instantly I thought of a class structure, is this an appropriate way to do it or is there another way that is appropriate for threading.

I hope this makes sense.

Thanks, R.

Upvotes: 0

Views: 673

Answers (3)

Jim Mischel
Jim Mischel

Reputation: 133975

How you handle this depends in large part on whether the background thread needs to communicate anything to the main thread when it's done. If the background thread really is "fire and forget", then there's no particular reason why you need to maintain a reference to the observer. So you could write:

while loop
{
  if(criteria)
  {
    BackgroundWorker observer = new BackgroundWorker();
    observer.DoWork += new DoWorkEventHandler(observer_DoObserving);
    observer.RunWorkerAsync();
  }
}

The thread does its work and goes away. observer is a local variable that goes out of scope when execution leaves the if block. There's no way that the variable will be overwritten if you have to start another observer thread before the first one is finished.

If you need to keep track of information for individual observers, then you'd create an object of some type (a class that you define) that contains information about the worker's state, and pass that to the RunWorkerAsync method. The worker can then modify that object and send you progress notifications (see the ProgressChanged event and the ReportProgress method), and also report the status when the worker has finished (see RunWorkerCompleted, the state object you passed to RunWorkerAsync will be in the RunWorkerCompletedEventArgs.UserState property).

Upvotes: 1

Andrew Barber
Andrew Barber

Reputation: 40139

I am not entirely able to grasp what you are doing exactly, so I may or may not be helpful here;

You seem to be, in part, asking if it's appropriate to create a class to hold some data indicating the state of a thread or what it's working on. That is entirely appropriate to do, provided the object is not an 'expensive' one to create. (no creating Exception objects and throwing them around all the time, for instance).

Upvotes: 0

LukeH
LukeH

Reputation: 269288

You could use the thread pool, specifically ThreadPool.

while (something)
{
    if (criteria)
    {
        // QueueUserWorkItem also has an overload that allows you to pass data
        // that data will then be passed into WorkerMethod when it is called
        ThreadPool.QueueUserWorkItem(new WaitCallback(WorkerMethod));
    }
}

// ...

private void WorkerMethod(object state)
{
    // do work here
}

Upvotes: 2

Related Questions