如何使用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添加在您的示例中,最好不要使用老式的continue,而完全支持async/await。后者更容易理解,如果您坚持使用ContinueWith,您可以使用task.ContinueWith(t=>Thread.Sleep(idelayin毫秒))