DispatcherTimer
을 사용합니다. 매 2 분마다 작업을해야하기 때문입니다. 이 내에서 BackgroundWorker
을 호출하여 내 작업을 수행 한 다음 타이머에 연결된 디스패처를 사용하여 UI를 업데이트합니다. 내가 얻는 오류가 타이머와 관련이 있다고 생각하지만 확실하지 않습니다. 운영자 또는 백그라운드 관리자입니까? foreach 내부에서 ReportProgress를 수행하려면 어떻게해야합니까?ReportProgress를 수행 할 때 BackgroundWorker 또는 (Dispatcher) 작업이 이미 완료되었습니다.
오류 :
이러는 :This operation has already had OperationCompleted called on it and further calls are illegal.
DispatcherTimer dispTimer = new DispatcherTimer();
Dispatcher dispatcher = dispTimer.Dispatcher;
dispTimer.Tick += delegate {dispTimer_Tick(dispatcher); };
dispTimer.Interval = new TimeSpan(0, 0, 45);
dispTimer.Start();
private void DoWork(object sender,Dispatcher dispatcher)
{
int counterTotalSteps = PartialEmployees.Count();
int counterOnStep = 1;
dispatcher.BeginInvoke(new Action(() =>
{
AllEmployees.Clear();
//calling the ReportProgress here works
foreach (var item in PartialEmployees)
{
counterOnStep ++;
//part below throws the error
(sender as BackgroundWorker).ReportProgress((counterTotalSteps/100) * counterOnStep);
AllEmployees.Add(item);
}
counterOnStep = 0;
}));
}
EDIT : 스택 트레이스 :
여기(sender as BackgroundWorker).ReportProgress((counterTotalSteps/100) * counterOnStep);
은 간략화 "AllEmployees.Clear를();"이벤트 617,451,515,
at System.ComponentModel.AsyncOperation.VerifyNotCompleted()
at System.ComponentModel.AsyncOperation.Post(SendOrPostCallback d, Object arg)
at System.ComponentModel.BackgroundWorker.ReportProgress(Int32 percentProgress, Object userState)
at System.ComponentModel.BackgroundWorker.ReportProgress(Int32 percentProgress)
at testDispatcher.ViewModel.EmployeeListViewModel.<>c__DisplayClass7.<DoWork>b__6() in C:\Users\kozaj\Documents\Visual Studio 2010\Projects\testDispatcher\testDispatcher\ViewModel\EmployeeListViewModel.cs:line 91
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at testDispatcher.App.Main() in C:\Users\kozaj\Documents\Visual Studio 2010\Projects\testDispatcher\testDispatcher\obj\x86\Debug\App.g.cs:line 50
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
스택 추적이란 무엇입니까? – SLaks
왜 BackgroundWorker를 사용하고 있지만 UI 스레드에서 모든 작업을 수행하고 있습니까? – SLaks
이것은 다른 예제에서 구현하기 전에 제대로 작동하는지 확인할 수있는 예제 코드입니다. dipatcher 밖에서 어떤 부분을 움직이시겠습니까? 또한 이러한 목록은 UI에 바인딩됩니다. – LobalOrning