How to get threading to work? - vb.net

Here is the code:
Imports System.Threading
Partial Public Class frmTermsOfUse
Inherits Form
Public Sub New()
InitializeComponent()
rtxtContent.Text = My.Resources.TermsOfUse
End Sub
Private Shared [exit] As Boolean = True
Private Sub btnAccept_Click(sender As Object, e As EventArgs)
XMLSettings.WriteValue("ShowToU", (Not chkDontShowAgain.Checked).ToString())
[exit] = False
Me.Close()
End Sub
Private Sub frmTermsOfUse_Load(sender As Object, e As EventArgs)
lblToU.Left = (Me.Width \ 2) - (lblToU.Width / 2)
Dim t As New Thread(AddressOf Wait20Sec)
t.Start()
End Sub
Private Sub Wait20Sec()
For i As Integer = 19 To 0 Step -1
System.Threading.Thread.Sleep(1000)
Try
Me.Invoke(DirectCast(Sub() btnAccept.Text = "Accept (" & i & ")", MethodInvoker))
Catch
End Try
Next
Me.Invoke(DirectCast(Sub()
btnAccept.Text = "Accept"
btnAccept.Enabled = True
End Sub, MethodInvoker))
End Sub
Private Sub btnDecline_Click(sender As Object, e As EventArgs)
System.Diagnostics.Process.GetCurrentProcess().Kill()
End Sub
Private Sub frmTermsOfUse_FormClosing(sender As Object, e As FormClosingEventArgs)
If e.CloseReason = CloseReason.UserClosing AndAlso [exit] Then
System.Diagnostics.Process.GetCurrentProcess().Kill()
End If
End Sub
End Class
I don't get the countdown on the button to work.
Button is disabled when I launch the application and does nothing else.
How can I fix this?
Already thanks for the help

Related

How can I cancel a thread?

I do something in a thread. But sometimes I don't want to wait till all pings are finished.
How can I cancel a thread?
Can you show me please the code?
Private Sub Start_Button_Click(sender As Object, e As EventArgs) Handles Start_Button.Click
DoSomething()
End Sub
Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
THRD.Cancel '<-- Thread cancel!??!???
End Sub
Sub DoSomething()
Dim THRD As New Thread(Sub()
Dim IPArea As String = "192.168.1."
Dim LastIP As Integer
For LastIP = 0 To 255
Dim TestIP As String = IPArea & CStr(LastIP)
If My.Computer.Network.Ping(TestIP, 10) Then
ListBox1.Items.Add(TestIP)
End If
Next
End Sub)
THRD.IsBackground = True
THRD.Start()
End Sub
Here is my working solution and this solution is only to show how to move the THRD as a form level variable to allow stopping it when clicking the cancel button. I added some validations to prevent exceptions.
Public Class Form1
Private THRD As Threading.Thread
Private Sub Start_Button_Click(sender As Object, e As EventArgs) Handles Start_Button.Click
DoSomething()
End Sub
Private Sub Cancel_Button_Click(sender As Object, e As EventArgs) Handles Cancel_Button.Click
If THRD IsNot Nothing AndAlso THRD.IsAlive Then
THRD.Abort() '<-- Thread cancel!??!???
THRD = Nothing
AddToList("Stopped.")
Else
AddToList("Thread not running.")
End If
End Sub
Sub DoSomething()
If THRD IsNot Nothing AndAlso THRD.IsAlive Then
AddToList("Still working...")
Exit Sub
End If
THRD = New Threading.Thread(Sub()
Dim IPArea As String = "192.168.1."
Dim LastIP As Integer
For LastIP = 0 To 255
Dim TestIP As String = IPArea & CStr(LastIP)
If My.Computer.Network.Ping(TestIP, 10) Then
AddToList(TestIP)
End If
Next
AddToList("Done")
End Sub)
THRD.IsBackground = True
THRD.Start()
End Sub
''' <summary>
''' Thead-safe add value to list.
''' </summary>
''' <param name="value">The value.</param>
Private Sub AddToList(value As String)
If ListBox1.InvokeRequired Then
ListBox1.Invoke(Sub() ListBox1.Items.Add(value))
Else
ListBox1.Items.Add(value)
End If
End Sub
End Class

VB.Net Send output and input of process started in another form

I have 2 forms, MainForm and RCONForm.
What I am trying to do is to start a process when the programs starts and in this case I have choosen cmd.exe and it works.
The thing is I want to be able to read the output and send input onto the process from another form using 2 textboxes.
The problem is that I can't read anything from the process neither can I send any input to it either.
My MainForm:
Public Class MainForm
#Region "Import Function"
Dim Functions As New Functions
Friend WithEvents RCON As Process
#End Region
Friend Sub AppendOutputText(ByVal text As String)
If RCONForm.RCONLogText.InvokeRequired Then
Dim myDelegate As New RCONForm.AppendOutputTextDelegate(AddressOf AppendOutputText)
Me.Invoke(myDelegate, text)
Else
RCONForm.RCONLogText.AppendText(text)
End If
End Sub
Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RCON = New Process
With RCON.StartInfo
.FileName = "C:\Windows\system32\CMD.exe"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
RCON.Start()
RCON.BeginErrorReadLine()
RCON.BeginOutputReadLine()
AppendOutputText("RCON Started at: " & RCON.StartTime.ToString)
End Sub
Private Sub RCONButton_Click(sender As Object, e As EventArgs) Handles RCONButton.Click
Functions.KillOldForm()
Functions.SpawnForm(Of RCONForm)()
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Application.Exit()
End Sub
Private Sub ServerButton_Click(sender As Object, e As EventArgs) Handles ServerButton.Click
Functions.KillOldForm()
Functions.SpawnForm(Of ServerForm)()
End Sub
End Class
And my RCONForm:
Public Class RCONForm
Friend Delegate Sub AppendOutputTextDelegate(ByVal text As String)
Friend WithEvents RCON As Process = MainForm.RCON
Friend Sub RCON_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles RCON.ErrorDataReceived
MainForm.AppendOutputText(vbCrLf & "Error: " & e.Data)
End Sub
Friend Sub RCON_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles RCON.OutputDataReceived
MainForm.AppendOutputText(vbCrLf & e.Data)
End Sub
Friend Sub RCONCommandText_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RCONCommandText.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
MainForm.RCON.StandardInput.WriteLine(RCONCommandText.Text)
MainForm.RCON.StandardInput.Flush()
RCONCommandText.Text = ""
e.Handled = True
End If
End Sub
Friend Sub RCONForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
MainForm.RCON.StandardInput.WriteLine("EXIT") 'send an EXIT command to the Command Prompt
MainForm.RCON.StandardInput.Flush()
MainForm.RCON.Close()
End Sub
Private Sub RCONForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
My Functions:
#Region "Kill Old Forms"
Function KillOldForm()
While MainForm.SpawnPanel.Controls.Count > 0
MainForm.SpawnPanel.Controls(0).Dispose()
End While
Return Nothing
End Function
#End Region
#Region "Spawn Form"
Function SpawnForm(Of T As {New, Form})() As T
Dim spawn As New T() With {.TopLevel = False, .AutoSize = False}
Try
spawn.Dock = DockStyle.Fill
MainForm.SpawnPanel.Controls.Add(spawn)
spawn.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return Nothing
End Function
#End Region
I have been thinking of using threads and maybe that can solve the issue, or is there a more simple way?
You are using default form instances heavily. You should avoid that if possible. Here's a way to keep track of the proper instance of RCONForm in MainForm
Private myRCONForm As RCONForm
Private Sub RCONButton_Click(sender As Object, e As EventArgs) Handles RCONButton.Click
Functions.KillOldForm()
myRCONForm = Functions.SpawnForm(Of RCONForm)()
End Sub
Now SpawnForm is a function, and it would return the form so you can keep a reference to it
Public Function SpawnForm(Of T As {New, Form})() As T
Dim myForm = New T()
' add myForm to the appropriate Panel or whatever
Return myForm
End Function
Update all access to RCONForm with myRCONForm in MainForm
Also, this is a little flawed, where you check if invocation is required on RCONForm.RCONLogText, then invoke on Me. It can be simplified
' old with default form instance
Friend Sub AppendOutputText(ByVal text As String)
If RCONForm.RCONLogText.InvokeRequired Then
Dim myDelegate As New RCONForm.AppendOutputTextDelegate(AddressOf AppendOutputText)
Me.Invoke(myDelegate, text)
Else
RCONForm.RCONLogText.AppendText(text)
End If
End Sub
' new, with instance reference plus simplified invocation
Friend Sub AppendOutputText(text As String)
If myRCONForm.RCONLogText.InvokeRequired Then
myRCONForm.RCONLogText.Invoke(New Action(Of String)(AddressOf AppendOutputText), text)
Else
myRCONForm.RCONLogText.AppendText(text)
End If
End Sub

How to access richtextbox via different thread

Hey guys my question is how can I access (update/read) richtextbox in a thread.
I just created a very simple code for you to understand what I am doing. I searched some articles on internet mentioned about invoke, delegate or backgroundworker, hope someone can come and tell me which and how to use. Really thanks.
Imports System.Threading
Public Class form1
Dim flag As Boolean = True
Dim startbtn As Thread
Dim stopbtn As Thread
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
startbtn = New Thread(AddressOf startfuction)
startbtn.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
stopbtn = New Thread(AddressOf stopfunction)
stopbtn.Start()
End Sub
'************** thread 1
Private Sub startfuction()
flag = True
While flag = True
richtextbox1.text = "Your process started" 'error
End While
End Sub
'************** thread 2
Private Sub stopfunction()
flag = False
startbtn.Abort()
MsgBox("You ended the process")
End Sub
End Class
Imports System.Threading
Public Class Form1
Dim flag As Boolean = True
Dim startbtn As Thread
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
startbtn = New Thread(AddressOf startfuction)
startbtn.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
flag = False
MsgBox("You ended the process")
End Sub
'************** thread 1
Private Sub startfuction()
flag = True
While flag = True
Me.Invoke(Sub() RichTextBox1.Text = "Your process started") 'error
End While
Me.Invoke(Sub() RichTextBox1.Text = "Your process stopped") 'error
End Sub
End Class
EDIT 1
Also when running threads, When you goto close your app you could run into problems because your threads will end prematurely..
do something like
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
flag = False
Application.DoEvents()
End Sub

Random delay in seconds from numericupdown in VB.Net

I have a desktop winforms app code:
Sub Delay(ByVal dblSecs As Double)
Const OneSec As Double = 1.0# / (1440.0# * 60.0#)
Dim dblWaitTil As Date
Now.AddSeconds(OneSec)
dblWaitTil = Now.AddSeconds(OneSec).AddSeconds(dblSecs)
Do Until Now > dblWaitTil
Application.DoEvents()
Loop
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Webbrowser1.Navigate(TextBox1.Text)
Delay(Val(DelayText.Text))
end sub
What I need is to set max. delay, same from textbox by entering seconds.
I need this to be random delay number so Im stacking here, thanks for correcting my code.
Also If its possible to make it in NumericUpDown, as i found some topics which says textbox text property is different as NumericUpDown but i like it more.
You could use an inbetween class which does most of the work for you
This class would take the WebBrowser, attach to some events of it, and would refresh periodically (depending on MinimumWait / MaximumWait)
As it is using threading, it also checks if the usercontrol needs to be invoked to Refresh it and when yes, invokes the custom refresh delegate
Public Class Refresher
Protected Delegate Sub RefreshNavigationDelegate(browser As WebBrowser)
Protected Sub RefreshNavigation(browser As WebBrowser)
If browser.InvokeRequired Then
browser.Invoke(New RefreshNavigationDelegate(AddressOf RefreshNavigation), browser)
Return
End If
browser.Refresh(WebBrowserRefreshOption.Completely)
End Sub
Private _isBusy As Boolean = False
Public Property IsBusy As Boolean
Get
Return _isBusy
End Get
Protected Set(value As Boolean)
If _isBusy = value Then
Return
End If
_isBusy = value
End Set
End Property
Public Property MinimumWait As Integer = 2000
Public Property MaximumWait As Integer = 10000
Private refreshThread As Thread = Nothing
Private _browser As WebBrowser
Public Property Browser As WebBrowser
Get
Return _browser
End Get
Set(value As WebBrowser)
If Object.Equals(_browser, value) Then
Return
End If
StopRefresh()
If _browser IsNot Nothing Then
RemoveHandler Browser.DocumentCompleted, AddressOf DocumentComplete
RemoveHandler Browser.Navigating, AddressOf Navigating
End If
_browser = value
If _browser IsNot Nothing Then
AddHandler Browser.DocumentCompleted, AddressOf DocumentComplete
AddHandler Browser.Navigating, AddressOf Navigating
AddHandler Browser.ProgressChanged, AddressOf ProgressChanged
End If
StartRefresh()
End Set
End Property
Protected Sub ProgressChanged(sender As Object, e As WebBrowserProgressChangedEventArgs)
IsBusy = e.CurrentProgress > 0 AndAlso e.CurrentProgress < e.MaximumProgress
End Sub
Protected Sub DocumentComplete(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
IsBusy = False
End Sub
Protected Sub Navigating(sender As Object, e As WebBrowserNavigatingEventArgs)
IsBusy = True
End Sub
Public Sub StartRefresh()
If refreshThread IsNot Nothing Then
Return
End If
refreshThread = New Thread(AddressOf DoRandomRefreshes)
refreshThread.Start()
End Sub
Public Sub StopRefresh()
If refreshThread Is Nothing Then
Return
End If
refreshThread.Abort()
refreshThread = Nothing
End Sub
Protected Overridable Sub DoRandomRefreshes()
Dim randomGenerator As New Random()
While Not refreshThread.ThreadState = ThreadState.AbortRequested
Dim newTimeout As Integer = MinimumWait + randomGenerator.Next(MaximumWait - MinimumWait)
Thread.Sleep(newTimeout)
If Not IsBusy Then
RefreshNavigation(Browser)
End If
End While
End Sub
Public Sub New()
End Sub
End Class
You could then use it in your form as such:
Public Class Form1
Dim myRefresher As Refresher = New Refresher()
Private Sub tsbGo_Click(sender As Object, e As EventArgs) Handles tsbGo.Click
WebBrowser1.Navigate(txtUrl.Text)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myRefresher.Browser = WebBrowser1
End Sub
Private Sub txtMin_TextChanged(sender As Object, e As EventArgs) Handles txtMin.TextChanged
Dim int As Integer = 0
If Integer.TryParse(txtMin.Text, int) Then
myRefresher.MinimumWait = int
End If
End Sub
Private Sub txtMax_TextChanged(sender As Object, e As EventArgs) Handles txtMax.TextChanged
Dim int As Integer = 0
If Integer.TryParse(txtMax.Text, int) Then
myRefresher.MaximumWait = int
End If
End Sub
End Class

vb2012 textbox backcolor does't change using thread

I wrote very simple thread example.
Make normal form1 and drop 1 textbox
run thread work on form load
thread change a textbox backcolor looks like switch on/off
But, it doesn't work.....
Can you tell me why doesn't work??
see my source.
=====================================================================
Imports System.Threading
Public Class Monitor
Public wObj As Worker = New Worker()
Public MyThread As Thread = New Thread(AddressOf wObj.DoWork)
Public Sub ChangeTBColor(pOption As Integer)
If pOption = 1 Then
tb1.BackColor = Color.Aqua
Else
tb1.BackColor = Color.Red
End If
End Sub
Private Sub Monitor_Load(sender As Object, e As EventArgs) Handles Me.Load
MyThread.Start()
Console.WriteLine("Running OrgThread..")
Console.WriteLine("Stop running")
End Sub
Private Sub BtnThreadStop_Click(sender As Object, e As EventArgs) Handles BtnThreadStop.Click
Me.wObj.RequestStop()
End Sub
End Class
Public Class Worker
Private LoopStop As Boolean = True
Public Sub DoWork()
Console.WriteLine("User Thread Start!")
Dim iTemp As Integer = 0
While (LoopStop)
Monitor.ChangeTBColor(iTemp Mod 2)
iTemp = iTemp + 1
Thread.Sleep(500)
End While
Console.WriteLine("User Thread End.!")
End Sub
Public Sub RequestStop()
LoopStop = False
End Sub
End Class
As already mentioned, your starting a new thread for your work, the issue is your trying to change the color for a control that need invoking. With this said, we need a delegate for when the control needs to be invoked... In my example, I used one class that handles this all and works great. Also please make sure to import System.ComponentModel because this is needed for the BackgroundWorker... I used the background worker as it takes all the heavy lifting off you would need...
Imports System.ComponentModel
Imports System.Threading
Public Class Monitor
Delegate Sub SetColor(ByVal clr As Integer) 'Your delegate..
Private WithEvents bw As New BackgroundWorker
Public Sub ChangeTBColor(pOption As Integer)
If Me.tb1.InvokeRequired Then 'Invoke if required...
Dim d As New SetColor(AddressOf ChangeTBColor) 'Your delegate...
Me.Invoke(d, New Object() {pOption})
Else
If pOption = 1 Then
tb1.BackColor = Color.Aqua
Else
tb1.BackColor = Color.Red
End If
End If
End Sub
Private Sub Monitor_Load(sender As Object, e As EventArgs) Handles Me.Load
bw.WorkerSupportsCancellation = True
Console.WriteLine("Running OrgThread..")
bw.RunWorkerAsync()
End Sub
Private Sub bw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
Dim iTemp As Integer = 0
Dim LoopStop As Boolean = True
Console.WriteLine("User Thread Start!")
While (LoopStop)
If Not (bw.CancellationPending) Then
ChangeTBColor(iTemp Mod 2)
iTemp = iTemp + 1
Thread.Sleep(500)
Else
e.Cancel = True
LoopStop = False
End If
End While
End Sub
Private Sub bw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
Console.WriteLine("User Thread End.!")
End Sub
Private Sub BtnThreadStop_Click(sender As Object, e As EventArgs) Handles BtnThreadStop.Click
If bw.IsBusy Then
bw.CancelAsync()
Else
Console.WriteLine("Running OrgThread..")
bw.RunWorkerAsync()
End If
End Sub
End Class
Here's my screenshot of my test... This is tried and tested. Please be sure to vote if this helps you!