0

Hi,

I made a simple Form1, with Text.Box1 (starting value=1) and Text.Box2 (starting value = 100000), as shown in the attachment :

I wanted that, when the Form loads,value of Text.Box1 keeps on increasing by 2 and value of Text.Box2 keeps on

decreasing by 3. I wanted to achieve this by multi-threading.

Following is the code I wrote, which definitely has some problem with it.
*************************************************************

Imports System.Threading.Thread

Public Delegate Sub funcHolder(ByVal diff1234 As Integer)

Public Class Form1
    Dim i As Integer
    Dim j As Integer
'-------------------------------------------------------------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim upd12Holder As funcHolder
        Dim upd34Holder As funcHolder
        upd12Holder = AddressOf upd12
        upd34Holder = AddressOf upd34
        Dim t12 As New System.Threading.Thread(AddressOf upd12)
        t12.Start(2)
        Dim t34 As New System.Threading.Thread(AddressOf upd12)
        t34.Start(3)
    End Sub
'-------------------------------------------------------------
    Public Sub upd12(ByVal diff12 As Integer)
        While (1)
            TextBox1.Text = TextBox1.Text + diff12
            For i = 1 To 10000          'delay loop
            Next i
        End While
    End Sub
'-------------------------------------------------------------
    Public Sub upd34(ByVal diff34 As Integer)
        While (1)
            TextBox2.Text = TextBox2.Text - diff34
            For j = 1 To 10000          'delay loop
            Next j
        End While
    End Sub
'-------------------------------------------------------------
End Class

*************************************************************

It would be very helpful if you could help me identify and correct the problem.

Attachments Form1.GIF 7.79 KB
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by GeekByChoiCe
0

i wasn'T sure what this "While (1)" is ... anyway here is a working code...

Imports System.Threading.Thread

Public Class Form1

	Private Delegate Function getCurrent(ByVal txtBox As TextBox) As Integer
	Private Delegate Sub setCurrent(ByVal txtBox As TextBox, ByVal i As Integer, ByVal increase As Boolean)
	'-------------------------------------------------------------
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim t12 As New System.Threading.Thread(AddressOf upd12) With {.IsBackground = True}
		t12.Start(2)
		Dim t34 As New System.Threading.Thread(AddressOf upd34) With {.IsBackground = True}
		t34.Start(3)
	End Sub

	Public Sub upd12(ByVal diff12 As Object)
		While (CInt(TextBox1.Invoke(New getCurrent(AddressOf GetNumber), TextBox1)) < 100000)  'check if number is < 1000000
			TextBox1.Invoke(New setCurrent(AddressOf SetNumber), TextBox1, CInt(diff12), True)
			Threading.Thread.Sleep(500) 'delay in ms
		End While
	End Sub

	Public Sub upd34(ByVal diff34 As Object)
		While (CInt(TextBox2.Invoke(New getCurrent(AddressOf GetNumber), TextBox2)) > 1)  'check if number is > 1
			TextBox2.Invoke(New setCurrent(AddressOf SetNumber), TextBox2, CInt(diff34), False)
			Threading.Thread.Sleep(500) 'delay in ms
		End While
	End Sub

	Private Function GetNumber(ByVal txtBox As TextBox) As Integer
		Return CInt(txtBox.Text)
	End Function

	Private Sub SetNumber(ByVal txtBox As TextBox, ByVal i As Integer, ByVal increase As Boolean)
		txtBox.Text = If(increase, CStr(CInt(txtBox.Text) + i), CStr(CInt(txtBox.Text) - i))
	End Sub

End Class

Edited by GeekByChoiCe: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.