简体   繁体   English

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

[英]Switch to background thread and continue method execution

I have a deserialization method (XML) that functions well until it gets to X number of objects. 我有一个反序列化方法(XML),该方法可以很好地起作用,直到达到X个对象为止。 Below X , the time is takes to deserialize is acceptable and the UI being blocked is not a problem. 低于X ,反序列化所花费的时间是可以接受的,并且UI被阻塞不是问题。 However, if the number of objects is greater than X (relatively unlikely, but possible), then the load time is too long. 但是,如果对象数大于X (相对不太可能,但可能),则加载时间太长。 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? 一旦反序列化(或任何其他方法)开始,是否可以从UI线程跳到后台线程,以便在反序列化花费的时间太长时可以解锁UI线程? thanks 谢谢

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. 如果对象数小于X,则不会有问题;如果对象数大于X,则不会阻塞UI。

You can do it in a background worker, and use the result in the RunWorkerCompleted event (which is raised on the UI thread) 您可以在后台工作程序中执行此操作,并将结果用于RunWorkerCompleted事件(在UI线程上引发)

No, once you have requested deserialization from a BCL facility, you cannot switch the thread type. 否,一旦从BCL设施请求反序列化,就无法切换线程类型。 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 (eg threadpool) do this task in all cases (on account of the big cases), rather than after you cross boundary X . 也许,如果您编写了自己的自定义反序列化器,则可以执行此操作,但是,在所有情况下(由于较大的情况),简单地使用单独的线程(例如,线程池)来执行此任务会比起交叉操作要有效得多。边界X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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