Solved

parallel invoke and multithreading

Posted on 2013-02-07
2
507 Views
Last Modified: 2013-02-07
Trying to figure out if parallel invoke and multithreading play perform similar functionality as the following two code :

Parallel :

Parallel.Invoke(() => FillDictionary(dict1, 1000000), () => FillDictionary(dict2, 1000000));


Multithreading :

var tasks = new Task[]
{
	Task.Factory.StartNew(
		() =>
		{
			Thread.Sleep(200);
			"Task 1 complete".Dump();
		}
	),
	Task.Factory.StartNew(
		() =>
		{
			Thread.Sleep(250);
			"Task 2 complete".Dump();
		}
	),
	Task.Factory.StartNew(
		() =>
		{
			Thread.Sleep(200);
			"Task 3 complete".Dump();
		}
	)
};

"Waiting for all tasks".Dump();
Task.WaitAny(tasks);
"Done waiting".Dump();

Open in new window


will both of the code run methods at once , or multithreading as in the code will sequentially run one after another?
0
Comment
Question by:doramail05
  • 2
2 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 38863064
Neither will definately run the tasks sequentially.

Parallel.Invoke will attempt to run the tasks concurrently (simultaneously - time slices to each)
Multithreading creates separate threads, one for each task that you give it and these will run concurrently.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38863076
Consider two tasks A and B.

Sequential code would be
A();
B();

In that you are certain that the code A is run completely before code B is even started.


When you use threads, one thread for A and one for B.
A might finish before B even starts.
B might finish before A.
You have no control over what is run when
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now