3
I have a system that is a Web Api, the control runs a classes like this:
public class CrawlBO
{
public void IniciaProcesso()
{
...
CarregaConfiguracaoCrawlBO carregaconfiguracaocrawlbo = new CarregaConfiguracaoCrawlBO(type);
Task<ConfiguracaoCrawlModel> montarconfiguracao = carregaconfiguracaocrawlbo.MontarConfiguracaoAsync(type);
...
montarconfiguracao.Wait();
conf = montarconfiguracao.Result;
...
}
}
My class CarregaConfiguracaoCrawlBO has a method async, that’s the class:
public class CarregaConfiguracaoCrawlBO
{
public async Task<ConfiguracaoCrawlModel> MontarConfiguracaoAsync(EnumOrigens type)
{
try
{
MYEntities db = new MYEntities ();
int t = (int)type;
CRAWL conf = await db.CRAWLs.Where(p => p.ID_ORIGEM == t).FirstOrDefaultAsync();
...
return c;
}
catch (Exception ex)
{
throw ex;
}
}
}
The problem is that it freezes in the montarconfiguracao.Wait();. I did all kinds of tests, and I know inside the Task it freezes on the line of .FirstOrDefaultAsync();.
At first I thought it might be some mistake, so I put the try catch, but it does not take error, then I thought it could be problem in the localhost, but on the server it hangs in the same place too.
I’d like to use the .Wait() because I am not able to put all the methods as async at once.
Adding More Content:
I know he stops at .Wait() to wait for the task, but the problem that the task should take about 4 seconds, and it really freezes, an hour later and is still there in the task without doing anything
I think I didn’t write well, the problem is that it freezes even! If you run the
.FirstOrDefaultwithout theasyncit runs in 3 seconds, but with theasynche freezes for more than an hour and I’ve never seen him leave theWait(). I’ve improved the question.– Ricardo
You in the method
MontarConfiguracaoAsync()is returning to variablecwas error when posting the code or is it even so? The method should returnconf.– ramaral
@user3517631 This is a deadlock, see my answer for more details.
– dcastro
@dcastro Ask me a question: the first code I know does not cause deadlock, for use async in the method and whether or not the latter causes?
– ramaral
@ramaral Good question - I edited my answer to answer that question too (did not fit in a comment)
– dcastro