加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Task.Continuewith(在当前线程上)

发布时间:2020-12-16 02:01:44 所属栏目:百科 来源:网络整理
导读:我有一个方法,使用以下代码: object frm = null;// shows the overlay loading maskCore.ShowLoadingMask("Please wait...");// start taskTask.Factory.StartNew(() = { // go to server and get the data var employee = new Data.Entities.Employee(empl
我有一个方法,使用以下代码:

object frm = null;

// shows the overlay loading mask
Core.ShowLoadingMask("Please wait...");

// start task
Task.Factory.StartNew(() => {

    // go to server and get the data
    var employee = new Data.Entities.Employee(employeeId);

    // instantiate the class type (reflection)
    frm = Activator.CreateInstance(type,employee );

}).ContinueWith((task) => {

    // hide loading mas
    Core.HideLoadingMask();

    if (frm != null) this.Panel.Controls.Add(frm);

});

那么,我如何强制使用ContinueWith()中的代码强制使用当前线程,或者我可能做错了.

我需要的过程是:

>从服务器获取数据之前显示加载掩码
>从服务器获取数据(可能需要3秒)
>之后,退出任务并隐藏加载掩码.

任何线索?

解决方法

TaskScheduler.FromCurrentSynchronizationContext()传递给ContinueWith()……

.ContinueWith((task) => {
    // hide loading mas
    Core.HideLoadingMask();

    if (frm != null) this.Panel.Controls.Add(frm);
},TaskScheduler.FromCurrentSynchronizationContext());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读