0

I have a 3rd party DLL that I cannot change and I wanted to make asynchronous one of its method.
So I have:

procedure bool Food()
{
   Task<ReturnClass> response = SomeDLL.SyncMethod(); // Method returns "ReturnClass"

   response.ContinueWith (_ => 
   {
     return response.Result != null;
   });
 }

I get the known compile error:
Cannot implicitly convert type 'SomeDLL.ReturnClass' to 'System.Threading.Tasks.Task'

It's important to notice that I cannot change the 3rd PartyDLL

  1. What's the right way to accomplish this goal?
    I need to wait for the SomeDLL SyncMethod return so that's why I used ContinueWith.

  2. Does the procedure bool Foo need to set as "async"?

8
  • 1
    You're trying to create a synchronous wrapper for an asynchronous method. Here is a great article about it. The answer is that in virtually all cases just don't do this. It's generally a sign that something is wrong, and attempting to do this can cause all sorts of problems if you're not very careful. Commented Nov 15, 2013 at 18:00
  • @Servy I understand the OP is trying to do the opposite: build an async wrapper for a sync method. Commented Nov 15, 2013 at 18:03
  • @ken2k You also shouldn't be doing that, but no, that's not the case. This method returns a bool, not a Task<bool>, and SomeDLL.Method() clearly returns a Task, based on the first line of the method. This is very much a sync over async wrapper. Commented Nov 15, 2013 at 18:04
  • SomeDLL.Method returns "ReturnClass" not a Task. Commented Nov 15, 2013 at 18:06
  • 1
    @Servy From the question, I understand SomeDLL.Method() is sync (I wanted to make asynchronous one of its method) and the first line of the method generates a compile-time error the OP is trying to fix. Commented Nov 15, 2013 at 18:06

1 Answer 1

2

You say you want to make the method async, but you don't say why. If you're looking to gain the scalability improvements of async, then that's not possible: SyncMethod() will block a thread and there's nothing you can do about that.

If you're in a UI application and you want gain better responsiveness, then you can do that by invoking your method on a background thread using Task.Run(). Something like:

async Task<bool> FoodAsync()
{
   Task<ReturnClass> response = Task.Run(() => SomeDLL.SyncMethod());

   return await response != null;
}

This assumes you're using C# 5. If that's not the case, you can still do this, but the code is going to be more complicated:

Task<bool> FoodAsync()
{
   Task<ReturnClass> response =
       Task.Factory.StartNew(() => SomeDLL.SyncMethod());

   return response.ContinueWith(_ => response.Result != null);
}
Sign up to request clarification or add additional context in comments.

2 Comments

The DLL is a Twilio DLL that will send a Text message via their APIs. So my thought was that the main thread will get released and another will be used for handling the Twilio API. Isn't that how you achieve further scalability?
@EagertoLearn If you're talking about ASP.NET application (or something like that), then no, that won't help you at all. In ASP.NET, there is no “main thread” and you get better scalability by using smaller number of threads. So just switching from one thread to another is useless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.