良い一日!
他のコントロールを配置できるカスタムpopupcontainer control
コンボボックスがあり、ポップアップに表示されます。私の場合、それは含まれていますdatagrid
。高速に動作させたい-ユーザーがコントロールをポップアップすることを決定した場合にのみ、そのデータグリッドが必要です。データグリッドの作成を他のスレッドに移動して使用Backgroundworker
することにしました。これは、UIに完全に適合しており、Control.Invokeをいじりたくないためです。これが私の簡略化されたコードです:
protected override void OnCreateControl()
{
base.OnCreateControl();
CreateContainer();
}
protected virtual void CreateContainer()
{
_worker = new BackgroundWorker();
_worker.DoWork += DoActionsOnAsyncWork;
_worker.RunWorkerCompleted += AsyncWorker_RunWorkerCompleted;
_worker.RunWorkerAsync(this);
}
protected void DoActionsOnAsyncWork(object sender, DoWorkEventArgs e)
{
var _grid = ///Create and initialize local grid here
e.Result = _grid; // Throw it to RunWorkerCompleted event
}
private void AsyncWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null && !IsDisposed && !IsDisposing)
{
if (!_closing)
{
this.Grid = e.Result as MyGrid;
AttachEventHandlersToGrid();
} else
(e.Result as MyGrid).Dispose(); // Get rid of grid
}
// Get rid of worker
_worker.DoWork -= DoActionsOnAsyncWork;
_worker.RunWorkerCompleted -= AsyncWorker_RunWorkerCompleted;
_worker.Dispose();
_worker = null;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
WaitForBackgroundWorker();
// Dispose other stuff
...
}
base.Dispose(disposing);
}
protected void WaitForBackgroundWorker()
{
if (_worker == null ||
!_worker.IsBusy)
return;
_closing = true;
while (_worker != null && _worker.IsBusy)
Application.DoEvents(); // Hack throw worker.RunWorkerCompleted on top of msg queue
}
今、私は_worker
自分のコントロールを適切に破棄するために待つ必要があります(フォームはより広い範囲です)。さらに、それも処分する必要があるものを_worker
作成します。私の質問は、 Application.DoEvents()なしgrid
でどのように待つことができるかです。backgroundworker
時々(多くの場合、リモートデスクトップ上で-おそらく他のペイントアルゴリズム?)、アプリケーション全体がハングする原因になります。コールスタック:
mscorlib.dll!System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle waitableSafeHandle, long millisecondsTimeout, bool hasThreadAffinity, bool exitContext) + 0x1f bytes
mscorlib.dll!System.Threading.WaitHandle.WaitOne(long timeout, bool exitContext) + 0x23 bytes
mscorlib.dll!System.Threading.WaitHandle.WaitOne(int millisecondsTimeout, bool exitContext) + 0x1c bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle waitHandle) + 0x96 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control caller, System.Delegate method, object[] args, bool synchronous) + 0x34b bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.Invoke(System.Delegate method, object[] args) + 0x50 bytes
System.Windows.Forms.dll!System.Windows.Forms.WindowsFormsSynchronizationContext.Send(System.Threading.SendOrPostCallback d, object state) + 0x56 bytes
System.dll!Microsoft.Win32.SystemEvents.SystemEventInvokeInfo.Invoke(bool checkFinalization, object[] args) + 0x66 bytes
System.dll!Microsoft.Win32.SystemEvents.RaiseEvent(bool checkFinalization, object key, object[] args) + 0x110 bytes
System.dll!Microsoft.Win32.SystemEvents.RaiseEvent(object key, object[] args) + 0xe bytes
System.dll!Microsoft.Win32.SystemEvents.OnUserPreferenceChanged(int msg, System.IntPtr wParam, System.IntPtr lParam) + 0x76 bytes
System.dll!Microsoft.Win32.SystemEvents.WindowProc(System.IntPtr hWnd, int msg, System.IntPtr wParam, System.IntPtr lParam) + 0x2c6 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) + 0x357 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) + 0x33d bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) + 0x5f bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.DoEvents() + 0x18 bytes
MyControl.WaitForBackgroundWorker() Line 874 + 0x1b bytes
... // Many recursive disposes
Form.Dispose()