ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

InvalidOperationException, Collection was modified;

My application works fine on the development PC, but it fails when I run it on the target PC.
The target runs XP (fully up to date), I have installed dotnetfx and Measurement Studio 8 (with the patch).
I use Visual C# 2005 Express.
I have used the Publish Wizard in Release configuration

When my application starts I get a messagebox saying there was an Unhandled exception, and two graphs in a window have red crosses on them. I can continue and the rest of the application works, including another window with graphs.

The exception dump:
************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Hashtable.HashtableEnumerator.MoveNext()
at UICM.ac.c(ComponentDrawArgsInternal )
at UICM.ac.DrawForeground(ComponentDrawArgsInternal args)
at NationalInstruments.Restricted.ControlElement.a(ComponentDrawArgsInternal , Rectangle , Boolean )
at NationalInstruments.Restricted.ControlElement.DrawChildren(ComponentDrawArgsInternal args, Rectangle clipRectangle)
at NationalInstruments.Restricted.ControlElement.a(ComponentDrawArgsInternal , Rectangle , Boolean )
at NationalInstruments.Restricted.ControlElement.DrawChildren(ComponentDrawArgsInternal args, Rectangle clipRectangle)
at NationalInstruments.Restricted.ControlElement.a(ComponentDrawArgsInternal , Rectangle , Boolean )
at NationalInstruments.Restricted.ControlElement.a(ComponentDrawArgsInternal , Rectangle )
at NationalInstruments.Restricted.ControlElement.Paint(PaintEventArgs e)
at NationalInstruments.UI.WindowsForms.ControlBase.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

---------------------------
Could not include everything since message exceeds 5000 characters.
0 Kudos
Message 1 of 3
(5,715 Views)

From the description and the stack trace, I believe that the problem is that you are updating the graph from a non-UI thread. This is a problem because Windows Forms controls are not thread-safe. Notice that the class overviews of all .NET Framework and Measurement Studio Windows Forms controls have a thread safety section that states:

"Only the following members are safe for multithreaded operations: BeginInvoke, EndInvoke, Invoke, InvokeRequired, and CreateGraphics."

For more information, see the .NET Quickstart article "Making procedure calls across thread boundaries." Please check if this is the problem and post a reply with what you find. Thanks.

- Elton

0 Kudos
Message 2 of 3
(5,705 Views)
You are right. It works now. Thanks.

I have had a few 'reminders' about IllegalCrossThreadCalls during development, and fixed those with delegates.
I thought I had it sorted out, but it is easy to loose track of which task is doing what. And it was a bit unexpected to get that kind of exception.
0 Kudos
Message 3 of 3
(5,693 Views)