처음에는 " 비동기 메서드가 완료 될 때까지 기다리는 방법 "질문 일 뿐이라고 생각했습니다 . 하지만 그 이상이라고 생각합니다.
타이머가 설정되어 있습니다.
public void Start()
{
_timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
_timer.Interval = _context.adpSettings.SyncInterval * 1000;
_timer.AutoReset = false;
_timer.Enabled = true;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var t = ExecuteTransactionReportAsync();
_timer.Start();
}
private async Task ExecuteTransactionReportAsync()
{
AccessEvent accessEvent = new AccessEvent();
.... do some logic
await _context.GetConnector().EnqeueuEventAsync(accessEvent);
}
내가하려는 것은 끝날 timer_Elapsed()
때까지 다시 불 을 피우지 않는 ExecuteTransactionReportAsync()
것입니다. 그러나 ExecuteTransactionReportAsync()
비동기 이기 때문에 프로세스가 계속 진행되고 timer_Elapsed()
다시 시작됩니다.
실제로 ExecuteTransactionReportAsync()
작업을 완료하는 데 10 초 이상 걸리지 않습니다 . (적어도 좋지 않거나 다른 문제가 있습니다.)하지만 디버깅 할 때는 고통 스럽습니다.
ExecuteTransactionReportAsync()
비 동기화를 포함하지 않는 간단한 솔루션이 있습니까?
TAP에 대한 나의 이해가 정확하다면 의도 한대로 작동 할 것입니다.
private async void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
await ExecuteTransactionReportAsync();
_timer.Start();
}
또는 DavidG가 제안한 내용 :
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
_timer.Stop();
ExecuteTransactionReportAsync().ContinueWith( () => {_timer.Start(); });
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다