Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need more help on threads...

Posted on 2007-11-18
11
Medium Priority
?
253 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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