troubleshooting Question

MultiThreading in Classes

Avatar of wtconway
wtconway asked on
Visual Basic.NET
12 Comments1 Solution253 ViewsLast Modified:
I have a form which is doing authentication by Querying our Active Directory.  To validate and to fully log into the software can sometimes take up to 5 to 6 seconds.  I don't want the user to think that the application is hung and try to close or relogin so I decided to make small "MSN Messenger" style alert boxes appear in the bottom right of the screen.  My problem is that the boxes don't always appear until after the Active Directory Querying is completed and then you can see the box skip or quickly close itself.  My thought was to have these spawn from their own independent threads.  I quickly realized that you can't send parameters to a Sub procedure when creating the thread so I decided to put it in a class.  My problem is now the popup box won't appear at all.

My Class:
Imports System.Threading

Public Class BalloonThread

#Region "Privates"
    Private _Title As String
    Private _Text As String
    Private _TimeToShow As Integer
    Private _TimeToHide As Integer
    Private _TimeToStay As Integer
#End Region

#Region "Properties"
    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal Value As String)
            _Title = Value
        End Set
    End Property
    Public Property Text() As String
        Get
            Return Text
        End Get
        Set(ByVal Value As String)
            _Text = Value
        End Set
    End Property
    Public Property TimeToShow() As String
        Get
            Return _TimeToShow
        End Get
        Set(ByVal Value As String)
            _TimeToShow = Value
        End Set
    End Property
    Public Property TimeToHide() As String
        Get
            Return _TimeToHide
        End Get
        Set(ByVal Value As String)
            _TimeToHide = Value
        End Set
    End Property
    Public Property TimeToStay() As String
        Get
            Return _TimeToStay
        End Get
        Set(ByVal Value As String)
            _TimeToStay = Value
        End Set
    End Property


#End Region

#Region "Procedures"
    Public Sub Main()

    End Sub

    Public Sub Main(ByVal p_Title As String, _
        ByVal p_Text As String, _
        Optional ByVal p_TimeToShow As Integer = 300, _
        Optional ByVal p_TimeToHide As Integer = 300, _
        Optional ByVal p_TimeToStay As Integer = 5000)

        _Title = p_Title
        _Text = p_Text
        _TimeToShow = p_TimeToShow
        _TimeToHide = p_TimeToHide
        _TimeToStay = p_TimeToStay

    End Sub

    Public Overloads Sub Start()
        Dim thd As New Thread(AddressOf CreateThread)
        thd.Start()
    End Sub

    Public Overloads Sub Start(ByVal p_Title As String, _
        ByVal p_Text As String, _
        Optional ByVal p_TimeToShow As Integer = 300, _
        Optional ByVal p_TimeToHide As Integer = 300, _
        Optional ByVal p_TimeToStay As Integer = 5000)

        _Title = p_Title
        _Text = p_Text
        _TimeToShow = p_TimeToShow
        _TimeToHide = p_TimeToHide
        _TimeToStay = p_TimeToStay

        Dim thd As New Thread(AddressOf CreateThread)
        thd.Start()
    End Sub

    Private Sub CreateThread()
        Debug.WriteLine("Called CreateThread for " & _Title)
        Dim bal As New TNet.SysTrayBalloon(TNet.SysTrayBalloon.Skins.TNet_Default, _
                TNet.SysTrayBalloon.CloseButtons.TNet_Default)
        bal.Show(_Title, _Text, _TimeToShow, _TimeToStay, _TimeToHide)
    End Sub
#End Region
End Class

I am trying to create the balloon by doing this:
    Public bal As New BalloonThread
    Private Sub Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLogin.Click
    ...
            bal.Start("Logging In", "Attempting to login as " & UserName)
    ...
    End Sub

I realize that the thread can access the _Title because I see it in the Debug output.  I should note that the Balloon is actually a form.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros