Link to home
Start Free TrialLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

asked on

Need more help on threads...

I have a main process, with one additional thread which works great.  I want to be able to do other things while the separate thread is running, but then when the thread completes, I want to resume the next statement after which I started the thread.  (hope that makes sense.)...  my code follows:

                    ParameterizedThreadStart pts = new ParameterizedThreadStart(getISBNPricingData);
                    Thread t = new Thread(pts);
                    ArrayList al = new ArrayList();
                    al.Add(initialISBNRetValue);
                    al.Add(isbnRetValue);
                    t.Start(al);
                   //  need to come back here to next statement when thread is finished processing, but not until


How do I do this?
Avatar of RedKelvin
RedKelvin

Hi there,
To do other stuff while the thread is running check to see if the thread is alive, like this


while (t.IsAlive)
{
    //Perform other tasks
}

Open in new window

you just need:
t.Join()
ASKER CERTIFIED SOLUTION
Avatar of RedKelvin
RedKelvin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmmarsh

ASKER

I tried thread.join(), but "message pumping" was "suspended", ie I couldn't do anything in the Windows program while the thread was running.

I need to be able to run other tasks in the main form of my Windows program.

Is there a way I can do something like this?  
while(signal not received from thread)
sleep();
Avatar of rmmarsh

ASKER

I also noticed that the join() will "block the calling thread" until the thread terminates... doesn't this mean that my main process (mainForm) will be blocked until the thread terminates?  I want to run the thread, and still be able to run other processes (methods) in the windows program.

Hope that makes it clearer...

R
Yes, the example I provided above will do this
As an alternative, you can use some kind of synchronization event, by example a ManualResetEvent object.

public ManualResetEvent manualEvent;   // put as a class member

// do all the thread stuff
                    ParameterizedThreadStart pts = new ParameterizedThreadStart(getISBNPricingData);
                    Thread t = new Thread(pts);
                    ArrayList al = new ArrayList();
                    al.Add(initialISBNRetValue);
                    al.Add(isbnRetValue);

                    manualEvent = new ManualResetEvent(false);
                    t.Start(al);
                    manualEvent.WaitOne();

// inside the thread
void getISBNPricingData(ArrayList array)
{
      // do all your stuff
      manualEvent.Set();
}
Avatar of rmmarsh

ASKER

Thank you all; it works like a champ!
Avatar of rmmarsh

ASKER

Thank you all; it works like a champ!