Hallo,
we use the WinGrid in a Windows Forms application and have a threading issue there. We get a call via .NET Remoting which starts a seperate thread. This thread sets the datasource of the grid. At that moment the main thread seems to get a command to paint the grid again which leads to the following just 200 milliseconds after the setter of the datasource has finished:
Exception System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawHelper(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Boolean clipText, Boolean forceDrawAsFocused, Boolean preventAlphaBlendGraphics) bei Infragistics.Win.UIElement.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Boolean forceDrawAsFocused, Boolean preventAlphaBlendGraphics) bei Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Size elementSize, Boolean preventAlphaBlendGraphics) bei Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Size elementSize) bei Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode) bei Infragistics.Win.UltraWinGrid.UltraGridUIElement.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode) bei Infragistics.Win.UltraControlBase.OnPaint(PaintEventArgs pe) bei Infragistics.Win.UltraWinGrid.UltraGrid.OnPaint(PaintEventArgs pe) bei System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) bei System.Windows.Forms.Control.WmPaint(Message& m) bei System.Windows.Forms.Control.WndProc(Message& m) bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) with Message: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.StackTrace: bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawChildElements(UIElementDrawParams& drawParams) bei Infragistics.Win.UIElement.DrawElement(UIElementDrawParams& defaultDrawParams) bei Infragistics.Win.UIElement.DrawHelper(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Boolean clipText, Boolean forceDrawAsFocused, Boolean preventAlphaBlendGraphics) bei Infragistics.Win.UIElement.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Boolean forceDrawAsFocused, Boolean preventAlphaBlendGraphics) bei Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Size elementSize, Boolean preventAlphaBlendGraphics) bei Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode, Size elementSize) bei Infragistics.Win.ControlUIElementBase.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode) bei Infragistics.Win.UltraWinGrid.UltraGridUIElement.Draw(Graphics graphics, Rectangle invalidRectangle, Boolean doubleBuffer, AlphaBlendMode alphaBlendMode) bei Infragistics.Win.UltraControlBase.OnPaint(PaintEventArgs pe) bei Infragistics.Win.UltraWinGrid.UltraGrid.OnPaint(PaintEventArgs pe) bei System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) bei System.Windows.Forms.Control.WmPaint(Message& m) bei System.Windows.Forms.Control.WndProc(Message& m) bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
We tried locking (lock(this)) in the datasource setter, in the OnPaint (which is not called), in a handler of the paint-Event and in a handler of the paint-Event of the parent control but nothing helped. We tried calling SuspendLayout and ResumeLayout in the setter but the problem occurs afterwards. For all the other controls (UltraEditor ...) we could solve the thread issues by calling Invoke but for the grid we don't know what to invoke.
Any suggestions?
The key is to use
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(MethodToInvoke))
return
}
The first line, InvokeRequired, will tell you if an invoke is needed. If so this.Invoke will Invoke the Method on the correct thread.
This error is clearly occurring because something you are doing is crossing threads without proper marshalling. You must be very careful when using mulitple threads that nothing you do on a different thread affects the UI Thread directly without marshalling across. So if your grid is bound to a data source and you do anything to that data source on a thread other than the UI Thread, the data source will send a notification to the grid and the grid will respond to it, thus causing all sorts of problems and possible exceptions like the one you have here.