切换到后台线程并继续执行方法

发布于 2024-09-24 23:44:52 字数 221 浏览 1 评论 0原文

我有一个反序列化方法 (XML),该方法运行良好,直到达到 X 个对象为止。在 X 下面,反序列化所需的时间是可以接受的,并且 UI 被阻塞不是问题。但是,如果对象的数量大于X(相对不太可能,但有可能),则加载时间太长。一旦反序列化(或任何其他方法)开始,是否可以从 UI 线程跳转到后台线程,以便在反序列化花费太长时间时我可以解锁 UI 线程?谢谢

I have a deserialization method (XML) that functions well until it gets to X number of objects. Below X, the time is takes to deserialize is acceptable and the UI being blocked is not a problem. However, if the number of objects is greater than X (relatively unlikely, but possible), then the load time is too long. Is it possible to jump from the UI thread to a background thread once deserialization (or any other method) has started so that I can unlock the UI thread if the deserialization is taking too long? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巡山小妖精 2024-10-01 23:44:52

在所有情况下,只需在后台线程中进行反序列化即可。如果对象数量低于 X,则不会出现问题;如果对象数量大于 X,则不会阻塞 UI。

您可以在后台工作程序中执行此操作,并在 RunWorkerCompleted 中使用结果 事件(在 UI 线程上引发)

Just do the deserialization in a background thread in all cases. It won't be an issue if the number of objects is below X, and it won't block the UI if it is greater than X.

You can do it in a background worker, and use the result in the RunWorkerCompleted event (which is raised on the UI thread)

美煞众生 2024-10-01 23:44:52

不可以,一旦您从 BCL 设施请求反序列化,您就无法切换线程类型。也许如果您编写自己的自定义反序列化器,您可以这样做,但是简单地让一个单独的线程(例如线程池)在所有情况下执行此任务会更有效(考虑到大情况) ),而不是在跨越边界 X 之后。

No, once you have requested deserialization from a BCL facility, you cannot switch the thread type. Perhaps if you wrote your own custom deserializer you could do this, but it would be far more effective to simply have a seperate thread (e.g. threadpool) do this task in all cases (on account of the big cases), rather than after you cross boundary X.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文