试图不使用异步 /等待冻结UI

发布于 2025-01-29 14:32:47 字数 692 浏览 2 评论 0原文

我试图不冻结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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文