等待时无响应 UI
Unresponsive UI While Waiting
最终编辑:问题原来与此异步实现无关。第一个答案帮助我移动了足够多的东西,以全新的眼光看待这个问题。谢谢大家。
我正在通过 httpclient(请求和接收图像)和 websocket(通过 websocket-sharp,接收数据)与我的应用程序中的 IP 摄像机通话。
现在我只是打开 websocket 并请求一张图片(稍后会出现循环)。索取图片是我做的最后一件事。
当我将图片请求定义为
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);
该应用程序运行所有代码并冻结。如果我关闭 ConfigureAwait 项,await 会将控制交还给 UI 代码,它将到达所述代码的末尾,冻结几秒钟,然后加载图像。
在那里使用 configureawait(false) 它将加载图像,然后冻结。我认为我从请求中获得的流会立即开始,因此如果它不必等待上下文(?),它实际上是同步运行的。老实说,我仍然不太明白 configureawait 实际上做了什么,或者人们谈论的上下文实际上意味着什么,但这种行为让我认为 UI 冻结与代码。
我看不到调试器通过 F11 单步执行跳转到了意想不到的地方(虽然这在与异步代码一起使用时似乎有一些缺点),它确实似乎只是到达了代码的末尾并且冻结几秒钟。
这让我想到了几个问题。
到达代码末尾时 UI 是否会始终冻结?如果是,我是否需要进行某种 UI 刷新代码?
并且,或者更模糊地,
我的异步实现是否存在可能导致此冻结的问题?
编辑:完成方法:
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);
}
来自
private async void HoldingPattern()
{
textBox1.Text = wsmanager.passer;
connector.BitmapReady += (sender, e) =>
pictureBox1.Image = e.BMP;
connector.DownloadJPG();
}
edit2:事件处理程序:
public event EventHandler<BMPReadyEventArgs> BitmapReady;
BMPReadyEventArgs
class BMPReadyEventArgs:EventArgs
{
public Bitmap BMP {get;set;}
}
你的方法很复杂
public async Task<Image> DownloadJPGAsync()
{
string picloc = "[redacted]";
Stream imgstream = await client.GetStreamAsync(picloc).ConfigureAwait(false);
Bitmap blah2 = new Bitmap(imgstream);
return blah2;
}
现在来自异步事件处理程序
public async void Button1_Click()
{
var image = await connector.DownloadJPGAsync();
pictureBox1.Image = image;
}
最终编辑:问题原来与此异步实现无关。第一个答案帮助我移动了足够多的东西,以全新的眼光看待这个问题。谢谢大家。
我正在通过 httpclient(请求和接收图像)和 websocket(通过 websocket-sharp,接收数据)与我的应用程序中的 IP 摄像机通话。
现在我只是打开 websocket 并请求一张图片(稍后会出现循环)。索取图片是我做的最后一件事。
当我将图片请求定义为
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);
该应用程序运行所有代码并冻结。如果我关闭 ConfigureAwait 项,await 会将控制交还给 UI 代码,它将到达所述代码的末尾,冻结几秒钟,然后加载图像。
在那里使用 configureawait(false) 它将加载图像,然后冻结。我认为我从请求中获得的流会立即开始,因此如果它不必等待上下文(?),它实际上是同步运行的。老实说,我仍然不太明白 configureawait 实际上做了什么,或者人们谈论的上下文实际上意味着什么,但这种行为让我认为 UI 冻结与代码。
我看不到调试器通过 F11 单步执行跳转到了意想不到的地方(虽然这在与异步代码一起使用时似乎有一些缺点),它确实似乎只是到达了代码的末尾并且冻结几秒钟。
这让我想到了几个问题。
到达代码末尾时 UI 是否会始终冻结?如果是,我是否需要进行某种 UI 刷新代码?
并且,或者更模糊地,
我的异步实现是否存在可能导致此冻结的问题?
编辑:完成方法:
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);
}
来自
private async void HoldingPattern()
{
textBox1.Text = wsmanager.passer;
connector.BitmapReady += (sender, e) =>
pictureBox1.Image = e.BMP;
connector.DownloadJPG();
}
edit2:事件处理程序:
public event EventHandler<BMPReadyEventArgs> BitmapReady;
BMPReadyEventArgs
class BMPReadyEventArgs:EventArgs
{
public Bitmap BMP {get;set;}
}
你的方法很复杂
public async Task<Image> DownloadJPGAsync()
{
string picloc = "[redacted]";
Stream imgstream = await client.GetStreamAsync(picloc).ConfigureAwait(false);
Bitmap blah2 = new Bitmap(imgstream);
return blah2;
}
现在来自异步事件处理程序
public async void Button1_Click()
{
var image = await connector.DownloadJPGAsync();
pictureBox1.Image = image;
}