Results 1 to 7 of 7

Thread: [RESOLVED] Code running in the background

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Resolved [RESOLVED] Code running in the background

    Hi Guys

    Is it possible to have code running in the background and still be able to use other parts of the program (i.e. firing other events).

    For example I have a instant messaging chat in my program and when the user has a new message then the display flashes from green to red as seen below

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Red
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Green
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Red
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Green
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Red
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Green
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Red
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Green
            System.Threading.Thread.Sleep(500)
            Me.dockMessages.PredefinedTabColor = DevComponents.DotNetBar.eTabItemColor.Red
    
    End Sub
    But I still want to be able to continue using the other functions in my program despite having this code running in the background (which highlights to the user that he has a new instant message by changing the display background colour)

    How can I overcome this?

    Thanks

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Code running in the background

    Put this in your form:

    Code:
    Private Delegate Sub SetPTabColor_Delegate(ByVal tc As DevComponents.DotNetBar.eTabItemColor)
    Private Sub SetPTabColor(ByVal tc As DevComponents.DotNetBar.eTabItemColor)
         Me.dockMessages.PredefinedTabColor = tc
    End Sub
    Private Sub MySub()
            Dim deleg As New SetPTabColor_Delegate(AddressOf SetPTabColor)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Red)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Green)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Red)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Green)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg, DevComponents.DotNetBar.eTabItemColor.Red)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Green)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Red)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Green)
            System.Threading.Thread.Sleep(500)
            Me.Invoke(deleg,DevComponents.DotNetBar.eTabItemColor.Red)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         Call (New Threading.Thread(AddressOf MySub) With {.IsBackground = True}).Start()
    End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code running in the background

    You should just use a Timer. In the Tick event handler, toggle the colour. After a specific number of Tick events, stop the Timer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    321

    Re: Code running in the background

    Wow thanks guys - its work a treat! But i admit i dont understand the first thing you did with the code that made it work. Is there anything you can tell me or point me to that will help me understand this and so i can learn for the future?

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Code running in the background

    It uses threading. Using the Call (New Threading.Thread(AddressOf MySub) With {.IsBackground = True}).Start(), you can make a Sub run on another thread, meaning it executes along with everything else. Your application stays responsive. The Me.Invoke(xxx) parts are to set the PredefinedTabColor property of your control using Delegates. (Google that, and you'll understand better than I can explain it.)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Code running in the background

    Multi-threading is unnecessary. There's no need to keep the UI thread responsive because what you're doing is intermittent, not constant. To do something intermittently you just use a Timer. The code in the Tick event handler executes very quickly, so the UI is never tied up so it remains responsive inherently. E.g.
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Const MAX_FLASH_COUNT As Integer = 10
    4.  
    5.     Private flashCount As Integer
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         Me.flashCount = 0
    9.         Me.Timer1.Start()
    10.     End Sub
    11.  
    12.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    13.         Dim backColor As Color
    14.  
    15.         If Me.flashCount = MAX_FLASH_COUNT Then
    16.             Me.Timer1.Stop()
    17.             backColor = TextBox.DefaultBackColor
    18.         ElseIf Me.TextBox1.BackColor = Color.Red Then
    19.             backColor = Color.Green
    20.         Else
    21.             backColor = Color.Red
    22.         End If
    23.  
    24.         Me.TextBox1.BackColor = backColor
    25.         Me.flashCount += 1
    26.     End Sub
    27.  
    28. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Code running in the background

    A Timer uses multi-threading in the background anyways. Threading in this case is much more straightforward.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width