English 中文(简体)
B. 界定基于同步方法的任务要求
原标题:Where to define callback for Task based asynchronous method

> 这个问题,我试图采用TPL,并试图遵循技援方案的准则。

我希望我的同仁办法在结束时能够回响。 只要我能看到,我可以这样做。

(1) 我的任务代表人工背叛

public Task DoWorkAsync(DoWorkCompletedCallback completedCallback)
{
    return Task.Factory.StartNew(
    { 
        //do work

        //call callback manually
        completedCallback();
    });
}

2) 任务代表的任务被放弃

public Task DoWorkAsync(DoWorkCompletedCallback completedCallback)
{
    return Task.Factory.StartNew(
    { 
        //do work
    }
    ).ContinueWith(completedCallback); //assign callback to Task
}

3) 辞退召集人的任务

public Task DoWorkAsync()
{
    return Task.Factory.StartNew(
    { 
        //do work
    });
}

public void SomeClientCode()
{
    Task doingWork = DoWorkAsync();
    doingWork.ContinueWith(OnWorkCompleted);
}

我的感觉是,3个是更加正确的,因为它从这种方法中解脱了呼吁,意味着客户守则能够以它认为合适的任何方式管理任务(使用警示、投票等),而这似乎是所有任务。 然而,如果在客户代码背书之前完成工作,情况如何?

是否有普遍接受的办法来做到这一点,还是太新?

这样做是否比1更有利?

最佳回答

普遍接受的方式是3。

TPL的使用者一般知道他们可以继续工作。 第1和第2条提供了同样的功能,但具有非标准接口;用户必须说明代表参数的含义,以及如果他们不希望继续这样做的话,那么所有那些方法仍然能够以标准方式延续的任务。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签