?
Solved

Need more help on threads...

Posted on 2007-11-18
11
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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
Independent Software Vendors: 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 400 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 1600 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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