주 스레드에서 많은 계산을하고 있으며 이러한 계산은 별도의 스레드에서 실행할 수 없습니다.백그라운드 스레드를 사용하여 "Busy Indicator"를 표시합니다.
계산이 실행 중일 때 애플리케이션 UI에 '통화 중 표시기'(즉, 회전 위젯)를 표시하려고합니다. 따라서 이러한 계산이 실행되는 동안 UI가 잠기므로 주 스레드에서 통화 중 표시기를 표시 할 수 없습니다.
이 문제를 해결하려면 통화 중 표시기를 별도의 스레드로 이동하려고 시도했습니다. this post의 도움으로 별도의 스레드에 통화 중 표시기를 배치 할 수 있습니다. 그러나이 스레드와 통신하여 통화 중 표시기를 시작하거나 중지 할 수 없습니다.
private HostVisual CreateBusyIndicatorOnWorkerThread()
{
// Create the HostVisual that will "contain" the VisualTarget
// on the worker thread.
HostVisual hostVisual = new HostVisual();
Thread thread = new Thread(new ParameterizedThreadStart(BusyIndicatorWorkerThread));
thread.ApartmentState = ApartmentState.STA;
thread.IsBackground = true;
thread.Start(hostVisual);
// Wait for the worker thread to spin up and create the VisualTarget.
s_event.WaitOne();
return hostVisual;
}
private static AutoResetEvent s_event = new AutoResetEvent(false);
private void BusyIndicatorWorkerThread(object arg)
{
// Create the VisualTargetPresentationSource and then signal the
// calling thread, so that it can continue without waiting for us.
HostVisual hostVisual = (HostVisual)arg;
VisualTargetPresentationSource visualTargetPS = new VisualTargetPresentationSource(hostVisual);
s_event.Set();
// Create a MediaElement and use it as the root visual for the
// VisualTarget.
visualTargetPS.RootVisual = CreateBusyIndicator();
// Run a dispatcher for this worker thread. This is the central
// processing loop for WPF.
System.Windows.Threading.Dispatcher.Run();
}
private FrameworkElement CreateBusyIndicator()
{
var busyIndicator = new MyBusyIndicator();
//busyIndicator.DataContext = this.
Binding myBinding = new Binding("IsBusy");
myBinding.Source = this;
busyIndicator.SetBinding(MyBusyIndicator.IsBusyProperty, myBinding);
}
항상 "다른 스레드가이 개체를 소유하고 있기 때문에 호출 한 스레드는이 개체에 액세스 할 수 없습니다."예외가 발생합니다. 통화 중 표시기가 다른 스레드에 의해 소유되어있는 동안 주 스레드에서 통화 표시기를 업데이트하려고하기 때문입니다. 나는 또한 this article에 주어진 접근을 시도
,
private void CreateAndShowContent()
{
Dispatcher = Dispatcher.CurrentDispatcher;
VisualTargetPresentationSource source =
new VisualTargetPresentationSource(_hostVisual);
_sync.Set();
source.RootVisual = _createContent();
DesiredSize = source.DesiredSize;
_invalidateMeasure();
Dispatcher.Run();
source.Dispose();
}
그러나이 방법의 Dispatcher.Run() 아무것도 계산의 종료 후까지 발생하지 후 바쁜 표시기가 표시됩니다와
.메인 스레드에서 통화 중 표시기가있는 스레드로 통신하고 싶습니다. 누구나 접근 할 수 있습니까?
다른 스레드에서 이러한 작업을 실행할 수없는 이유를 입력하십시오. 실용적인 이유는 없습니다. – ElGauchooo
UI 스레드에서 비 UI 작업을 수행하고 비 UI 스레드에서 UI 작업을 수행하는 대신 역순으로 처리하십시오. UI 스레드는 UI 스레드에서 작동하고 비 UI UI는 비 UI 스레드에서 작동합니까? 이는 단순히 사용중인 전체 시스템의 디자인 일뿐입니다. – Servy
해당 계산 및 호출자 스레드가 주 스레드로 확인하는 타사 라이브러리를 사용하고 있습니다. 따라서 UI를 다른 스레드에서 수행해야하므로 구현을 변경할 수 없습니다. – Ahmed