Solved

Need more help on threads...

Posted on 2007-11-18
11
248 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get the web content with different encodings 2 44
Where can I ask about my ASP.NET MVC project? 6 35
Video Player 11 23
Cant save 3D 4 19
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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