Solved

Need more help on threads...

Posted on 2007-11-18
11
250 Views
Last Modified: 2011-09-20
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?
0
Comment
Question by:rmmarsh
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20309868
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

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20309872
you just need:
t.Join()
0
 
LVL 22

Accepted Solution

by:
RedKelvin earned 400 total points
ID: 20309876
more precisely

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

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Assisted Solution

by:cookre
cookre earned 100 total points
ID: 20309915
Note that if you want to stop the main line dead in its tracks, Join() would be less resource intensive than the while loop.  Also, a Join() allows message pump processing to continue normally, whereas the loop will block message pump processing until the main line is swapped or it does a DoEvents().
0
 
LVL 22

Assisted Solution

by:RedKelvin
RedKelvin earned 400 total points
ID: 20310021
good point cookre,

if you are going to use .isAlive, make sure you put
Application.DoEvents();
Inside the loop or the message pump will be blocked.

if the loop is going to repeat very quickly I usually put a sleep in as well, to prevent the processor getting tied up

System.Threading.Thread.Sleep(1000);
0
 

Author Comment

by:rmmarsh
ID: 20310144
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();
0
 

Author Comment

by:rmmarsh
ID: 20310149
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
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20310204
Yes, the example I provided above will do this
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20310236
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();
}
0
 

Author Closing Comment

by:rmmarsh
ID: 31409863
Thank you all; it works like a champ!
0
 

Author Comment

by:rmmarsh
ID: 20312954
Thank you all; it works like a champ!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question