when should i use parallel foreach and when should i use parallel linq?











up vote
2
down vote

favorite












I have tried these below code,it working fine and the performance also good.But when i run the nunit testcases its not working.I dont have idea about parallel linq or parallel foreach.Is it possible convert the code to parallel linq.Please
give your valuable suggestion.



Parallel.ForEach(ListofData, item =>
{
if (this.DataSource != null && item != null)
{
string itemText = this.GetListViewItemValue(item, this.displayMember);
if (!string.IsNullOrEmpty(this.Text) && itemText.ToLower().Equals(this.Text.ToString().ToLower()))
{
this.textBox.Text = itemText.ToString();
this.textBox.SelectAll();
this.listView.SelectedItems.Add(item);
}
}
});









share|improve this question






















  • But when i run the nunit testcases its not working. In what way is it not working?
    – mjwills
    Nov 12 at 4:57










  • If I have 10 testcases, 5 test cases passed other test cases not running further
    – Sam10
    Nov 12 at 5:02










  • That is not sufficient detail @Sam10.
    – mjwills
    Nov 12 at 5:03















up vote
2
down vote

favorite












I have tried these below code,it working fine and the performance also good.But when i run the nunit testcases its not working.I dont have idea about parallel linq or parallel foreach.Is it possible convert the code to parallel linq.Please
give your valuable suggestion.



Parallel.ForEach(ListofData, item =>
{
if (this.DataSource != null && item != null)
{
string itemText = this.GetListViewItemValue(item, this.displayMember);
if (!string.IsNullOrEmpty(this.Text) && itemText.ToLower().Equals(this.Text.ToString().ToLower()))
{
this.textBox.Text = itemText.ToString();
this.textBox.SelectAll();
this.listView.SelectedItems.Add(item);
}
}
});









share|improve this question






















  • But when i run the nunit testcases its not working. In what way is it not working?
    – mjwills
    Nov 12 at 4:57










  • If I have 10 testcases, 5 test cases passed other test cases not running further
    – Sam10
    Nov 12 at 5:02










  • That is not sufficient detail @Sam10.
    – mjwills
    Nov 12 at 5:03













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have tried these below code,it working fine and the performance also good.But when i run the nunit testcases its not working.I dont have idea about parallel linq or parallel foreach.Is it possible convert the code to parallel linq.Please
give your valuable suggestion.



Parallel.ForEach(ListofData, item =>
{
if (this.DataSource != null && item != null)
{
string itemText = this.GetListViewItemValue(item, this.displayMember);
if (!string.IsNullOrEmpty(this.Text) && itemText.ToLower().Equals(this.Text.ToString().ToLower()))
{
this.textBox.Text = itemText.ToString();
this.textBox.SelectAll();
this.listView.SelectedItems.Add(item);
}
}
});









share|improve this question













I have tried these below code,it working fine and the performance also good.But when i run the nunit testcases its not working.I dont have idea about parallel linq or parallel foreach.Is it possible convert the code to parallel linq.Please
give your valuable suggestion.



Parallel.ForEach(ListofData, item =>
{
if (this.DataSource != null && item != null)
{
string itemText = this.GetListViewItemValue(item, this.displayMember);
if (!string.IsNullOrEmpty(this.Text) && itemText.ToLower().Equals(this.Text.ToString().ToLower()))
{
this.textBox.Text = itemText.ToString();
this.textBox.SelectAll();
this.listView.SelectedItems.Add(item);
}
}
});






c# winforms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 4:31









Sam10

375




375












  • But when i run the nunit testcases its not working. In what way is it not working?
    – mjwills
    Nov 12 at 4:57










  • If I have 10 testcases, 5 test cases passed other test cases not running further
    – Sam10
    Nov 12 at 5:02










  • That is not sufficient detail @Sam10.
    – mjwills
    Nov 12 at 5:03


















  • But when i run the nunit testcases its not working. In what way is it not working?
    – mjwills
    Nov 12 at 4:57










  • If I have 10 testcases, 5 test cases passed other test cases not running further
    – Sam10
    Nov 12 at 5:02










  • That is not sufficient detail @Sam10.
    – mjwills
    Nov 12 at 5:03
















But when i run the nunit testcases its not working. In what way is it not working?
– mjwills
Nov 12 at 4:57




But when i run the nunit testcases its not working. In what way is it not working?
– mjwills
Nov 12 at 4:57












If I have 10 testcases, 5 test cases passed other test cases not running further
– Sam10
Nov 12 at 5:02




If I have 10 testcases, 5 test cases passed other test cases not running further
– Sam10
Nov 12 at 5:02












That is not sufficient detail @Sam10.
– mjwills
Nov 12 at 5:03




That is not sufficient detail @Sam10.
– mjwills
Nov 12 at 5:03












1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










CONGRATULATIONS, you were my 1000th answer




when should i use parallel foreach and when should i use parallel
linq?




They both do exactly the same thing, they are both built on the Task Parallel Library, and only differ in semantics.



Take a look at all the overloads and extension methods, and you will see sometimes its just easier to write PLinq, sometimes its easier to write Parallel.For/Foreach



As to your problem, its nothing to do with either approach directly. However you ARE trying to update the UI from a potentially different thread and bound to fail.



The only reason why this wouldn't fail is that the internals has deemed this not Thread-Worthy and is running this on the original context.



I think you really need to rethink your approach, and if you do want to use multiple threads, you will need to Marshal back to the UI Thread accordingly




But when i run the nunit testcases its not working




As for your test cases... I think you have bigger problems at the moment, your code is not thread safe






share|improve this answer























  • If i upload 50000 data to the listview,it loops 50000 times so to improve the performance i used parallel foreach..... can i get any other way to improve the performance...If any please suggest....
    – Sam10
    Nov 12 at 4:57






  • 3




    That is a different question @Sam10.
    – mjwills
    Nov 12 at 4:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255994%2fwhen-should-i-use-parallel-foreach-and-when-should-i-use-parallel-linq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










CONGRATULATIONS, you were my 1000th answer




when should i use parallel foreach and when should i use parallel
linq?




They both do exactly the same thing, they are both built on the Task Parallel Library, and only differ in semantics.



Take a look at all the overloads and extension methods, and you will see sometimes its just easier to write PLinq, sometimes its easier to write Parallel.For/Foreach



As to your problem, its nothing to do with either approach directly. However you ARE trying to update the UI from a potentially different thread and bound to fail.



The only reason why this wouldn't fail is that the internals has deemed this not Thread-Worthy and is running this on the original context.



I think you really need to rethink your approach, and if you do want to use multiple threads, you will need to Marshal back to the UI Thread accordingly




But when i run the nunit testcases its not working




As for your test cases... I think you have bigger problems at the moment, your code is not thread safe






share|improve this answer























  • If i upload 50000 data to the listview,it loops 50000 times so to improve the performance i used parallel foreach..... can i get any other way to improve the performance...If any please suggest....
    – Sam10
    Nov 12 at 4:57






  • 3




    That is a different question @Sam10.
    – mjwills
    Nov 12 at 4:57















up vote
5
down vote



accepted










CONGRATULATIONS, you were my 1000th answer




when should i use parallel foreach and when should i use parallel
linq?




They both do exactly the same thing, they are both built on the Task Parallel Library, and only differ in semantics.



Take a look at all the overloads and extension methods, and you will see sometimes its just easier to write PLinq, sometimes its easier to write Parallel.For/Foreach



As to your problem, its nothing to do with either approach directly. However you ARE trying to update the UI from a potentially different thread and bound to fail.



The only reason why this wouldn't fail is that the internals has deemed this not Thread-Worthy and is running this on the original context.



I think you really need to rethink your approach, and if you do want to use multiple threads, you will need to Marshal back to the UI Thread accordingly




But when i run the nunit testcases its not working




As for your test cases... I think you have bigger problems at the moment, your code is not thread safe






share|improve this answer























  • If i upload 50000 data to the listview,it loops 50000 times so to improve the performance i used parallel foreach..... can i get any other way to improve the performance...If any please suggest....
    – Sam10
    Nov 12 at 4:57






  • 3




    That is a different question @Sam10.
    – mjwills
    Nov 12 at 4:57













up vote
5
down vote



accepted







up vote
5
down vote



accepted






CONGRATULATIONS, you were my 1000th answer




when should i use parallel foreach and when should i use parallel
linq?




They both do exactly the same thing, they are both built on the Task Parallel Library, and only differ in semantics.



Take a look at all the overloads and extension methods, and you will see sometimes its just easier to write PLinq, sometimes its easier to write Parallel.For/Foreach



As to your problem, its nothing to do with either approach directly. However you ARE trying to update the UI from a potentially different thread and bound to fail.



The only reason why this wouldn't fail is that the internals has deemed this not Thread-Worthy and is running this on the original context.



I think you really need to rethink your approach, and if you do want to use multiple threads, you will need to Marshal back to the UI Thread accordingly




But when i run the nunit testcases its not working




As for your test cases... I think you have bigger problems at the moment, your code is not thread safe






share|improve this answer














CONGRATULATIONS, you were my 1000th answer




when should i use parallel foreach and when should i use parallel
linq?




They both do exactly the same thing, they are both built on the Task Parallel Library, and only differ in semantics.



Take a look at all the overloads and extension methods, and you will see sometimes its just easier to write PLinq, sometimes its easier to write Parallel.For/Foreach



As to your problem, its nothing to do with either approach directly. However you ARE trying to update the UI from a potentially different thread and bound to fail.



The only reason why this wouldn't fail is that the internals has deemed this not Thread-Worthy and is running this on the original context.



I think you really need to rethink your approach, and if you do want to use multiple threads, you will need to Marshal back to the UI Thread accordingly




But when i run the nunit testcases its not working




As for your test cases... I think you have bigger problems at the moment, your code is not thread safe







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 3:56

























answered Nov 12 at 4:41









TheGeneral

26.6k63163




26.6k63163












  • If i upload 50000 data to the listview,it loops 50000 times so to improve the performance i used parallel foreach..... can i get any other way to improve the performance...If any please suggest....
    – Sam10
    Nov 12 at 4:57






  • 3




    That is a different question @Sam10.
    – mjwills
    Nov 12 at 4:57


















  • If i upload 50000 data to the listview,it loops 50000 times so to improve the performance i used parallel foreach..... can i get any other way to improve the performance...If any please suggest....
    – Sam10
    Nov 12 at 4:57






  • 3




    That is a different question @Sam10.
    – mjwills
    Nov 12 at 4:57
















If i upload 50000 data to the listview,it loops 50000 times so to improve the performance i used parallel foreach..... can i get any other way to improve the performance...If any please suggest....
– Sam10
Nov 12 at 4:57




If i upload 50000 data to the listview,it loops 50000 times so to improve the performance i used parallel foreach..... can i get any other way to improve the performance...If any please suggest....
– Sam10
Nov 12 at 4:57




3




3




That is a different question @Sam10.
– mjwills
Nov 12 at 4:57




That is a different question @Sam10.
– mjwills
Nov 12 at 4:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255994%2fwhen-should-i-use-parallel-foreach-and-when-should-i-use-parallel-linq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python