Question Reg help with threads

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.

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:
First up, maybe you should give your identifiers meaningful names, so that it's a bit clearer what's happening. It's no fun reading someone else's code when all the names are essentially meaningless.

As for the question, the issue is the fact that you are creating foreground threads. The one and only difference between foreground threads and background threads is that foreground threads prevent the process exiting and background threads don't. You need to set the IsBackground property of your Thread object to True before calling Start to make it a background thread.
 
Dear jmcilhinney,

Thanks for your reply.

I did whatever you told. But still it doesn't work.

subthread1 = New Thread(New ThreadStart(AddressOf Fun1))
subthread2 = New Thread(New ThreadStart(AddressOf Fun2))

subthread1.IsBackground = True
subthread1.Start()
As you can see in the snapshot that I have attached, clicking on the button "ABORT" doesn't respond (to trigger thread2- which closes the application).

Thread.JPG
 

Attachments

  • Thread.JPG
    Thread.JPG
    94.2 KB · Views: 24
I just had a closer look at your code. The actual problem is that you're not actually doing anything useful on subthread1. You enter the Fun1 method and you immediately delegate back to the UI thread, so your For loop is run in its entirety on the UI thread. As such, your Abort Button won't raise its Click event until the UI thread is free, which is after the entire loop is done.

If you're going to create a secondary thread then do your work on that thread. You ONLY delegate back to the UI thread to update the UI. That means that your For loop must be executed on the secondary thread and then you delegate back to the UI once each iteration. If your loop does 10,000 iterations then you will delegate to the UI thread 10,000 times. That doesn't make much sense because this is such a contrived situation but in a real app, where you are processing some data and then updating the UI afterwards, it would make more sense.
 
Back
Top