Jump to content

Just another C# async/await Question

straight_stewie
Go to solution Solved by MisterWhite,

PipelineItem.DoSomething() is an async Task method with an await keyword in it, so yes if you plainly invoke a PipelineItem.DoSomthing it will run asynchronously. I am not familiar with ActionBlock and it's implementation so there might something there and how you use 

testBlock.Complete();
testBlock.Completion.Wait()

Keep in mind that an async method runs code synchronously until it encounters an await keyword so
 

private async Task<PipeLineItem> RunIfNotCancelledAsync(Func<Task<PipelineItem>> function)
  {
    if (!_isCancelled)
      return await function();
    
    else
      return new PipelineItem(true);
  }

runs synchronously if _isCanceled is true.

 

Also try to keep the method naming the same. If it's an async method add Async to ir => DoSomethingAsync() as with RunIfNotCancelledAsync

I'm looking to be able to easily cancel "in progress" items that are currently in a TPL Dataflow pipeline. As such, I make my work as methods in a class which has a bool isCancelled. Therefore:

public class PipelineItem
{
  private bool _isCancelled = false;
  
  public PipelineItem(bool cancelled)
  {
    _isCancelled = cancelled;
  }
  
  private async Task<PipeLineItem> RunIfNotCancelledAsync(Func<Task<PipelineItem>> function)
  {
    if (!_isCancelled)
      return await function();
    
    else
      return new PipelineItem(true);
  }
  
  public async Task<PipelineItem> DoSomething()
  {
    return await RunIfNotCancelledAsync(() => 
    {
      // Do work
      
      return Task.FromResult(this);
    });
  }
  
  public SomeType GetResult()
  {
    return someResult;
  }
}

 

The "work" is inserted into a Dataflow Block like:

ActionBlock<PipelineItem> testBlock = new ActionBlock<PipelineItem>(async workItem => await workItem.DoSomething());

 

And does produce the result I am expecting when called like:

PipelineItem item = new PipelineItem(false);
testBlock.Post(item);

testBlock.Complete();
testBlock.Completion.Wait();

var testResult = item.GetResult();


The majority of my question is:


Does PipelineItem.DoSomething() actually run asynchronously when called like await PipelineItem.DoSomething()?

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

I think if you have multiple PipeLineItem.DoSomething() calls and store them in variables like:

var task1 = await new PipeLineItem(false).DoSomething();
var task2 = await new PipeLineItem(false).DoSomething();

// And then when you evaluate the results, you'll find that the above was run asynchronously
var result1 = task1.Result;
var result2 = task2.Result;

It's been a little while since I've done any C#, so the above syntax may be incorrect or my understanding might be off. Please correct me if I'm wrong

Link to comment
Share on other sites

Link to post
Share on other sites

  • 3 weeks later...

PipelineItem.DoSomething() is an async Task method with an await keyword in it, so yes if you plainly invoke a PipelineItem.DoSomthing it will run asynchronously. I am not familiar with ActionBlock and it's implementation so there might something there and how you use 

testBlock.Complete();
testBlock.Completion.Wait()

Keep in mind that an async method runs code synchronously until it encounters an await keyword so
 

private async Task<PipeLineItem> RunIfNotCancelledAsync(Func<Task<PipelineItem>> function)
  {
    if (!_isCancelled)
      return await function();
    
    else
      return new PipelineItem(true);
  }

runs synchronously if _isCanceled is true.

 

Also try to keep the method naming the same. If it's an async method add Async to ir => DoSomethingAsync() as with RunIfNotCancelledAsync

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×