Sunday 15 September 2013

.net - Create a do-nothing TPL task to complete with a result when triggered -


What is a good way to work with TPL that does not do anything manually, but wait completely To return any other event to the system, with the data of the task to trigger it?

If the triggering event is itself work or IAsyncResult pattern which expects, then support is created. But is the incident different? Dissatisfied solution:

The closest I could have come is to create a task that is executed when a fee is returned by an ineligible object. Here's an example using object 1-element array as an object:

  var work = new work & lt; ReturnType & gt; (Results dialog => ([ReturnType []) Results dialog [0], New ReturnType [1]);   

Somewhere else in the code, when the event occurs, the contents of the container's content Is set and the task begins:

  ((ReturnType [] Task.AsyncState) [0] = theResult; task.Start ();   < P> 

The result is expected; wait for the job;

Left- Nostrilic code odor: to start the job and its entry point After executing the policy, no actual work is done in the work.

Correct nose code: The holder object whose sole purpose is to include a functional return value. / P>

Does anyone care to clean the air?

use Task end source & lt; T & gt; Create the source like this:

  var source = new function end source & lt; ReturnType & gt; (); Work = source. Task; // Stop the wait code   

When the event occurs, set the result like this:

  source.SetResult (theResult);    

No comments:

Post a Comment