Yesterday I was making a progressbarForm (form with a progressbar on it) that run in it's own thread to show the user the progress a certain process was making. More or less like the IE download screen's. And just like the windows download screens I wanted to show the progress percentage in the taskbarbutton. In windowsforms the Me.text of the form is reflected in the taskbar button so I set that and run the form. Allthought the titlebar of the form changes like mad (it's ussually a quick process) the taskbar hardly changes and most of the times just shows up when all the fun is done.
So first thing I did was google an no surprise nothing much that came up. Except one that used some p/invoke to refersh the taskbar but that din't help It was still not showing fast enough. I was refreshing faster but not as fast as the titlebar of the form.
So then I made a testproject and I tried somethint like this
and noticed the taskbar button follow the titlebar like a puppy.
Then I changed the thread.sleep to 10 and noticed that the taskbar wasn't following anymore. So I guess the taskbar needs some time to update wich is well proven by this
[/code]
Private Delegate Sub updatetextCallback(ByVal Progress As Integer)
Public Sub updatetext(ByVal Progress As Integer)
If Me.InvokeRequired Then
Invoke(New updatetextCallback(AddressOf updatetext), New Object() {Progress})
Else
Me.Text = Progress & " %"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim thr As New Threading.Thread(AddressOf countthread)
thr.IsBackground = True
thr.Start()
End Sub
Private Sub countthread()
For x As Integer = 1 To 1000
updatetext(x)
Threading.Thread.Sleep(10)
If x Mod 100 = 1 Then
Threading.Thread.Sleep(100)
End If
Next
End Sub
[/code]
where you will see it hitting the 101% then 201% and so on.
I hope other people can come up with better solutions this seems a bit quick and dirty and it will slow down the thread. But not so that the user will notice.
Comments welcome and I hope this can help other users out there.
Christiaan Baes
Belgium
"My new site" - Me
So first thing I did was google an no surprise nothing much that came up. Except one that used some p/invoke to refersh the taskbar but that din't help It was still not showing fast enough. I was refreshing faster but not as fast as the titlebar of the form.
So then I made a testproject and I tried somethint like this
Code:
Private Delegate Sub updatetextCallback(ByVal Progress As Integer)
Public Sub updatetext(ByVal Progress As Integer)
If Me.InvokeRequired Then
Invoke(New updatetextCallback(AddressOf updatetext), New Object() {Progress})
Else
Me.Text = Progress & " %"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim thr As New Threading.Thread(AddressOf countthread)
thr.IsBackground = True
thr.Start()
End Sub
Private Sub countthread()
For x As Integer = 1 To 1000
updatetext(x)
Threading.Thread.Sleep(100)
Next
End Sub
and noticed the taskbar button follow the titlebar like a puppy.
Then I changed the thread.sleep to 10 and noticed that the taskbar wasn't following anymore. So I guess the taskbar needs some time to update wich is well proven by this
[/code]
Private Delegate Sub updatetextCallback(ByVal Progress As Integer)
Public Sub updatetext(ByVal Progress As Integer)
If Me.InvokeRequired Then
Invoke(New updatetextCallback(AddressOf updatetext), New Object() {Progress})
Else
Me.Text = Progress & " %"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim thr As New Threading.Thread(AddressOf countthread)
thr.IsBackground = True
thr.Start()
End Sub
Private Sub countthread()
For x As Integer = 1 To 1000
updatetext(x)
Threading.Thread.Sleep(10)
If x Mod 100 = 1 Then
Threading.Thread.Sleep(100)
End If
Next
End Sub
[/code]
where you will see it hitting the 101% then 201% and so on.
I hope other people can come up with better solutions this seems a bit quick and dirty and it will slow down the thread. But not so that the user will notice.
Comments welcome and I hope this can help other users out there.
Christiaan Baes
Belgium
"My new site" - Me