Solved

Need more help on threads...

Posted on 2007-11-18
11
246 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now