vb.net 3.5 + serialport cotnrol + Threads = help
-
hello to everyone, im new in .NET programming -and in multithreading- so if the answer of my question is to read more of multithreading just say it :) i try to make an application to communicate with a device through serial port, after a lot of reading i made it to communicate in a way i want, but..... now the problem, when i use a second form, and at this form i create a thread and through that thread i try to send a data through com i get an error that com port is closed! i try the .isAlive and in that thread the serial port looked closed. To be more specific IN form1 i use
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open() End Sub
IN form2 i use
Imports System.Threading
Public Class Form2
Dim thr As Thread Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Debug.Print(Form1.SerialPort1.IsOpen) thr = New Thread(AddressOf thrsub) thr.IsBackground = True thr.Start() End Sub Public Sub thrsub() Debug.Print(Form1.SerialPort1.IsOpen) End Sub
End Class
at the first Debug.Print(Form1.SerialPort1.IsOpen) if the Button1_click it gives TRUE but in the second Debug.Print(Form1.SerialPort1.IsOpen) in the thrsub it gives FALSE.... :sigh: any suggestions?
-
hello to everyone, im new in .NET programming -and in multithreading- so if the answer of my question is to read more of multithreading just say it :) i try to make an application to communicate with a device through serial port, after a lot of reading i made it to communicate in a way i want, but..... now the problem, when i use a second form, and at this form i create a thread and through that thread i try to send a data through com i get an error that com port is closed! i try the .isAlive and in that thread the serial port looked closed. To be more specific IN form1 i use
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open() End Sub
IN form2 i use
Imports System.Threading
Public Class Form2
Dim thr As Thread Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Debug.Print(Form1.SerialPort1.IsOpen) thr = New Thread(AddressOf thrsub) thr.IsBackground = True thr.Start() End Sub Public Sub thrsub() Debug.Print(Form1.SerialPort1.IsOpen) End Sub
End Class
at the first Debug.Print(Form1.SerialPort1.IsOpen) if the Button1_click it gives TRUE but in the second Debug.Print(Form1.SerialPort1.IsOpen) in the thrsub it gives FALSE.... :sigh: any suggestions?
I think the problem is not related to threads , it is something to do with creating proper instance of a class. When from Form2 you call Form.SerialPort1.IsOpen you are not creating an instance of Form1 which means Form1_Load never gets called , and hence the code inside the Form1_Load event is not called ,keeping the the port closed , in Form2 try this , just before Debug.Print(Form1.SerialPort1.IsOpen) statements put the following statements
Form1.Show()
Form1.Hide()We are forcing the Form1 to load. Let me know if this answers you question Feel free to ask if something is not clear
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
-
hello to everyone, im new in .NET programming -and in multithreading- so if the answer of my question is to read more of multithreading just say it :) i try to make an application to communicate with a device through serial port, after a lot of reading i made it to communicate in a way i want, but..... now the problem, when i use a second form, and at this form i create a thread and through that thread i try to send a data through com i get an error that com port is closed! i try the .isAlive and in that thread the serial port looked closed. To be more specific IN form1 i use
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open() End Sub
IN form2 i use
Imports System.Threading
Public Class Form2
Dim thr As Thread Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Debug.Print(Form1.SerialPort1.IsOpen) thr = New Thread(AddressOf thrsub) thr.IsBackground = True thr.Start() End Sub Public Sub thrsub() Debug.Print(Form1.SerialPort1.IsOpen) End Sub
End Class
at the first Debug.Print(Form1.SerialPort1.IsOpen) if the Button1_click it gives TRUE but in the second Debug.Print(Form1.SerialPort1.IsOpen) in the thrsub it gives FALSE.... :sigh: any suggestions?
Using the serial port instance on your Form1 instance from Form2 is bad practice. You also can't do it with the code you're written since Form2 has no idea that Form1 even exists. If the bulk of your serial port communication is going to happen from Form2, create the port and connection on Form2, not Form1. Also, if you dragged the SerialPort from the ToolBox onto your form, you can't use it from a second thread. The event's won't fire correctly because they depend on the main UI thread to work.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008