-3

this line

 success = await _myHid.SendOutputReportViaInterruptTransfer(_deviceData, _hidHandle, outputReportBuffer);

gives me this error Error

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. S:\blissbox\SOURCE_FILES\Bliss-Box_api\managed dx attempt\BBAPI.cs 40

here is my method.

internal async Task<Boolean> SendOutputReportViaInterruptTransfer
            (FileStream fileStreamDeviceData, SafeFileHandle hidHandle, Byte[] outputReportBuffer)
        {
            try
            {
                var success = false;

                    // Begin writing the Output report. 

                    Task t = fileStreamDeviceData.WriteAsync(outputReportBuffer, 0, outputReportBuffer.Length);

                    await t;

                    // Gets to here only if the write operation completed before a timeout.

                    Debug.Print("Asynchronous write completed");

                    // The operation has one of these completion states:

                    switch (t.Status)
                    {
                        case TaskStatus.RanToCompletion:
                            success = true;
                            Debug.Print("Output report written to device");
                            break;
                        case TaskStatus.Canceled:
                            Debug.Print("Task canceled");
                            break;
                        case TaskStatus.Faulted:
                            Debug.Print("Unhandled exception");
                            break;
                    }

                return success;
            }
            catch (Exception ex)
            {
                DisplayException(ModuleName, ex);
                throw;
            }
        }

What did I do wrong?

David Tansey
  • 5,813
  • 4
  • 35
  • 51
sean green
  • 53
  • 6
  • 1
    The compiler is asking you to make the method from which you are calling `SendOutputReportViaInterruptTransfer` `async` as well. – Sergey Kalinichenko Jun 16 '17 at 01:44
  • When you introduce `async` into your project, you need to make the `async`'s go all the way up. In other words, short of encapsulating the call in a task like `Task.Run(async() => { ... });` you need to make all the calling functions `async`, and all those calling functions' calling functions `async`, all the way up the chain. – lebelinoz Jun 16 '17 at 01:50

1 Answers1

0

The problem is the location of the function which is called. It means where you called the following code is not an async function:

success = await _myHid.SendOutputReportViaInterruptTransfer(_deviceData, _hidHandle, outputReportBuffer);

One solution can be call this async function like the following:

var task = TaskEx.RunEx(async () => 
{
   await _myHid.SendOutputReportViaInterruptTransfer(_deviceData, _hidHandle, outputReportBuffer);
});
var result = task.WaitAndUnwrapException();
OmG
  • 18,337
  • 10
  • 57
  • 90