为什么以这种方式使用 System.Threading.Task 类时我的 UI 被阻止?

发布于 2024-11-06 18:20:24 字数 439 浏览 0 评论 0原文

在我的 ViewModel 中,我有以下代码:

Logs = new ObservableCollection<Log>();
Logs = Task.Factory.StartNew(() => mainModel.GetLogs()).Result;

Log 是一个非常简单的类,具有几个公共属性。

根据我对 Task 类的理解,以这种方式调用的 mainModel 函数 GetLogs() 应该在单独的线程上运行,当它从数据库中获取记录时,我的 UI 应该做出响应,但这不是正在发生的情况,而是在记录被读取时从数据存储中获取我的 UI 被阻止。

我希望有人能解释为什么......TIA。

编辑:我对 Task 类的理解不完整,使用 Task 类的ContinueWith 方法将确保异步执行,如下面成员回复中所述...

In my ViewModel I have this code:

Logs = new ObservableCollection<Log>();
Logs = Task.Factory.StartNew(() => mainModel.GetLogs()).Result;

wtih Log being a very simple class with a couple of public properties.

According to my understanding of Task class the mainModel function GetLogs() invoked this way should run on a separate thread and while it is fetching records from the database my UI should be responsive, however that is not what is happening, instead while records are being fetched from the data store my UI is blocked.

I was hoping someone could explain why... TIA.

EDIT: My understading of the Task class was incomplete, using ContinueWith method of the Task class will ensure the async execution as explained below in member replies...

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

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

发布评论

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

评论(3

薄荷港 2024-11-13 18:20:24

这是因为您在开始异步操作后立即调用 ResultResult 属性的 getter 会阻塞当前线程的执行,直到任务完成。

更新:

为了异步获取结果,您需要调用ContinueWith并指定任务完成时将调用的函数:

Task.Factory.StartNew(() => mainModel.GetLogs()).ContinueWith(t => Logs = t.Result);

This is because you call Result right after you started the asynchronous operation. The getter of the Result property blocks the execution of current thread until the task is completed.

Update:

In order to get the result asynchronously, you need to call ContinueWith and specify a function that will be called when the task is completed:

Task.Factory.StartNew(() => mainModel.GetLogs()).ContinueWith(t => Logs = t.Result);
ぽ尐不点ル 2024-11-13 18:20:24

我认为您不应该像这样使用 Result 属性,来自 MSDN

此属性的 get 访问器可确保异步操作在返回之前完成。一旦计算结果可用,它就会被存储,并在以后调用 Result 时立即返回。

如果您在任务运行时访问当前线程,它会使当前线程等待。

You should not use the Result property like this i think, from MSDN:

The get accessor for this property ensures that the asynchronous operation is complete before returning. Once the result of the computation is available, it is stored and will be returned immediately on later calls to Result.

It makes the current thread wait if you access it while the task is running.

再见回来 2024-11-13 18:20:24

假设 mainModel.GetLogs 是线程安全的,您可能需要这样的东西,它在后台线程上调用 GetLogs,然后仅在完成获取日志时将 Logs 设置为结果。 TaskScheduler.FromCurrentSynchronizationContext() 确保该部分的执行在 UI 线程上运行,如果您的 UI 绑定到该集合,则需要这样做。

Task doStuff = Task.Factory.StartNew<ICollection>(() =>
    {
        return mainModel.GetLogs();
    })
    .ContinueWith((result) =>
    {
        Logs = result;
    }, TaskScheduler.FromCurrentSynchronizationContext());

Assuming mainModel.GetLogs is threadsafe you probably want something like this, which calls the GetLogs on a background thread, and then only sets Logs to the result when it finished gettings the logs. The TaskScheduler.FromCurrentSynchronizationContext() ensures that execution of that part is run on the UI thread, which will be required if your UI is bound to that collection.

Task doStuff = Task.Factory.StartNew<ICollection>(() =>
    {
        return mainModel.GetLogs();
    })
    .ContinueWith((result) =>
    {
        Logs = result;
    }, TaskScheduler.FromCurrentSynchronizationContext());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文