Results 1 to 19 of 19

Thread: [RESOLVED] Problems with Invoke | Serial Port Reading

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Resolved [RESOLVED] Problems with Invoke | Serial Port Reading

    In my project, i have a parent form where i will open a child form called Monitor.

    In this form i will read the Serial Port and place its contents on a textbox " txtID "

    This is how im reading the SerialPort and placing the string in the Textbox.

    Code:
    Delegate Sub myMethodDelegate(ByVal [text] As String)
        Dim myDelegate As New myMethodDelegate(AddressOf ShowString)
    
        Public Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
    
            Dim aa As String = sp.ReadLine
            Invoke(myDelegate, aa)    // This is where the error happens
            SerialString = aa
    
            Me.Invoke(Sub() txtid.Text = aa.TrimEnd) // This is the line of code that puts the current string in the textbox
     End Sub
    The first time i open the Child Form "Monitor" and Place my finger into the Fingerprint Sensor, it will read the string that the Arduino sends to the serial port, and places it correctly on the textbox, All good.

    BUT!!! If i close the Child Form "Monitor" and open it again, and place my finger on the Sensor again, The project crashes before reading the Serial Port and i get this Ex Error.

    Cannot call Invoke or BeginInvoke on a control until the window handle has been created

    I have to be able to Close and Open this ChildForm " Monitor " as many times as i want and to be able to use it correctly without any problems.

    What is this ? How can i deal with this? Im guessing it's some kind of Multi Threading problem? I know little about that part of programming.

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

    Re: Problems with Invoke | Serial Port Reading

    Your problem is that your DataReceived event handler is running before the window handle for the control you are invoking is created. I would guess that you are hiding and showing the same form each time rather than creating a new form instance each time?

    You may need to destroy the form and create a new instance each time, or detached the handler for the DataRecieved event and add it as needed. I don't see the Handles keyword on the tail of the sub declaration, so you must be adding the handler dynamically. If that is the case, then you need to change when you are adding the handler.

    It's really hard to say how to fix it because ultimately, the problem is not occurring because of the code you posted. It's occurring because of how you are loading/showing the form I think.
    kevin
    Last edited by kebo; Apr 16th, 2015 at 09:12 AM.
    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
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: Problems with Invoke | Serial Port Reading

    This is the full code that im using to manage the SerialPort
    Code:
     Delegate Sub myMethodDelegate(ByVal [text] As String)
        Dim myDelegatee As New myMethodDelegate(AddressOf ShowString1)
    
        Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
    
            Dim bb As String = sp.ReadLine
    
            BeginInvoke(myDelegatee, bb)
            Me.BeginInvoke(Sub() txtid.Text = bb.TrimEnd)
    
        End Sub
    
        Private Sub monitorizar_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler sp.DataReceived, AddressOf SerialPort_DataReceived
    
        End Sub
    
        Public Sub ShowString1(ByVal myString As String)
            txtsp.AppendText(myString)
        End Sub
    And this is how i open the Child forms

    Code:
     For Each f As Form In Application.OpenForms
                If TypeOf f Is monitorizar Then
                    f.Activate()
                    Return
                End If
            Next
            Dim myChild As New monitorizar
            myChild.MdiParent = Me
            myChild.Show()
    Each time i open the Child form and close it, am i hiding/showing or am i creating a new instance as you said?

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

    Re: Problems with Invoke | Serial Port Reading

    Each time i open the Child form and close it, am i hiding/showing or am i creating a new instance as you said?
    Yes. With exception to the first time you show it.

    Try moving this
    Code:
    AddHandler sp.DataReceived, AddressOf SerialPort_DataReceived
    into the form's Shown event.
    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

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: Problems with Invoke | Serial Port Reading

    Quote Originally Posted by kebo View Post
    Yes. With exception to the first time you show it.

    Try moving this
    Code:
    AddHandler sp.DataReceived, AddressOf SerialPort_DataReceived
    into the form's Shown event.
    Will do but can only test it in 1hour, if that doesn't work do you have any other ideas? i've been completely stuck for days!! my final project is done and this is the only thing preventing me from presenting it to my teacher

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: Problems with Invoke | Serial Port Reading

    Quote Originally Posted by kebo View Post
    You may need to destroy the form and create a new instance each time, or detached the handler for the DataRecieved event and add it as needed
    kevin
    What about this?

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

    Re: Problems with Invoke | Serial Port Reading

    If that doesn't work, then dispose of the form and create a new one each time.
    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

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: Problems with Invoke | Serial Port Reading

    Quote Originally Posted by kebo View Post
    If that doesn't work, then dispose of the form and create a new one each time.
    How can i make sure im disposing of the form and creating a new one each time ? All this time i thought i was doing that, but as it seems i was only hiding and showing.

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

    Re: Problems with Invoke | Serial Port Reading

    That depends on how/where you are declaring and creting the form. Try the solution in post #4 and see if it works.
    Probability is high with that solution it is.
    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

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: Problems with Invoke | Serial Port Reading

    Quote Originally Posted by kebo View Post
    That depends on how/where you are declaring and creting the form. Try the solution in post #4 and see if it works.
    Probability is high with that solution it is.

    Didn't work, same error, creating a new form each time makes some sense ( i guess ), how can i do that instead of just hiding and showing?

    This is how im creating and declaring the Child forms

    Code:
    Private Sub MonitorizarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MonitorizarToolStripMenuItem.Click
    
            For Each f As Form In Application.OpenForms
                If TypeOf f Is monitorizar Then
                    f.Activate()
                    Return
                End If
            Next
            Dim myChild As New monitorizar
            myChild.MdiParent = Me
            myChild.Show()
        End Sub
    This code is for the button that Opens the child form, this code is in the Main parent form.

  11. #11
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Problems with Invoke | Serial Port Reading

    Why have the serial port on the child form? It should be on the parent form. Doing this, you will eliminate any instancing issue.

    I also would not create multiple instances of the child form, unless you actually need to show multiple forms - which is unlikely, since you are using a COM port in that form. Just create a single form, and hide and show it as appropriate, in response to serial events.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  12. #12
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Problems with Invoke | Serial Port Reading

    let me ask you one question: what happens if you do the following:

    open the Child Form "Monitor" and Place your finger into the Fingerprint Sensor, it will read the string that the Arduino sends to the serial port, and places it correctly on the textbox, All good.

    BUT!!! If i close the Child Form "Monitor" and place your finger on the Sensor again while the window is not displayed anymore, then....?

  13. #13
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Problems with Invoke | Serial Port Reading

    You seem very confused and have chucked multiple examples together. One minute you write custom delegates then use Lambda expressions. Look in the code bank at JMc's or Nyas examples.

  14. #14

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: Problems with Invoke | Serial Port Reading

    Quote Originally Posted by digitalShaman View Post
    let me ask you one question: what happens if you do the following:

    open the Child Form "Monitor" and Place your finger into the Fingerprint Sensor, it will read the string that the Arduino sends to the serial port, and places it correctly on the textbox, All good.

    BUT!!! If i close the Child Form "Monitor" and place your finger on the Sensor again while the window is not displayed anymore, then....?
    Nothing will happen, because there's nothing supposed to happen! Its just supposed to read the SerialPort when the desired child form is opened.


    Quote Originally Posted by ident View Post
    You seem very confused and have chucked multiple examples together. One minute you write custom delegates then use Lambda expressions. Look in the code bank at JMc's or Nyas examples.
    Yes i am, ive been studying eletronics for the past 3 years and in my final project i have to code in 2 different languages i've never even learned (Vb.net and Arduino) ..for the past months i've been jumping around the different types of languages and im trying to do the best i can, with the time i have and it gets very confusing at times!


    Quote Originally Posted by SJWhiteley View Post
    Why have the serial port on the child form? It should be on the parent form. Doing this, you will eliminate any instancing issue.

    I also would not create multiple instances of the child form, unless you actually need to show multiple forms - which is unlikely, since you are using a COM port in that form. Just create a single form, and hide and show it as appropriate, in response to serial events.
    When i was coding i only wanted to read the SerialPort when that Child Form was opened so i thought it was ok to put the code in the child form.

    And im not creating multiple instances of the same form!
    Code:
     For Each f As Form In Application.OpenForms
                If TypeOf f Is monitorizar Then
                    f.Activate()
                    Return
                End If
    This prevents me from doing just that, only one instance can be opened at a time!

  15. #15

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: [RESOLVED] Problems with Invoke | Serial Port Reading

    And this is how i solved my original problem, all i did was, at the Form_Closing Event, i removed the Handler that was created at Form_Load Event, now i can open and close the Form as many times as i want without any errors.

    Thanks Kebo for suggesting the handler solution!

    Code:
    Private Sub monitorizar_FormClosing(sender As Object, e As EventArgs) Handles MyBase.Load
            RemoveHandler sp.DataReceived, AddressOf SerialPort_DataReceived

  16. #16
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: [RESOLVED] Problems with Invoke | Serial Port Reading

    Nothing will happen, because there's nothing supposed to happen! Its just supposed to read the SerialPort when the desired child form is opened.
    you say nothing will happen. have you tried as i suggested? the reson you give: "there's nothing supposed to happen!" is not at all valid as it was also not supposed to crash with the scenario you described but it did...
    the solution you provided would support my theory that the old handler is still active eevn though the window does not exist anymore so it would crash if you put your finger on the reader while the window was already closed and not yet reopened.

  17. #17
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Problems with Invoke | Serial Port Reading

    If I was writing it, I would probably have also had the serial port processed where it could be opened once and left open.
    You say you don't want it to process if the child form wasn't open, which is fine. They had a term for that, called the "bit bucket". Also known as, "dropping it on the floor".
    If you didn't want to process the serial data, you would just read but not process it.
    If, on the other hand, you want to make the port available to others when the form is not displayed, then I would just Close the port when the form was closed, and reopen the port when the form was shown, but the actual serial port object (and its handler) could always stay available for the life of the program.

  18. #18

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: [RESOLVED] Problems with Invoke | Serial Port Reading

    Quote Originally Posted by digitalShaman View Post
    you say nothing will happen. have you tried as i suggested? the reson you give: "there's nothing supposed to happen!" is not at all valid as it was also not supposed to crash with the scenario you described but it did...
    the solution you provided would support my theory that the old handler is still active eevn though the window does not exist anymore so it would crash if you put your finger on the reader while the window was already closed and not yet reopened.
    You are right, i don't think i tried how you suggested it, i just guessed but i think it would crash anyway, because as Kebo said, when i opened the Form it would create the handler but when i closed it i was just hiding it, not really disposing of it, and that would cause problems, the reason i didn't try is because the sensor is in my school, i only have the VB part with me, i can try if you want but you are correct!

  19. #19

    Thread Starter
    Member
    Join Date
    Jan 2015
    Posts
    39

    Re: [RESOLVED] Problems with Invoke | Serial Port Reading

    Quote Originally Posted by passel View Post
    If I was writing it, I would probably have also had the serial port processed where it could be opened once and left open.
    You say you don't want it to process if the child form wasn't open, which is fine. They had a term for that, called the "bit bucket". Also known as, "dropping it on the floor".
    If you didn't want to process the serial data, you would just read but not process it.
    If, on the other hand, you want to make the port available to others when the form is not displayed, then I would just Close the port when the form was closed, and reopen the port when the form was shown, but the actual serial port object (and its handler) could always stay available for the life of the program.
    Thanks, that makes sense ! I might change the code to fit your suggestion!

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