Results 1 to 7 of 7

Thread: InvalidOperationException was unhandled by user code

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    InvalidOperationException was unhandled by user code

    Hello folks,

    So, I'm trying to make a program that logs data from a multimeter (voltage vs. time) and displays it on a graph. The computer is talking to the voltmeter OK, but the plotting part isn't working properly. When trying to run the code, I get the aforementioned error, in addition to the following:

    Cross-thread operation not valid: Control 'FIDChart' accessed from a thread other than the thread it was created on.

    Here's the code:
    Code:
     Private Sub voltTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles voltTimer.Elapsed
            If TCDCheckBox.Checked And FIDCheckBox.Checked Then
                'switch channels and read voltages -- IN PROGRESS
    
            ElseIf TCDCheckBox.Checked Then
                myDevice.Write("READ?")
                Dim TCDVoltage = myDevice.ReadString()
    
    
            ElseIf FIDCheckBox.Checked Then
                Runtime = Runtime + 2
                myDevice.Write(":READ?")
                Dim FIDVoltage = myDevice.ReadString()
                MessageBox.Show(FIDVoltage)
                MessageBox.Show(Runtime)
                FIDChart.Series("Series1").Points.AddXY(2, 5)
                FIDChart.Series("Series1").Points.AddXY(Runtime, FIDVoltage)
    
            End If
            'when read is over, print values to chart ... when exporting data copy chart data to a text or csv file\
    
        End Sub
    The code won't even print the fixed point of (2,5), on the graph. But the variables (voltage, time) display totally fine in messageboxes. And the following code works totally fine, and places two points on the graph:
    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            FIDChart.Series("Series1").Points.AddXY(10, 20)
            FIDChart.Series("Series1").Points.AddXY(5, 10)
        End Sub
    Any idea what might be going on?

    If the auto-updating chart can be made to work, I've another question; how can I display the graph data in the form of a .csv file? For example:
    2,1.1
    4,0.5
    6,0.7

    Thanks,

    NSEasternShoreChemist
    Last edited by Shaggy Hiker; Oct 2nd, 2017 at 03:09 PM. Reason: Added CODE tags.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: InvalidOperationException was unhandled by user code

    You problem is happening because the system.Timers.Time class raises it's elapsed event on a secondary thread and you cannot (or at least should not) update the GUI from a thread other than the mainUI thread.

    The easy fix is to change the timer to use the System.Windows.Forms.Timer Class.

    The other fix is to invoke the mainUI when you need to update the GUI like this,

    VB.Net Code:
    1. 'create a new method to update from
    2. Private sub updateGui(x as integer, y as integer,runTime as string, FIDVoltage as string)
    3.     MessageBox.Show(FIDVoltage)
    4.     MessageBox.Show(Runtime)
    5.     FIDChart.Series("Series1").Points.AddXY(x, y)
    6.     FIDChart.Series("Series1").Points.AddXY(Runtime, FIDVoltage)
    7. End Sub
    8.  
    9. 'then in the timer hander
    10. Me.Invoke(Sub() updateGui(2,5,RunTime,FIDVoltage))
    beware of freehand code
    Last edited by kebo; Oct 2nd, 2017 at 04:38 PM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: InvalidOperationException was unhandled by user code

    Nevermind, I fixed the problem, using this code:
    Code:
     Private Sub V_Tick(sender As System.Object, e As System.EventArgs) Handles VTimer.Tick
            If TCDCheckBox.Checked And FIDCheckBox.Checked Then
                'switch channels and read voltages -- IN PROGRESS
    
            ElseIf TCDCheckBox.Checked Then
                myDevice.Write("READ?")
                Dim TCDVoltage = myDevice.ReadString()
    
    
            ElseIf FIDCheckBox.Checked Then
                Runtime = Runtime + 1
                myDevice.Write(":READ?")
                Dim FIDVoltage = myDevice.ReadString()
                FIDChart.Series("Series1").Points.AddXY(Runtime, FIDVoltage)
            End If
    
        End Sub
    Thanks!
    Last edited by Shaggy Hiker; Oct 2nd, 2017 at 03:10 PM. Reason: Added CODE tags.

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: InvalidOperationException was unhandled by user code

    looks like you changed the timer
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: InvalidOperationException was unhandled by user code

    I edited your posts to add [CODE][/CODE] tags. You can do this by pressing the # button and pasting your code between the tags, or pasting the code, selecting it, then pressing the # button.

    It does look like you just changed the Forms.Timer, but that may not be the best solution for you in the long run. That puts all the work on the UI thread, which may be just fine. However, if you want to read often, then the reading might be better done on a background thread. If your timer interval is 100, or greater, then you should be fine. If you want to get readings faster than that, then a background thread would probably become necessary. You can do finer timing things on a background thread than you'd ever care to do on a foreground thread.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    10

    Re: InvalidOperationException was unhandled by user code

    While the multimeter can theoretically read up to 500 times per second, I don't think I'll ever want to change the timer to less than 100, as that would likely lead to burnt out relays in the voltmeter.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: InvalidOperationException was unhandled by user code

    Good enough, then.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width