Solved

Run Multiple Threads in Parallel

Posted on 2013-01-03
7
380 Views
Last Modified: 2013-01-08
Hello Experts,

 I'm using the following code to run a Thread in parallel to the program:
            Thread myThread = new System.Threading.Thread(delegate()
            {
                //Your code here
                System.Threading.Thread.Sleep(14000);
                close_window_by_handle(ie);
            });
            myThread.Start();

Open in new window



In this code I need to wait 14 seconds ( according to the customer request )

the problem is when I run this code few times during 14 seconds it stuck

 How do I enhance my code check if a thread is running, if YES it create a new thread in addition to the running  one
else it use the Stopped thread

Thanks
0
Comment
Question by:madmare
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38739768
What do you mean by "else it use the Stopped thread?" A terminated thread cannot be restarted, and you must create a new Thread instance in that case.
0
 

Author Comment

by:madmare
ID: 38739775
I'll fix my self

 in any way create new Thread :)

Thanks
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 38739802
Is this a desktop app (eg. Winform) or Asp.net?
How many threads are you running in parallel in your App?

If this number is small (<20) then you can use ThreadPool.QueueUserWorkItem
else consider using TPL (.Net 4.0 up)

Note: Check TPL vs queueuserworkitem
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:madmare
ID: 38739815
can you please give me the example for ThreadPool.QueueUserWorkItem ?

Thanks
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 38739825
Check the example to run the method ProcessFile at:
http://www.dotnetperls.com/threadpool
0
 

Author Comment

by:madmare
ID: 38739995
Thanks mas

 I looked again to the request, I think that TPL can be helpful for  me

 can you please help to convert my code to TPL

Thanks
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 38750199
using System.Threading.Tasks;

Open in new window


Your code should look like:
            Parallel.Invoke(
                () => {
                    //Your code here
                System.Threading.Thread.Sleep(14000);
                close_window_by_handle(ie);
                }
            );

Open in new window

Note: I use Parallel.Invoke because you are not returning  any value.
If you are returning  a value use:
Task<int> task1 = Task<int>.Factory.StartNew(() => DoWork());
// Get the results and write them out.
Console.WriteLine("Result ={0}", task1.Result);

Open in new window


where
static int DoWork()
{
     //Your code here
     System.Threading.Thread.Sleep(14000);
     close_window_by_handle(ie);
     return 0;
}

Open in new window

0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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