Search code examples
asynchronouswaitnavision

Call function "ReadAsMultipartAsync" synchronously


I'm using Navision to call ReadAsMultipartAsync().Result in a C# library, but this blocks Navision. If I change the library with await and sync, I can't manage a Task result from Navision, so I would like to call the "ReadAsMultipartAsync" function but in a synchronous way.

If I do something like this: streamCont.ReadAsMultipartAsync(provider).Wait(TimeSpan.FromSeconds(20)); works, but I spent 20 seconds on this case.

There are any way of wait the task of ReadAsMultipartAsync without block Navision?. Thanks


Solution

  • I have found a way to do it that works. It's funny but this blocks Nav:

    Task<MultipartMemoryStreamProvider> task = streamContent.ReadAsMultipartAsync();
    task.Wait();
    

    And these two work fine:

    Task thread1 = Task.Factory.StartNew(() => result = streamContent.ReadAsMultipartAsync().Result);
    Task.WaitAll(thread1);
    
    Task tarea1 = new Task(() => result = streamContent.ReadAsMultipartAsync().Result);
    tarea1.Start();
    tarea1.Wait();
    

    I don't know what the difference is between the first option and the others, or if it can give an error in some cases, but for now it seems to work.