简体   繁体   中英

Unresponsive UI While Waiting

Final edit: Problem turned out to be unrelated to this async implementation. The first answer helped me move stuff around enough to look at the issue with a fresh set of eyes. Thanks, guys.

I'm talking with an IP camera in my app, both through httpclient (to request and receive the image) and a websocket (via websocket-sharp, to receive data).

Right now I'm just opening the websocket and asking for one image (loops will come later). Asking for the image is the last thing I do.

When I define the request for the image as

string picloc = "[redacted]";
Stream imgstream = await client.GetStreamAsync(picloc).ConfigureAwait(false);
Bitmap blah2 = new Bitmap(imgstream);
BMPReadyEventArgs args = new BMPReadyEventArgs();
args.BMP = blah2;
BitmapReady(this, args);

the app runs through all the code and freezes up. If I leave the ConfigureAwait term off, the await will surrender control back to the UI code, it will reach the end of said code, freeze for a couple seconds and then load the image.

With configureawait(false) on there it will load the image and then freeze up. I think that the stream I get from the request starts immediately so if it doesn't have to wait for context (?) it essentially runs synchronously. To be honest, I still don't really understand what configureawait actually does, or what the context people talk about actually means, but this behavior makes me think that the UI freezing has nothing to do with the async nature of the code.

I can't see the debugger jumping to an unexpected place by stepping through with F11 (though this seems to have some shortcomings when used with asynchronous code), it really does seem to just reach the end of the code and freeze up for a couple of seconds.

This leads me to a couple questions.

Will the UI always freeze up when the end of code is reached and, if so, do I need to make some sort of UI refresh ticker?

And, alternatively but more nebulously,

Is there an issue with my async implementation that could be causing this freeze?

Edit: Complete method:

public async void DownloadJPG()
    {
        string picloc = "[redacted]";
        Stream imgstream = await client.GetStreamAsync(picloc).ConfigureAwait(false);
        Bitmap blah2 = new Bitmap(imgstream);
        BMPReadyEventArgs args = new BMPReadyEventArgs();
        args.BMP = blah2;
        BitmapReady(this, args);
    }

called from

private async void HoldingPattern()
    {
        textBox1.Text = wsmanager.passer;
        connector.BitmapReady += (sender, e) =>
            pictureBox1.Image = e.BMP;

        connector.DownloadJPG();
    }

edit2: event handler:

public event EventHandler<BMPReadyEventArgs> BitmapReady; 

BMPReadyEventArgs

class BMPReadyEventArgs:EventArgs
{
    public Bitmap BMP {get;set;}
}

Your approach is way to complicate

public async Task<Image> DownloadJPGAsync()
{
    string picloc = "[redacted]";
    Stream imgstream = await client.GetStreamAsync(picloc).ConfigureAwait(false);
    Bitmap blah2 = new Bitmap(imgstream);
    return blah2;
}

Now from an async event handler

public async void Button1_Click()
{
    var image = await connector.DownloadJPGAsync();
    pictureBox1.Image = image;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM