[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
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
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.
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?
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.)
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:
Public Class Form1
Private Const MAX_FLASH_COUNT As Integer = 10
Private flashCount As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.flashCount = 0
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim backColor As Color
If Me.flashCount = MAX_FLASH_COUNT Then
Me.Timer1.Stop()
backColor = TextBox.DefaultBackColor
ElseIf Me.TextBox1.BackColor = Color.Red Then
backColor = Color.Green
Else
backColor = Color.Red
End If
Me.TextBox1.BackColor = backColor
Me.flashCount += 1
End Sub
End Class
Re: Code running in the background
A Timer uses multi-threading in the background anyways. Threading in this case is much more straightforward.