Saravanan_84
New member
- Joined
- Feb 18, 2011
- Messages
- 2
- Programming Experience
- Beginner
Dear all,
I am new to VB.net & I need some help with threads.
Basically wat i'm trying to do is continuously write numbers from 1 to 10000 using 1 thread & stop this using another thread. But it doesn't seem to happen.
I have pasted the code for your reference.
I am new to VB.net & I need some help with threads.
Basically wat i'm trying to do is continuously write numbers from 1 to 10000 using 1 thread & stop this using another thread. But it doesn't seem to happen.
I have pasted the code for your reference.
VB.NET:
Imports System.Threading.Thread
Imports System.Threading
Public Class Form1
Dim mainthread As Threading.Thread
Dim subthread1 As Threading.Thread
Dim subthread2 As Threading.Thread
Public Sub Main()
'''System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
mainthread = Thread.CurrentThread
subthread1 = New Thread(New ThreadStart(AddressOf Fun1))
subthread2 = New Thread(New ThreadStart(AddressOf Fun2))
subthread1.Start()
End Sub
Public Delegate Sub SetTextCallback(ByVal [text] As String)
Public Sub Fun1()
Dim i As Integer
Dim d As New SetTextCallback(AddressOf Fun1)
If Me.Write_Log_Output.InvokeRequired Then
Dim NewText As String = "Written by the thread."
Me.Invoke(d, New Object() {[NewText] + " (Invoke)"})
Else
For i = 0 To 10000
Me.Write_Log(Conversion.Str(i))
Next
End If
End Sub
Public Sub Fun2()
MsgBox("CANCELLED BY THE USER")
Environment.Exit(0)
Application.Exit()
End Sub
Private Sub Write_Log(ByVal str As String)
Write_Log_Output.Focus()
Write_Log_Output.AppendText(vbCrLf)
Write_Log_Output.AppendText(str & vbCrLf)
Write_Log_Output.SelectionStart = Write_Log_Output.Text.Length
Write_Log_Output.ScrollToCaret()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mainthread = New Thread(New ThreadStart(AddressOf Main))
mainthread.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
subthread2.Start()
End Sub
End Class
Last edited by a moderator: