Re: Invoke/Thread
-
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
-
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
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
-
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
If you use
myThread.IsBackground = true;
the thread will close automatically afterthis.Close()
-
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
Thanks Navaneeth, What code would I use to stop an "this.Invoke()" thread? Ron
-
If you use
myThread.IsBackground = true;
the thread will close automatically afterthis.Close()
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
-
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
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. -
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
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
-
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.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
-
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
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. -
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.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