试图不使用异步 /等待冻结UI
我试图不冻结UI。
我使用了异步 /等待,但是它发生了InvalidOperationException,其他线程拥有此功能。 (我用英语翻译,所以这可能很奇怪)
public async void MimSetHeight(int _id, double _value)
{
int index;
index = Mim.FindIndex(mim => mim.ID == _id);
if(mim.Count > index)
{
mim[index].ShapeHeight = _value;
await Task.Delay(1000);
mim[index].Shape.Height = _value; // <- Exception occurs
}
}
switch(receivePacket.Data[1])
{
case 53:
Application.Current.Dispatcher.Invoke( () =>
mim.MimSetHeight(5, Convert.ToInt32(Encoding.Default.GetString(receivePacket.Data))));
})));
}
我觉得我应该使用dispatcher。
还有其他解决方案吗?
I am trying not to freeze UI.
I used async / await, but it occurs InvalidOperationException, other thread owned this function. (I translated in english so it might be strange sentence)
public async void MimSetHeight(int _id, double _value)
{
int index;
index = Mim.FindIndex(mim => mim.ID == _id);
if(mim.Count > index)
{
mim[index].ShapeHeight = _value;
await Task.Delay(1000);
mim[index].Shape.Height = _value; // <- Exception occurs
}
}
switch(receivePacket.Data[1])
{
case 53:
Application.Current.Dispatcher.Invoke( () =>
mim.MimSetHeight(5, Convert.ToInt32(Encoding.Default.GetString(receivePacket.Data))));
})));
}
I am feeling i should use Dispatcher.Invoke in last sentence.
Are there any other solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论