0

is this a better way to call a async method in a synchronised method.

public bool Sync() 
{ 
  return Task.Run(() => MyClass.IsSyncDone()).Result; 
}

will it cause deadlocks? I really need to know a better way. because I do not want to make async to system calls i.e. App launching, registered methods etc. can anyone please help.

Priti
  • 336
  • 1
  • 3
  • 15

1 Answers1

2

If you didn't need the responses then you could easily do something like this:

public bool Sync() 
{ 
    Task.Run(async () => await MyClass.DoSync());
}

That you need the response will mean more complicated code. Without seeing the full code it's hard to provide specific examples but it's generally better to not tie such long running, complicated asynchronous operations to synchronous ones.
Either pass a continuation to the async method to create via a task or have the async method update a bound property of a viewmodel when finished or fire a custom event.

It is important not to perform any potentially long running async operations in the application livecycle event handlers as if these take to long it can lead to the application being forcefully terminated or the app data being left in an inconsistent state.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • At some places its showing warning that await operator required. Can you please suggest something. – Priti Nov 11 '13 at 12:22
  • @pri I suggest adding the await operators where they're required. Without seeing the code there's little else to suggest. – Matt Lacey Nov 11 '13 at 20:21