How to check possibility of deadlock in c# code












4















My application sometimes stop in the below code, not always but sometimes.



All the 3 methods CalcQuarterlyFigures, CalcWeeklyFigures & CalcMonthlyFigures return Task<List<MyClass>>.



Note, this runs inside a foreach loop.



List<Task> TaskList = new List<Task>();

if(i.DoCalculateAllHistory) {

var quarterly = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
TaskList.Add(quarterly);
var weekly = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
TaskList.Add(weekly);
var monthly = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
TaskList.Add(monthly);

Task.WaitAll(TaskList.ToArray());

if(monthly.Result.Count > 0)
monthlyPerfFig.AddRange(monthly.Result);

if(weekly.Result.Count > 0)
weeklyPerfFig.AddRange(weekly.Result);

if(quarterly.Result.Count > 0)
quartPerfFig.AddRange(quarterly.Result);
} else {

monthlyPerfFig.AddRange(await CalcMonthlyFigures(MonthlyPrice, i.SeriesID));

}


Am I missing anything here that leads to dead lock ?










share|improve this question


















  • 1





    What is the environment? Full .NET Framework or .NET Core? Task.WaitAll(TaskList.ToArray()); will possibly cause a deadlock in full .NET framework. Since you already using await in else condition - use .WhenAll: await Task.WhenAll(TaskList.ToArray());

    – Fabio
    Jan 2 at 5:44











  • @Fabio my application is using .NET Framework 4.6.1.

    – Pரதீப்
    Jan 2 at 5:46











  • @Fabio Thanks, let me update the code and check. Application takes almost 1 full day to complete the full run. So it might take 24 hours to confirm if its working.

    – Pரதீப்
    Jan 2 at 5:47













  • @Fabio - I have a doubt. Is await in else condition is the problem or Task.WaitAll(TaskList.ToArray()) is the problem ?

    – Pரதீப்
    Jan 2 at 5:50











  • If methods CalcQuarterlyFigures, CalcWeeklyFigures and CalcMonthlyFigures are asynchronous then Task.WaitAll is a problem. When you are using asynchronous methods await is correct approach.

    – Fabio
    Jan 2 at 5:53


















4















My application sometimes stop in the below code, not always but sometimes.



All the 3 methods CalcQuarterlyFigures, CalcWeeklyFigures & CalcMonthlyFigures return Task<List<MyClass>>.



Note, this runs inside a foreach loop.



List<Task> TaskList = new List<Task>();

if(i.DoCalculateAllHistory) {

var quarterly = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
TaskList.Add(quarterly);
var weekly = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
TaskList.Add(weekly);
var monthly = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
TaskList.Add(monthly);

Task.WaitAll(TaskList.ToArray());

if(monthly.Result.Count > 0)
monthlyPerfFig.AddRange(monthly.Result);

if(weekly.Result.Count > 0)
weeklyPerfFig.AddRange(weekly.Result);

if(quarterly.Result.Count > 0)
quartPerfFig.AddRange(quarterly.Result);
} else {

monthlyPerfFig.AddRange(await CalcMonthlyFigures(MonthlyPrice, i.SeriesID));

}


Am I missing anything here that leads to dead lock ?










share|improve this question


















  • 1





    What is the environment? Full .NET Framework or .NET Core? Task.WaitAll(TaskList.ToArray()); will possibly cause a deadlock in full .NET framework. Since you already using await in else condition - use .WhenAll: await Task.WhenAll(TaskList.ToArray());

    – Fabio
    Jan 2 at 5:44











  • @Fabio my application is using .NET Framework 4.6.1.

    – Pரதீப்
    Jan 2 at 5:46











  • @Fabio Thanks, let me update the code and check. Application takes almost 1 full day to complete the full run. So it might take 24 hours to confirm if its working.

    – Pரதீப்
    Jan 2 at 5:47













  • @Fabio - I have a doubt. Is await in else condition is the problem or Task.WaitAll(TaskList.ToArray()) is the problem ?

    – Pரதீப்
    Jan 2 at 5:50











  • If methods CalcQuarterlyFigures, CalcWeeklyFigures and CalcMonthlyFigures are asynchronous then Task.WaitAll is a problem. When you are using asynchronous methods await is correct approach.

    – Fabio
    Jan 2 at 5:53
















4












4








4


2






My application sometimes stop in the below code, not always but sometimes.



All the 3 methods CalcQuarterlyFigures, CalcWeeklyFigures & CalcMonthlyFigures return Task<List<MyClass>>.



Note, this runs inside a foreach loop.



List<Task> TaskList = new List<Task>();

if(i.DoCalculateAllHistory) {

var quarterly = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
TaskList.Add(quarterly);
var weekly = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
TaskList.Add(weekly);
var monthly = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
TaskList.Add(monthly);

Task.WaitAll(TaskList.ToArray());

if(monthly.Result.Count > 0)
monthlyPerfFig.AddRange(monthly.Result);

if(weekly.Result.Count > 0)
weeklyPerfFig.AddRange(weekly.Result);

if(quarterly.Result.Count > 0)
quartPerfFig.AddRange(quarterly.Result);
} else {

monthlyPerfFig.AddRange(await CalcMonthlyFigures(MonthlyPrice, i.SeriesID));

}


Am I missing anything here that leads to dead lock ?










share|improve this question














My application sometimes stop in the below code, not always but sometimes.



All the 3 methods CalcQuarterlyFigures, CalcWeeklyFigures & CalcMonthlyFigures return Task<List<MyClass>>.



Note, this runs inside a foreach loop.



List<Task> TaskList = new List<Task>();

if(i.DoCalculateAllHistory) {

var quarterly = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
TaskList.Add(quarterly);
var weekly = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
TaskList.Add(weekly);
var monthly = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
TaskList.Add(monthly);

Task.WaitAll(TaskList.ToArray());

if(monthly.Result.Count > 0)
monthlyPerfFig.AddRange(monthly.Result);

if(weekly.Result.Count > 0)
weeklyPerfFig.AddRange(weekly.Result);

if(quarterly.Result.Count > 0)
quartPerfFig.AddRange(quarterly.Result);
} else {

monthlyPerfFig.AddRange(await CalcMonthlyFigures(MonthlyPrice, i.SeriesID));

}


Am I missing anything here that leads to dead lock ?







c# asynchronous task deadlock






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 4:45









Pரதீப்Pரதீப்

76k1280114




76k1280114








  • 1





    What is the environment? Full .NET Framework or .NET Core? Task.WaitAll(TaskList.ToArray()); will possibly cause a deadlock in full .NET framework. Since you already using await in else condition - use .WhenAll: await Task.WhenAll(TaskList.ToArray());

    – Fabio
    Jan 2 at 5:44











  • @Fabio my application is using .NET Framework 4.6.1.

    – Pரதீப்
    Jan 2 at 5:46











  • @Fabio Thanks, let me update the code and check. Application takes almost 1 full day to complete the full run. So it might take 24 hours to confirm if its working.

    – Pரதீப்
    Jan 2 at 5:47













  • @Fabio - I have a doubt. Is await in else condition is the problem or Task.WaitAll(TaskList.ToArray()) is the problem ?

    – Pரதீப்
    Jan 2 at 5:50











  • If methods CalcQuarterlyFigures, CalcWeeklyFigures and CalcMonthlyFigures are asynchronous then Task.WaitAll is a problem. When you are using asynchronous methods await is correct approach.

    – Fabio
    Jan 2 at 5:53
















  • 1





    What is the environment? Full .NET Framework or .NET Core? Task.WaitAll(TaskList.ToArray()); will possibly cause a deadlock in full .NET framework. Since you already using await in else condition - use .WhenAll: await Task.WhenAll(TaskList.ToArray());

    – Fabio
    Jan 2 at 5:44











  • @Fabio my application is using .NET Framework 4.6.1.

    – Pரதீப்
    Jan 2 at 5:46











  • @Fabio Thanks, let me update the code and check. Application takes almost 1 full day to complete the full run. So it might take 24 hours to confirm if its working.

    – Pரதீப்
    Jan 2 at 5:47













  • @Fabio - I have a doubt. Is await in else condition is the problem or Task.WaitAll(TaskList.ToArray()) is the problem ?

    – Pரதீப்
    Jan 2 at 5:50











  • If methods CalcQuarterlyFigures, CalcWeeklyFigures and CalcMonthlyFigures are asynchronous then Task.WaitAll is a problem. When you are using asynchronous methods await is correct approach.

    – Fabio
    Jan 2 at 5:53










1




1





What is the environment? Full .NET Framework or .NET Core? Task.WaitAll(TaskList.ToArray()); will possibly cause a deadlock in full .NET framework. Since you already using await in else condition - use .WhenAll: await Task.WhenAll(TaskList.ToArray());

– Fabio
Jan 2 at 5:44





What is the environment? Full .NET Framework or .NET Core? Task.WaitAll(TaskList.ToArray()); will possibly cause a deadlock in full .NET framework. Since you already using await in else condition - use .WhenAll: await Task.WhenAll(TaskList.ToArray());

– Fabio
Jan 2 at 5:44













@Fabio my application is using .NET Framework 4.6.1.

– Pரதீப்
Jan 2 at 5:46





@Fabio my application is using .NET Framework 4.6.1.

– Pரதீப்
Jan 2 at 5:46













@Fabio Thanks, let me update the code and check. Application takes almost 1 full day to complete the full run. So it might take 24 hours to confirm if its working.

– Pரதீப்
Jan 2 at 5:47







@Fabio Thanks, let me update the code and check. Application takes almost 1 full day to complete the full run. So it might take 24 hours to confirm if its working.

– Pரதீப்
Jan 2 at 5:47















@Fabio - I have a doubt. Is await in else condition is the problem or Task.WaitAll(TaskList.ToArray()) is the problem ?

– Pரதீப்
Jan 2 at 5:50





@Fabio - I have a doubt. Is await in else condition is the problem or Task.WaitAll(TaskList.ToArray()) is the problem ?

– Pரதீப்
Jan 2 at 5:50













If methods CalcQuarterlyFigures, CalcWeeklyFigures and CalcMonthlyFigures are asynchronous then Task.WaitAll is a problem. When you are using asynchronous methods await is correct approach.

– Fabio
Jan 2 at 5:53







If methods CalcQuarterlyFigures, CalcWeeklyFigures and CalcMonthlyFigures are asynchronous then Task.WaitAll is a problem. When you are using asynchronous methods await is correct approach.

– Fabio
Jan 2 at 5:53














1 Answer
1






active

oldest

votes


















7














In provided context (sample code of .NET 4.6.1) Task.WaitAll(TaskList.ToArray()) will cause a deadlock.

Definitely useful source: Don't Block on Async Code



You should make you code block fully asynchronous



if (i.DoCalculateAllHistory) 
{
var quarterlyTask = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
var weeklyTask = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
var monthlyTask = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);

// Task.WhenAll accepts "params" array
await Task.WhenAll(quarterlyTask, weeklyTask, monthlyTask);

// You don't need to check for .Count
// nothing will be added when empty list given to .AddRange
quartPerfFig.AddRange(await quarterlyTask);
weeklyPerfFig.AddRange(await weeklyTask);
monthlyPerfFig.AddRange(await monthlyTask);
}
else
{
var monthly = await CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
monthlyPerfFig.AddRange(monthly);
}





share|improve this answer


























  • Thanks mate, instead of quarterly.Result can we use await quarterly ? similarly for weekly and monthly results ? like this answer uses await instead of Result

    – Pரதீப்
    Jan 2 at 6:11













  • You can use await.

    – Fabio
    Jan 2 at 6:26











  • Thanks for your patience and help. I'll update you if the full run is completed successfully.

    – Pரதீப்
    Jan 2 at 6:30








  • 1





    @Pரதீப் .Result vs. await after .WhenAll is tricky... If you use .Result you show that you understand how all that works but on other hand people who don't will disagree and complain till you put await back... Overall await is safe if you are concerned that someone will change code by removing .WhenAll as not necessary.

    – Alexei Levenkov
    Jan 2 at 6:56













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',
autoActivateHeartbeat: false,
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%2f54001297%2fhow-to-check-possibility-of-deadlock-in-c-sharp-code%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









7














In provided context (sample code of .NET 4.6.1) Task.WaitAll(TaskList.ToArray()) will cause a deadlock.

Definitely useful source: Don't Block on Async Code



You should make you code block fully asynchronous



if (i.DoCalculateAllHistory) 
{
var quarterlyTask = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
var weeklyTask = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
var monthlyTask = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);

// Task.WhenAll accepts "params" array
await Task.WhenAll(quarterlyTask, weeklyTask, monthlyTask);

// You don't need to check for .Count
// nothing will be added when empty list given to .AddRange
quartPerfFig.AddRange(await quarterlyTask);
weeklyPerfFig.AddRange(await weeklyTask);
monthlyPerfFig.AddRange(await monthlyTask);
}
else
{
var monthly = await CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
monthlyPerfFig.AddRange(monthly);
}





share|improve this answer


























  • Thanks mate, instead of quarterly.Result can we use await quarterly ? similarly for weekly and monthly results ? like this answer uses await instead of Result

    – Pரதீப்
    Jan 2 at 6:11













  • You can use await.

    – Fabio
    Jan 2 at 6:26











  • Thanks for your patience and help. I'll update you if the full run is completed successfully.

    – Pரதீப்
    Jan 2 at 6:30








  • 1





    @Pரதீப் .Result vs. await after .WhenAll is tricky... If you use .Result you show that you understand how all that works but on other hand people who don't will disagree and complain till you put await back... Overall await is safe if you are concerned that someone will change code by removing .WhenAll as not necessary.

    – Alexei Levenkov
    Jan 2 at 6:56


















7














In provided context (sample code of .NET 4.6.1) Task.WaitAll(TaskList.ToArray()) will cause a deadlock.

Definitely useful source: Don't Block on Async Code



You should make you code block fully asynchronous



if (i.DoCalculateAllHistory) 
{
var quarterlyTask = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
var weeklyTask = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
var monthlyTask = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);

// Task.WhenAll accepts "params" array
await Task.WhenAll(quarterlyTask, weeklyTask, monthlyTask);

// You don't need to check for .Count
// nothing will be added when empty list given to .AddRange
quartPerfFig.AddRange(await quarterlyTask);
weeklyPerfFig.AddRange(await weeklyTask);
monthlyPerfFig.AddRange(await monthlyTask);
}
else
{
var monthly = await CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
monthlyPerfFig.AddRange(monthly);
}





share|improve this answer


























  • Thanks mate, instead of quarterly.Result can we use await quarterly ? similarly for weekly and monthly results ? like this answer uses await instead of Result

    – Pரதீப்
    Jan 2 at 6:11













  • You can use await.

    – Fabio
    Jan 2 at 6:26











  • Thanks for your patience and help. I'll update you if the full run is completed successfully.

    – Pரதீப்
    Jan 2 at 6:30








  • 1





    @Pரதீப் .Result vs. await after .WhenAll is tricky... If you use .Result you show that you understand how all that works but on other hand people who don't will disagree and complain till you put await back... Overall await is safe if you are concerned that someone will change code by removing .WhenAll as not necessary.

    – Alexei Levenkov
    Jan 2 at 6:56
















7












7








7







In provided context (sample code of .NET 4.6.1) Task.WaitAll(TaskList.ToArray()) will cause a deadlock.

Definitely useful source: Don't Block on Async Code



You should make you code block fully asynchronous



if (i.DoCalculateAllHistory) 
{
var quarterlyTask = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
var weeklyTask = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
var monthlyTask = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);

// Task.WhenAll accepts "params" array
await Task.WhenAll(quarterlyTask, weeklyTask, monthlyTask);

// You don't need to check for .Count
// nothing will be added when empty list given to .AddRange
quartPerfFig.AddRange(await quarterlyTask);
weeklyPerfFig.AddRange(await weeklyTask);
monthlyPerfFig.AddRange(await monthlyTask);
}
else
{
var monthly = await CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
monthlyPerfFig.AddRange(monthly);
}





share|improve this answer















In provided context (sample code of .NET 4.6.1) Task.WaitAll(TaskList.ToArray()) will cause a deadlock.

Definitely useful source: Don't Block on Async Code



You should make you code block fully asynchronous



if (i.DoCalculateAllHistory) 
{
var quarterlyTask = CalcQuarterlyFigures(QuarterlyPrices, i.SeriesID);
var weeklyTask = CalcWeeklyFigures(WeeklyPrices, i.SeriesID);
var monthlyTask = CalcMonthlyFigures(MonthlyPrice, i.SeriesID);

// Task.WhenAll accepts "params" array
await Task.WhenAll(quarterlyTask, weeklyTask, monthlyTask);

// You don't need to check for .Count
// nothing will be added when empty list given to .AddRange
quartPerfFig.AddRange(await quarterlyTask);
weeklyPerfFig.AddRange(await weeklyTask);
monthlyPerfFig.AddRange(await monthlyTask);
}
else
{
var monthly = await CalcMonthlyFigures(MonthlyPrice, i.SeriesID);
monthlyPerfFig.AddRange(monthly);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 6:26

























answered Jan 2 at 6:08









FabioFabio

20.1k22048




20.1k22048













  • Thanks mate, instead of quarterly.Result can we use await quarterly ? similarly for weekly and monthly results ? like this answer uses await instead of Result

    – Pரதீப்
    Jan 2 at 6:11













  • You can use await.

    – Fabio
    Jan 2 at 6:26











  • Thanks for your patience and help. I'll update you if the full run is completed successfully.

    – Pரதீப்
    Jan 2 at 6:30








  • 1





    @Pரதீப் .Result vs. await after .WhenAll is tricky... If you use .Result you show that you understand how all that works but on other hand people who don't will disagree and complain till you put await back... Overall await is safe if you are concerned that someone will change code by removing .WhenAll as not necessary.

    – Alexei Levenkov
    Jan 2 at 6:56





















  • Thanks mate, instead of quarterly.Result can we use await quarterly ? similarly for weekly and monthly results ? like this answer uses await instead of Result

    – Pரதீப்
    Jan 2 at 6:11













  • You can use await.

    – Fabio
    Jan 2 at 6:26











  • Thanks for your patience and help. I'll update you if the full run is completed successfully.

    – Pரதீப்
    Jan 2 at 6:30








  • 1





    @Pரதீப் .Result vs. await after .WhenAll is tricky... If you use .Result you show that you understand how all that works but on other hand people who don't will disagree and complain till you put await back... Overall await is safe if you are concerned that someone will change code by removing .WhenAll as not necessary.

    – Alexei Levenkov
    Jan 2 at 6:56



















Thanks mate, instead of quarterly.Result can we use await quarterly ? similarly for weekly and monthly results ? like this answer uses await instead of Result

– Pரதீப்
Jan 2 at 6:11







Thanks mate, instead of quarterly.Result can we use await quarterly ? similarly for weekly and monthly results ? like this answer uses await instead of Result

– Pரதீப்
Jan 2 at 6:11















You can use await.

– Fabio
Jan 2 at 6:26





You can use await.

– Fabio
Jan 2 at 6:26













Thanks for your patience and help. I'll update you if the full run is completed successfully.

– Pரதீப்
Jan 2 at 6:30







Thanks for your patience and help. I'll update you if the full run is completed successfully.

– Pரதீப்
Jan 2 at 6:30






1




1





@Pரதீப் .Result vs. await after .WhenAll is tricky... If you use .Result you show that you understand how all that works but on other hand people who don't will disagree and complain till you put await back... Overall await is safe if you are concerned that someone will change code by removing .WhenAll as not necessary.

– Alexei Levenkov
Jan 2 at 6:56







@Pரதீப் .Result vs. await after .WhenAll is tricky... If you use .Result you show that you understand how all that works but on other hand people who don't will disagree and complain till you put await back... Overall await is safe if you are concerned that someone will change code by removing .WhenAll as not necessary.

– Alexei Levenkov
Jan 2 at 6:56






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001297%2fhow-to-check-possibility-of-deadlock-in-c-sharp-code%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith