Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Re: Invoke/Thread

Re: Invoke/Thread

Scheduled Pinned Locked Moved C#
question
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    myNameIsRon
    wrote on last edited by
    #1

    Hi, I'm not very knowledgeable on threading... I'm using Invoke to receive Serial Port data. The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form? private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { serialPortData = serialPort1.ReadExisting(); this.Invoke(new EventHandler(SerialPortEvent)); } thanks, Ron

    N M M 3 Replies Last reply
    0
    • M myNameIsRon

      Hi, I'm not very knowledgeable on threading... I'm using Invoke to receive Serial Port data. The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form? private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { serialPortData = serialPort1.ReadExisting(); this.Invoke(new EventHandler(SerialPortEvent)); } thanks, Ron

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      myNameIsRon wrote:

      The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form?

      If you have any active threads, it good to stop them while you close the form. You can do this in the Dispose method of your form.

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      M 1 Reply Last reply
      0
      • M myNameIsRon

        Hi, I'm not very knowledgeable on threading... I'm using Invoke to receive Serial Port data. The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form? private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { serialPortData = serialPort1.ReadExisting(); this.Invoke(new EventHandler(SerialPortEvent)); } thanks, Ron

        M Offline
        M Offline
        Mohammad Dayyan
        wrote on last edited by
        #3

        If you use myThread.IsBackground = true; the thread will close automatically after this.Close()

        M 1 Reply Last reply
        0
        • N N a v a n e e t h

          myNameIsRon wrote:

          The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form?

          If you have any active threads, it good to stop them while you close the form. You can do this in the Dispose method of your form.

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          M Offline
          M Offline
          myNameIsRon
          wrote on last edited by
          #4

          Thanks Navaneeth, What code would I use to stop an "this.Invoke()" thread? Ron

          1 Reply Last reply
          0
          • M Mohammad Dayyan

            If you use myThread.IsBackground = true; the thread will close automatically after this.Close()

            M Offline
            M Offline
            myNameIsRon
            wrote on last edited by
            #5

            Hi Mo, I'm using "this.Invoke()". Should I be using a different type of thread? If not, how do I stop "this.Invoke()"? Ron

            M 1 Reply Last reply
            0
            • M myNameIsRon

              Hi, I'm not very knowledgeable on threading... I'm using Invoke to receive Serial Port data. The form locks when I try and call "this.Close()". Do I have to remove/stop the Invoke thread before using "this.Close()" to automatically close the form? private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { serialPortData = serialPort1.ReadExisting(); this.Invoke(new EventHandler(SerialPortEvent)); } thanks, Ron

              M Offline
              M Offline
              Mark Churchill
              wrote on last edited by
              #6

              Control.Invoke puts a serialised delegate onto the windows message loop. How much work are you doing in your SerialPortEvent method? If you are doing enough work to block the UI up then you are probably blocking the serial library as well. Consider taking data in the DataRecieved event, placing it on a queue, and perhaps Monitor.Pulse a background thread and returning out the event handler immediately to return control to the serial port code. Then your background thread can consume the data events that are on the queue, and process them. Then when you want to make UI changes, you either use Form.Invoke, or .BeginInvoke to make the required changes.

              Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
              Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

              M 1 Reply Last reply
              0
              • M myNameIsRon

                Hi Mo, I'm using "this.Invoke()". Should I be using a different type of thread? If not, how do I stop "this.Invoke()"? Ron

                M Offline
                M Offline
                Mohammad Dayyan
                wrote on last edited by
                #7

                Hey Ron. I'm using IsBackground property for closing Thread with closing form.

                myNameIsRon wrote:

                I'm using "this.Invoke()". Should I be using a different type of thread? If not, how do I stop "this.Invoke()"?

                I don't know exactly. :-O

                1 Reply Last reply
                0
                • M Mark Churchill

                  Control.Invoke puts a serialised delegate onto the windows message loop. How much work are you doing in your SerialPortEvent method? If you are doing enough work to block the UI up then you are probably blocking the serial library as well. Consider taking data in the DataRecieved event, placing it on a queue, and perhaps Monitor.Pulse a background thread and returning out the event handler immediately to return control to the serial port code. Then your background thread can consume the data events that are on the queue, and process them. Then when you want to make UI changes, you either use Form.Invoke, or .BeginInvoke to make the required changes.

                  Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                  Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                  M Offline
                  M Offline
                  myNameIsRon
                  wrote on last edited by
                  #8

                  Thanks Mark, The SerialPortEvent calls on a Method to parse the c/c swipe data. Is Method run through the Invoke thread, or the main thread? private void SerialPortEvent(object sender, EventArgs e) { SwipeMethod(serialPortData); } Also, if I use ShowDialog() (instead of Show()) to open the Form, there is no issue with using this.Close(). Ron

                  M 1 Reply Last reply
                  0
                  • M myNameIsRon

                    Thanks Mark, The SerialPortEvent calls on a Method to parse the c/c swipe data. Is Method run through the Invoke thread, or the main thread? private void SerialPortEvent(object sender, EventArgs e) { SwipeMethod(serialPortData); } Also, if I use ShowDialog() (instead of Show()) to open the Form, there is no issue with using this.Close(). Ron

                    M Offline
                    M Offline
                    Mark Churchill
                    wrote on last edited by
                    #9

                    SerialPortEvent is executed on the UI thread. So SwipeMethod will run on that same thread. Not sure on the ShowDialog vs Show deal... but I haven't had very much coffee today ;) My worker thread/queue setup was based on the assumption you were processing bunches of data from a serial port. If you are popping a form with "Swipe CC now" then it might be worth looking at just using Delegate.BeginInvoke and IAsyncResult. This will use a thread from the pool.

                    Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                    Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                    M 1 Reply Last reply
                    0
                    • M Mark Churchill

                      SerialPortEvent is executed on the UI thread. So SwipeMethod will run on that same thread. Not sure on the ShowDialog vs Show deal... but I haven't had very much coffee today ;) My worker thread/queue setup was based on the assumption you were processing bunches of data from a serial port. If you are popping a form with "Swipe CC now" then it might be worth looking at just using Delegate.BeginInvoke and IAsyncResult. This will use a thread from the pool.

                      Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
                      Alpha release: Entanglar: Transparant multiplayer framework for .Net games.

                      M Offline
                      M Offline
                      myNameIsRon
                      wrote on last edited by
                      #10

                      Yes, I've been playing around some. I'm not sure if it's a bug in .NET 2.0. After the transaction is approved, I want to exit the Form with "this.Close()" but it locks the app. If I switch to BeginInvoke it works ok. Also, I can use Invoke, if I open the form with ShowDialog instead of Show. Useful link: avoid invoke Ron

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups