Can I get rid of this horrible blocking code?

2019-08-07 08:21发布

问题:

I need some help, In a web api service call, I need to call a function in a dll that executes asynchronously, and then gives the response in a call back function. Now normally this would be good , but now with a web api the idea is to execute the command and then return the response.

Below is my current code which works, but I think its terrible code, its everything you don't want to do. Especially on a web server when this code is going to run for every request.

[HttpGet]
    public HttpResponseMessage Off(string id)
    {
        APNLink.Link link = LinkProvider.getDeviceLink(id, User.Identity.Name);

        if (link.LinkConnectionStatus == APNLink.ConnectionStatus.Connected)
        {
            link.RelayCommand(APNLink.RelayNumber.Relay1, APNLink.RelayCommand.OFF, test);
            BlockForResponse();
            var msg = Request.CreateResponse(HttpStatusCode.OK);
            return msg;
        }
        else
        {
            if (link.Connect())
            {
                var status = link.LinkConnectionStatus;
                int timeout = 0;
                while (status != APNLink.ConnectionStatus.Connected)
                {
                    Thread.Sleep(500);
                    status = link.LinkConnectionStatus;
                    if (status == APNLink.ConnectionStatus.Connected)
                    {
                        break;
                    }                        
                    if (timeout++ > 16)
                    {
                        var msg1 = Request.CreateResponse(HttpStatusCode.RequestTimeout);
                        return msg1;
                    }
                }
                link.RelayCommand(APNLink.RelayNumber.Relay1, APNLink.RelayCommand.OFF, test);
                BlockForResponse();
                var msg = Request.CreateResponse(HttpStatusCode.OK);
                return msg;
            }
            else
            {
                var msg2 = Request.CreateResponse(HttpStatusCode.BadRequest);
                return msg2;
            }
        }

    }

    bool flag = false;

    public void test(bool var)
    {
    flag = true;
    }

     private static bool BlockForResponse()
    {
        int count = 0;
        while (!flag)
        {
            Thread.Sleep(500);
            if (count > 10)
            {
                //timeout
                return false;
            }
        }
        return true;
    }

now the thing is I have to block when waiting for the dll, Connection to be connected, only then can I execute the command. once I have executed the command. I then have to block again for the response.

Another aspect is, Can I actually block on an asp.net thread? Surely each request is not handled on its own thread?

Is there any way I can make this code neater and most importantly more efficient?

回答1:

Answering the question:

In a web api service call, I need to call a function in a dll that executes asynchronously, and then gives the response in a call back function.

IMO, the best way of doing this is to make your controller method asynchronous and use TaskCompletionSource to wrap the callback of your DLL. Some good additional reads:

  • Using Asynchronous Methods in ASP.NET MVC 4.
  • The Nature of TaskCompletionSource<TResult>.

The code may look something like this:

[HttpGet]
public async Task<HttpResponseMessage> Off(string id)
{
    APNLink.Link link = LinkProvider.getDeviceLink(id, User.Identity.Name);

    if (link.LinkConnectionStatus == APNLink.ConnectionStatus.Connected)
    {
        var tcs = new TaskCompletionSource<object>();

        CallbackType test = delegate {           
           tcs.SetResult(null);
        };

        link.RelayCommand(
            APNLink.RelayNumber.Relay1, 
            APNLink.RelayCommand.OFF,
            test);

        // BlockForResponse();
        await tcs.Task; // non-blocking

        var msg = Request.CreateResponse(HttpStatusCode.OK);
        return msg;
    }

    // ...
}