相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

What is TaskEx ? In http://www.i-programmer.info/programming/c/1514-async-await-and-the-ui-problem.html?start=1 or await TaskEx.Delay or Await async clarification . 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 like await DoWork(); If you use just Task, await returns nothing and there is no response. If I use TaskEx it says it doesn't exist in context. Should TaskEx be a class or something with some sort of function? Fists one Works it's my mistake.

TaskEx was just an extra class which initially shipped with the CTPs of the async/await extensions for C# 5 before .NET 4.5 shipped... and is now part of the Async Targeting Pack (aka the Microsoft.Bcl.Async NuGet package) in case you want to use async/await but are targeting .NET 4.0 (which doesn't have some of the code required for it).

If you're using .NET 4.5 or later, just use Task.Run, which does the same thing. (You won't be using the targeting pack, so you won't have TaskEx.) The async targeting pack can't add a static method to the existing Task class, hence the need for TaskEx existing at all.

Shouldn't there be a pause when I call await DoWork(); , then it goes Task DoWork() { return Task.Run(() => { for (int i = 0; i < 30; i++) { Thread.Sleep(1000 * 60 * 30); } }); } || for a pause, should the pause be doing Task DoWork() or When it goes back to Await DoWork; ? – Vladimir Laktionov Jan 27, 2016 at 15:41 @VladimirLaktionov: If you're mostly asking about that, it's very unclear from your question - which has TaskEx in the title. It sounds like you've got two separate questions here: 1) What's TaskEx and why can't my compiler find it? 2) Why doesn't Task.Run behave as I expect it to. Those should be in different posts. – Jon Skeet Jan 27, 2016 at 15:43 I apologize it's a weird kind of pause, I put it inside the Loop and it doesn't Cycle again. Mean it does wait. My mistake. – Vladimir Laktionov Jan 27, 2016 at 15:48

"TaskEx is a class that exists only in the CTP, since there was no way to add extra methods to the existing Task class in the Framework (as the CTP is implemented using a separate assembly).

It will not exist in the final, RTM version of the framework. The methods defined on TaskEx will be migrated to Task."

Source: https://social.msdn.microsoft.com/Forums/en-US/74c5ba79-76cc-4f73-a3fa-35616525ab80/what-is-the-difference-between-task-and-taskex-?forum=async

CTP was a community preview version of the .NET compiler platform.

I only went by what was stated and accepted on MSDN. I haven't heard of the async targeting pack, is it a part of the .NET library? – sara Jan 27, 2016 at 15:40

This can also be achieved with the System.Threading.Timer. As written in the API documentation:

If period is zero (0) or Timeout.Infinite and dueTime is not Timeout.Infinite, callback is invoked once […]

Ergo, a single-shot timer can be created as, for instance,

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

There’s also a trick in the API to dispose of the timer from within its callback method: https://stackoverflow.com/a/26410217/3517056

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.

 
推荐文章