Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run Multiple Threads in Parallel

Posted on 2013-01-03
7
Medium Priority
?
394 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
[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
  • 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 36

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
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.

 

Author Comment

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

Thanks
0
 
LVL 36

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 36

Accepted Solution

by:
Miguel Oz earned 2000 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

Technology Partners: 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!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

688 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