이제는이 일이 저를 미치게합니다.작업 본문이 실행되지 않습니다
private static Task Delay(double milliseconds)
{
var tcs = new TaskCompletionSource<bool>();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += (obj, args) => { tcs.TrySetResult(true); };
timer.Interval = milliseconds;
timer.AutoReset = false;
timer.Start();
return tcs.Task;
}
나는 다음과 같은 코드에서 호출 : .NET FX는 4.0을 사용하여, 나는 다음과 같은 지연 메커니즘이
// delay the execution of SendKey to let the dialog show up
var sendKeyTask = Delay(500).ContinueWith((_) =>
{
// this gets executed when the dialog is visible
SendKeys.Send(filePath);
}, TaskScheduler.FromCurrentSynchronizationContext());
MyButton.InvokeMember("click");
sendKeyTask.Wait(3000); //will time out after 3 seconds.
....
문제는 SendKeys.Send(filePath);
라인이 실행되지 않습니다 것입니다. 내가 뭘 놓치고 있니?
메인 컨텍스트에서 동기 대기 및 비동기 연속을 한 곳에서하는 것은 거의 항상 나쁜 생각입니다. 나는'sendKeyTask'와'.Wait (3000)'의 연속 사이에 교착 상태가 있다는 것을 확신 할 수 있습니다. 특히 연속이 '현재'동기화 컨텍스트에서 실행되도록 예약되었으므로 말입니다. '지연 '자체가 작동하는 것처럼 보였습니다. LINQPad에서 시도했습니다. –