Search code examples
c#comasync-await

Running a C# async method via COM from single threaded programming language


We have a system running in a single threaded language (DataFlex), and make calls to C# code via COM. One such call takes multiple seconds to finish, and it would be nice to be able to let the C# method finish in the background without blocking the UI.

My first attempt, though, did not finish in the background - AsyncDbLoad seemingly had to complete its work before returning control to DataFlex.

  private async Task DbLoad()
  {
     LongRunningMethod();
  }

  public async Task AsyncDbLoad()
  {
     await DbLoad();
  }

What am I doing wrong? Is it even possible to do this using async-await? It not, what should I do?


Solution

  • I'm not familiar with this version of DataFlex but I don't think using async await will help you. I would split it into 2 requests:

    1: int StartDbLoad(): initiates the operation, returns an operation id.

    2: Result CheckResult(int operationId): checks if a result is available and retrieves it.

    On the C# side you can use a Task to process the request in a separate thread and keep the result in a Dictionary<int, Result>. Then you have to use any features available in DataFlex to poll for the result while keeping the UI responsive, such as a Timer.