Results 1 to 6 of 6

Thread: [RESOLVED] Button to turn program on / off

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Resolved [RESOLVED] Button to turn program on / off

    Hello,

    First of all I want to thank you guys for all the support. This forum is great!
    I had another topic about telnet data and you guys help me out to finish the project.

    Now I still have one issue. I want to use 1 button to start and stop the program.
    I tried this with this code but it seems that the moment I press the button (ButtonStart) the program is ON
    but I cant put it OFF again. Also the program is working fine but I cant fully close the program as well. It's like it's running but the form is bugged..

    Any ideas?

    Code:
    Public Class Form1
    
    
        Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
            If ButtonStart.Text = "OFF" Then
                ButtonStart.Text = "ON"
                ButtonStart.BackColor = Color.FromArgb(153, 180, 209)
    
                Dim session As New System.Net.Sockets.TcpClient
                Dim networkstream As Net.Sockets.NetworkStream
                Dim bytes(session.ReceiveBufferSize) As Byte
                Dim monitor As String
                Dim Caliope As Integer
    
                Dim Ipadress As String
                Ipadress = TextBoxIP.Text
    
                Dim Program As String
                Program = TextBoxProgram.Text
    
                Dim TelnetWaarde1 As String
                TelnetWaarde1 = TextBoxTelnet1.Text
                Dim TelnetWaarde2 As String
                TelnetWaarde2 = TextBoxTelnet2.Text
                Dim TelnetWaarde3 As String
                TelnetWaarde3 = TextBoxTelnet3.Text
                Dim TelnetWaarde4 As String
                TelnetWaarde4 = TextBoxTelnet4.Text
                Dim TelnetWaarde5 As String
                TelnetWaarde5 = TextBoxTelnet5.Text
                Dim TelnetWaarde6 As String
                TelnetWaarde6 = TextBoxTelnet6.Text
    
                Dim KeyboardToets1 As String
                KeyboardToets1 = TextBoxKeyboard1.Text
                Dim KeyboardToets2 As String
                KeyboardToets2 = TextBoxKeyboard2.Text
                Dim KeyboardToets3 As String
                KeyboardToets3 = TextBoxKeyboard3.Text
                Dim KeyboardToets4 As String
                KeyboardToets4 = TextBoxKeyboard4.Text
                Dim KeyboardToets5 As String
                KeyboardToets5 = TextBoxKeyboard5.Text
                Dim KeyboardToets6 As String
                KeyboardToets6 = TextBoxKeyboard6.Text
    
                Caliope = Shell(Program, AppWinStyle.NormalFocus)
                session.Connect(Ipadress, 4010)
    
                If session.Connected Then
    
    
                    Debug.Print("Connected")
    
    
                    Do While True
                        If session.GetStream.CanRead Then
                            networkstream = session.GetStream
                            networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))
                            monitor = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
    
                            Debug.Print(monitor)
    
                            If monitor.StartsWith(TelnetWaarde1) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets1, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde2) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets2, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde3) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets3, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde4) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets4, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde5) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets5, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde6) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets6, True)
                            End If
    
    
                        End If
    
                    Loop
    
                Else
                    Debug.Print("Disconnected")
                    MessageBox.Show("Error, contact TVV Sound Joris for more information, joris@tvvsound.be")
                End If
    
    
    
    
            ElseIf ButtonStart.Text = "ON" Then
                ButtonStart.Text = "OFF"
                ButtonStart.BackColor = Color.DimGray
    
    
            End If
        End Sub
    
        Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
            My.Settings.Reset()
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            My.Settings.Save()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBoxProgram.TextChanged
    
        End Sub
    End Class

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

    Re: Button to turn program on / off

    Since you have an endless loop of processing in your button click event, nothing else can happen in your form GUI related.
    The button should just kick off the processing in a Background thread of some sort.
    And in the background thread, rather than unconditional loop (Do While True), you should have a conditional loop, for example (Do While Running) where Running is a boolean value that is set before starting the background process, and can be set to false by your Button event handler to exit the loop and clean up and exit the background process.

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Button to turn program on / off

    Quote Originally Posted by JorisS View Post
    Hello,

    First of all I want to thank you guys for all the support. This forum is great!
    I had another topic about telnet data and you guys help me out to finish the project.

    Now I still have one issue. I want to use 1 button to start and stop the program.
    I tried this with this code but it seems that the moment I press the button (ButtonStart) the program is ON
    but I cant put it OFF again. Also the program is working fine but I cant fully close the program as well. It's like it's running but the form is bugged..

    Any ideas?

    Code:
    Public Class Form1
    
    
        Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
            If ButtonStart.Text = "OFF" Then
                ButtonStart.Text = "ON"
                ButtonStart.BackColor = Color.FromArgb(153, 180, 209)
    
                Dim session As New System.Net.Sockets.TcpClient
                Dim networkstream As Net.Sockets.NetworkStream
                Dim bytes(session.ReceiveBufferSize) As Byte
                Dim monitor As String
                Dim Caliope As Integer
    
                Dim Ipadress As String
                Ipadress = TextBoxIP.Text
    
                Dim Program As String
                Program = TextBoxProgram.Text
    
                Dim TelnetWaarde1 As String
                TelnetWaarde1 = TextBoxTelnet1.Text
                Dim TelnetWaarde2 As String
                TelnetWaarde2 = TextBoxTelnet2.Text
                Dim TelnetWaarde3 As String
                TelnetWaarde3 = TextBoxTelnet3.Text
                Dim TelnetWaarde4 As String
                TelnetWaarde4 = TextBoxTelnet4.Text
                Dim TelnetWaarde5 As String
                TelnetWaarde5 = TextBoxTelnet5.Text
                Dim TelnetWaarde6 As String
                TelnetWaarde6 = TextBoxTelnet6.Text
    
                Dim KeyboardToets1 As String
                KeyboardToets1 = TextBoxKeyboard1.Text
                Dim KeyboardToets2 As String
                KeyboardToets2 = TextBoxKeyboard2.Text
                Dim KeyboardToets3 As String
                KeyboardToets3 = TextBoxKeyboard3.Text
                Dim KeyboardToets4 As String
                KeyboardToets4 = TextBoxKeyboard4.Text
                Dim KeyboardToets5 As String
                KeyboardToets5 = TextBoxKeyboard5.Text
                Dim KeyboardToets6 As String
                KeyboardToets6 = TextBoxKeyboard6.Text
    
                Caliope = Shell(Program, AppWinStyle.NormalFocus)
                session.Connect(Ipadress, 4010)
    
                If session.Connected Then
    
    
                    Debug.Print("Connected")
    
    
                    Do While True
                        If session.GetStream.CanRead Then
                            networkstream = session.GetStream
                            networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))
                            monitor = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
    
                            Debug.Print(monitor)
    
                            If monitor.StartsWith(TelnetWaarde1) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets1, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde2) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets2, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde3) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets3, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde4) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets4, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde5) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets5, True)
                            End If
    
                            If monitor.StartsWith(TelnetWaarde6) Then
                                AppActivate(Caliope)
                                My.Computer.Keyboard.SendKeys(KeyboardToets6, True)
                            End If
    
    
                        End If
    
                    Loop
    
                Else
                    Debug.Print("Disconnected")
                    MessageBox.Show("Error, contact TVV Sound Joris for more information, joris@tvvsound.be")
                End If
    
    
    
    
            ElseIf ButtonStart.Text = "ON" Then
                ButtonStart.Text = "OFF"
                ButtonStart.BackColor = Color.DimGray
    
    
            End If
        End Sub
    
        Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
            My.Settings.Reset()
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            My.Settings.Save()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBoxProgram.TextChanged
    
        End Sub
    End Class
    A few comments on your code:
    1. Wouldn't you be better off using a Boolean variable (shared within the entire class) instead of a control's text property? (e.g. ButtonStart.Text = "OFF")?
    2. I see multiple instances of variables being declared followed by a line that assigns a value to them. Did you know vb.net allows assigning a value upon declaration?
    Instead of:
    Dim variable As type
    variable = value
    Use:
    Dim variable As type = value
    3. I see you're using foreign words (Dutch) in your variable names. You might want to explain that "waarde" means "value". Or use different names.
    4. "TelnetWaarde1"-"TelnetWaarde6"? Why not use an array called "TelnetWaarde"? Same goes for other variables using a similar naming scheme.
    Last edited by Peter Swinkels; Feb 7th, 2019 at 05:23 AM. Reason: typo

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

    Re: Button to turn program on / off

    Something along these lines is what I'm suggesting. Can't really test it.
    Code:
    Imports System.Threading
    
    Public Class Form1
        Private monitorThread As Thread
        Private Running As Boolean
    
        Private Ipadress As String
        Private Program As String
        Private TelnetWaarde1 As String
        Private TelnetWaarde2 As String
        Private TelnetWaarde3 As String
        Private TelnetWaarde4 As String
        Private TelnetWaarde5 As String
        Private TelnetWaarde6 As String
        Private KeyboardToets1 As String
        Private KeyboardToets2 As String
        Private KeyboardToets3 As String
        Private KeyboardToets4 As String
        Private KeyboardToets5 As String
        Private KeyboardToets6 As String
    
        Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
            If ButtonStart.Text = "OFF" Then
                ButtonStart.Text = "ON"
                ButtonStart.BackColor = Color.FromArgb(153, 180, 209)
    
                Ipadress = TextBoxIP.Text
                Program = TextBoxProgram.Text
                TelnetWaarde1 = TextBoxTelnet1.Text
                TelnetWaarde2 = TextBoxTelnet2.Text
                TelnetWaarde3 = TextBoxTelnet3.Text
                TelnetWaarde4 = TextBoxTelnet4.Text
                TelnetWaarde5 = TextBoxTelnet5.Text
                TelnetWaarde6 = TextBoxTelnet6.Text
    
                KeyboardToets1 = TextBoxKeyboard1.Text
                KeyboardToets2 = TextBoxKeyboard2.Text
                KeyboardToets3 = TextBoxKeyboard3.Text
                KeyboardToets4 = TextBoxKeyboard4.Text
                KeyboardToets5 = TextBoxKeyboard5.Text
                KeyboardToets6 = TextBoxKeyboard6.Text
    
                Running = True
                monitorThread = New Thread(AddressOf MonitorLoop)
                monitorThread.IsBackground = True
                monitorThread.Start()
    
            ElseIf ButtonStart.Text = "ON" Then
                ButtonStart.Text = "OFF"
                ButtonStart.BackColor = Color.DimGray
                Running = False
    
            End If
        End Sub
    
        Private Sub MonitorLoop()
            Dim session As New System.Net.Sockets.TcpClient
            Dim networkstream As Net.Sockets.NetworkStream = Nothing
            Dim bytes(session.ReceiveBufferSize) As Byte
            Dim monitor As String
            Dim Caliope As Integer
    
            Caliope = Shell(Program, AppWinStyle.NormalFocus)
            session.Connect(Ipadress, 4010)
    
            If session.Connected Then
    
                Debug.Print("Connected")
    
                Do While Running
                    If session.GetStream.CanRead Then
                        networkstream = session.GetStream
                        networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))
                        monitor = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
    
                        Debug.Print(monitor)
    
                        If monitor.StartsWith(TelnetWaarde1) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets1, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde2) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets2, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde3) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets3, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde4) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets4, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde5) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets5, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde6) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets6, True)
                        End If
    
                    End If
    
                Loop
                networkstream.Dispose()
                session.Close()
    
            Else
                Debug.Print("Disconnected")
                MessageBox.Show("Error, contact TVV Sound Joris for more information, joris@tvvsound.be")
            End If
    
        End Sub
    
    End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    12

    Re: Button to turn program on / off

    Quote Originally Posted by passel View Post
    Something along these lines is what I'm suggesting. Can't really test it.
    Code:
    Imports System.Threading
    
    Public Class Form1
        Private monitorThread As Thread
        Private Running As Boolean
    
        Private Ipadress As String
        Private Program As String
        Private TelnetWaarde1 As String
        Private TelnetWaarde2 As String
        Private TelnetWaarde3 As String
        Private TelnetWaarde4 As String
        Private TelnetWaarde5 As String
        Private TelnetWaarde6 As String
        Private KeyboardToets1 As String
        Private KeyboardToets2 As String
        Private KeyboardToets3 As String
        Private KeyboardToets4 As String
        Private KeyboardToets5 As String
        Private KeyboardToets6 As String
    
        Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
            If ButtonStart.Text = "OFF" Then
                ButtonStart.Text = "ON"
                ButtonStart.BackColor = Color.FromArgb(153, 180, 209)
    
                Ipadress = TextBoxIP.Text
                Program = TextBoxProgram.Text
                TelnetWaarde1 = TextBoxTelnet1.Text
                TelnetWaarde2 = TextBoxTelnet2.Text
                TelnetWaarde3 = TextBoxTelnet3.Text
                TelnetWaarde4 = TextBoxTelnet4.Text
                TelnetWaarde5 = TextBoxTelnet5.Text
                TelnetWaarde6 = TextBoxTelnet6.Text
    
                KeyboardToets1 = TextBoxKeyboard1.Text
                KeyboardToets2 = TextBoxKeyboard2.Text
                KeyboardToets3 = TextBoxKeyboard3.Text
                KeyboardToets4 = TextBoxKeyboard4.Text
                KeyboardToets5 = TextBoxKeyboard5.Text
                KeyboardToets6 = TextBoxKeyboard6.Text
    
                Running = True
                monitorThread = New Thread(AddressOf MonitorLoop)
                monitorThread.IsBackground = True
                monitorThread.Start()
    
            ElseIf ButtonStart.Text = "ON" Then
                ButtonStart.Text = "OFF"
                ButtonStart.BackColor = Color.DimGray
                Running = False
    
            End If
        End Sub
    
        Private Sub MonitorLoop()
            Dim session As New System.Net.Sockets.TcpClient
            Dim networkstream As Net.Sockets.NetworkStream = Nothing
            Dim bytes(session.ReceiveBufferSize) As Byte
            Dim monitor As String
            Dim Caliope As Integer
    
            Caliope = Shell(Program, AppWinStyle.NormalFocus)
            session.Connect(Ipadress, 4010)
    
            If session.Connected Then
    
                Debug.Print("Connected")
    
                Do While Running
                    If session.GetStream.CanRead Then
                        networkstream = session.GetStream
                        networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))
                        monitor = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
    
                        Debug.Print(monitor)
    
                        If monitor.StartsWith(TelnetWaarde1) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets1, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde2) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets2, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde3) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets3, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde4) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets4, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde5) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets5, True)
                        End If
    
                        If monitor.StartsWith(TelnetWaarde6) Then
                            AppActivate(Caliope)
                            My.Computer.Keyboard.SendKeys(KeyboardToets6, True)
                        End If
    
                    End If
    
                Loop
                networkstream.Dispose()
                session.Close()
    
            Else
                Debug.Print("Disconnected")
                MessageBox.Show("Error, contact TVV Sound Joris for more information, joris@tvvsound.be")
            End If
    
        End Sub
    
    End Class
    Thanks a lot!
    This works perfect!
    Good to see how you did this, going to use same setup for the rest of my programs.

    Also thanks to the rest to give me tips to make my code easer.
    I have only been working with VB.net since last week and do not have any training in programming. I am already very happy that thanks to the help of you guys, I can get so far. I learn every minute!

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

    Re: Button to turn program on / off

    A separate running thread can't access the GUI controls directly, which is why the textboxes text were copied to strings that were visible (in process scope) to both the button_click sub and the sub (MonitorLoop) being run running on the background thread.

    You should be aware that if you have more complicated class instances that you may be accessing from both the GUI thread and other threads, that such objects can't be access by more than one thread at the same time. You can get an "object busy" type of exception (not the real wording of the exception). In cases where this might happen, you need to provide synchronization access control to the object to insure that only one thread is using the object at a time. A common method is to use a SyncLock object.

    This is more for information at this point. With the above code, the only variable that might be in use at the same time currently is the boolean variable named Running. Since this is a simple type, I don't think that you would get a conflict between two threads accessing that variable simultaneously.
    Last edited by passel; Feb 7th, 2019 at 05:19 AM.

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