Waiting for async TaskEx

What is TaskEx? At http://www.i-programmer.info/programming/c/1514-async-await-and-the-ui-problem.html?start=1 either expect TaskEx.Delay or wait for an asynchronous update . I use

Task DoWork()
{
    return Task.Run(() =>
    {
        for (int i = 0; i < 30; i++)
        {
            Thread.Sleep(1000 * 60 * 30);
        }
    });
}

Examples use this

Task DoWork()
{
   return TaskEx.Run(() =>
   {
     for (int i = 0; i < 10; i++)
     {
         Thread.Sleep(500);
     }
   }
 });

I call it as await DoWork(); if you use only Task, awaitreturns nothing and there is no answer. If I use TaskEx, he says that does not exist in context. Should it TaskExbe a class or something like a function? Fists working alone is my fault.

+11
source share
3 answers

TaskEx , CTP-/ # 5 .NET 4.5... Async Targeting Pack ( Microsoft.Bcl.Async NuGet), async/wait, .NET 4.0 ( ).

.NET 4.5 , Task.Run, . ( , TaskEx.) Task, , TaskEx, .

+26

System.Threading.Timer. API:

If periodis zero (0) or Timeout.Infinite and dueTimeis not Timeout.Infinite, callbackis invoked once [...]

Ergo, a single shot timer can be created such as

Timer oneShotTimer = new Timer((object stateInfo) => {
  // your callback code here
}, null, yourOneShotTimePeriodHere, Timeout.Infinite);

The API also has a trick to get rid of a timer from its callback method: fooobar.com/questions/1342785 / ...

0
source

Source: https://habr.com/ru/post/1626207/


All Articles