Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#中的ContinueWith在ContinueWith之前使用延迟来执行Task.continue?_C#_Task_Task Parallel Library - Fatal编程技术网

如何使用C#中的ContinueWith在ContinueWith之前使用延迟来执行Task.continue?

如何使用C#中的ContinueWith在ContinueWith之前使用延迟来执行Task.continue?,c#,task,task-parallel-library,C#,Task,Task Parallel Library,我知道我可以做这样一个简单的延迟任务: Task.Delay(iDelayInMilliseconds).ContinueWith(() => SomeMethod()); Task.Run(() => SomeMethod()).ContinueWith(() => AnotherMethod()); Task.Run(() => SomeMethod()).Delay(iDelayInMilliseconds).ContinueWith(() => Anot

我知道我可以做这样一个简单的延迟任务:

Task.Delay(iDelayInMilliseconds).ContinueWith(() => SomeMethod());
Task.Run(() => SomeMethod()).ContinueWith(() => AnotherMethod());
Task.Run(() => SomeMethod()).Delay(iDelayInMilliseconds).ContinueWith(() => AnotherMethod());
void Main()
{
    var iDelayInMilliseconds = 4000;

    IObservable<int> query =
        from x in Observable.Start(() => SomeMethod())
        from y in Observable.Timer(TimeSpan.FromMilliseconds(iDelayInMilliseconds))
        from z in Observable.Start(() => AnotherMethod())
        select x + z;

    IDisposable subscription = query.Subscribe(w => Console.WriteLine(w));
}

public int SomeMethod() => 1;
public int AnotherMethod() => 2;
我可以将两种不同的方法链接为如下顺序任务:

Task.Delay(iDelayInMilliseconds).ContinueWith(() => SomeMethod());
Task.Run(() => SomeMethod()).ContinueWith(() => AnotherMethod());
Task.Run(() => SomeMethod()).Delay(iDelayInMilliseconds).ContinueWith(() => AnotherMethod());
void Main()
{
    var iDelayInMilliseconds = 4000;

    IObservable<int> query =
        from x in Observable.Start(() => SomeMethod())
        from y in Observable.Timer(TimeSpan.FromMilliseconds(iDelayInMilliseconds))
        from z in Observable.Start(() => AnotherMethod())
        select x + z;

    IDisposable subscription = query.Subscribe(w => Console.WriteLine(w));
}

public int SomeMethod() => 1;
public int AnotherMethod() => 2;
我搞不懂的是这样的:

Task.Delay(iDelayInMilliseconds).ContinueWith(() => SomeMethod());
Task.Run(() => SomeMethod()).ContinueWith(() => AnotherMethod());
Task.Run(() => SomeMethod()).Delay(iDelayInMilliseconds).ContinueWith(() => AnotherMethod());
void Main()
{
    var iDelayInMilliseconds = 4000;

    IObservable<int> query =
        from x in Observable.Start(() => SomeMethod())
        from y in Observable.Timer(TimeSpan.FromMilliseconds(iDelayInMilliseconds))
        from z in Observable.Start(() => AnotherMethod())
        select x + z;

    IDisposable subscription = query.Subscribe(w => Console.WriteLine(w));
}

public int SomeMethod() => 1;
public int AnotherMethod() => 2;
实际上,我希望SomeMethod()在延迟之后运行,然后再运行AnotherMethod()

有人能帮我吗

Task.Run(async () =>
{
    SomeMethod();
    await Task.Delay(iDelayInMilliseconds);
}).ContinueWith(cw => AnotherMethod());
在这里,我们需要实际等待内部
任务
(即
任务.Delay(idelayin毫秒)
)完成,否则
任务.Run()
返回的
任务
将立即返回,其
状态设置为
RanToCompletion

当然,如果这不需要在
ThreadPool
线程上运行,您可以删除
Task.run()

// ...
SomeMethod();
await Task.Delay(iDelayInMilliseconds);
AnotherMethod();

…这意味着您必须将父方法标记为
async
,并让它返回
Task
,而不是
T
,或者
Task
,如果它返回
void

,我建议查看Microsoft的反应式框架。它比Tasks、IMHO强大得多,可以像LINQ查询一样使用,并且可以很容易地与任务和异步操作进行互操作

在您的情况下,代码可以如下所示:

Task.Delay(iDelayInMilliseconds).ContinueWith(() => SomeMethod());
Task.Run(() => SomeMethod()).ContinueWith(() => AnotherMethod());
Task.Run(() => SomeMethod()).Delay(iDelayInMilliseconds).ContinueWith(() => AnotherMethod());
void Main()
{
    var iDelayInMilliseconds = 4000;

    IObservable<int> query =
        from x in Observable.Start(() => SomeMethod())
        from y in Observable.Timer(TimeSpan.FromMilliseconds(iDelayInMilliseconds))
        from z in Observable.Start(() => AnotherMethod())
        select x + z;

    IDisposable subscription = query.Subscribe(w => Console.WriteLine(w));
}

public int SomeMethod() => 1;
public int AnotherMethod() => 2;
这仍然会以正确的延迟生成正确的结果


只需NuGet“System.Reactive”并使用System.Reactive.Linq添加
task.ContinueWith(t=>Thread.Sleep(idelayin毫秒))