반응형
UI 스레드에서 작업 계속
초기 작업이 생성 된 스레드에서 작업 계속을 실행하도록 지정하는 '표준'방법이 있습니까?
현재 아래 코드가 있습니다. 작동하지만 디스패처를 추적하고 두 번째 액션을 만드는 것은 불필요한 오버 헤드처럼 보입니다.
dispatcher = Dispatcher.CurrentDispatcher;
Task task = Task.Factory.StartNew(() =>
{
DoLongRunningWork();
});
Task UITask= task.ContinueWith(() =>
{
dispatcher.Invoke(new Action(() =>
{
this.TextBlock1.Text = "Complete";
}
});
다음과 같이 연속을 호출하십시오 TaskScheduler.FromCurrentSynchronizationContext()
.
Task UITask= task.ContinueWith(() =>
{
this.TextBlock1.Text = "Complete";
}, TaskScheduler.FromCurrentSynchronizationContext());
현재 실행 컨텍스트가 UI 스레드에있는 경우에만 적합합니다.
비동기를 사용하면 다음과 같이 할 수 있습니다.
await Task.Run(() => do some stuff);
// continue doing stuff on the same context as before.
// while it is the default it is nice to be explicit about it with:
await Task.Run(() => do some stuff).ConfigureAwait(true);
하나:
await Task.Run(() => do some stuff).ConfigureAwait(false);
// continue doing stuff on the same thread as the task finished on.
반환 값이 있으면 UI로 보내야하며 다음과 같이 일반 버전을 사용할 수 있습니다.
필자의 경우 MVVM ViewModel에서 호출됩니다.
var updateManifest = Task<ShippingManifest>.Run(() =>
{
Thread.Sleep(5000); // prove it's really working!
// GenerateManifest calls service and returns 'ShippingManifest' object
return GenerateManifest();
})
.ContinueWith(manifest =>
{
// MVVM property
this.ShippingManifest = manifest.Result;
// or if you are not using MVVM...
// txtShippingManifest.Text = manifest.Result.ToString();
System.Diagnostics.Debug.WriteLine("UI manifest updated - " + DateTime.Now);
}, TaskScheduler.FromCurrentSynchronizationContext());
이 유용한 스레드이기 때문에이 버전을 추가하고 싶었고 이것이 매우 간단한 구현이라고 생각합니다. 멀티 스레드 응용 프로그램 인 경우이 유형을 여러 유형으로 여러 번 사용했습니다.
Task.Factory.StartNew(() =>
{
DoLongRunningWork();
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{ txt.Text = "Complete"; }));
});
참고URL : https://stackoverflow.com/questions/4331262/task-continuation-on-ui-thread
반응형
'Programing' 카테고리의 다른 글
Ruby on Rails : 전역 상수를 정의 할 위치는? (0) | 2020.05.07 |
---|---|
Kotlin의 MutableList를 초기화하여 MutableList를 비우려면 어떻게해야합니까? (0) | 2020.05.07 |
background-size와 같은 것이 있습니까? 이미지 요소에 대해 포함하고 포함합니까? (0) | 2020.05.07 |
jQuery를 사용하여 패딩 또는 여백 값을 정수로 픽셀 단위로 표시 (0) | 2020.05.07 |
현재 줄에서 파일 끝까지 모든 텍스트를 VIM에서 어떻게 삭제할 수 있습니까? (0) | 2020.05.07 |