TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > VB.NET

Notices

Progress bar update from worker thread

VB.NET


Reply
 
Thread Tools Display Modes
Old 05-12-2007, 03:32 AM   #1
dgleeson3@eircom.net
Guest
 
Posts: n/a
Progress bar update from worker thread

Hello all

Yes I know its been done before, but something silly is killing me on
this.

I have the standard progress bar and worker thread scenario with
progress of the worker thread being fed back to the main UI and
displayed on the progress bar. I have a delegate and am using
BeginInvoke. But the progress bar is not updating.

I have simplified my code below(and also built and tested this code).
Basic form1 with progress bar and button nothing else.
--------------------------------------------------------------------------------------------
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Threading
Imports System.IO


Public Class Form1

Dim worker As System.Threading.Thread ' Thread
Dim worker_obj As New worker_class ' Object from
worker class


Public Sub New()
Me.InitializeComponent()
'ProgressBar1
ProgressBar1.Name = "ProgressBar1"
ProgressBar1.Maximum = 100
ProgressBar1.Value = 10
End Sub

Sub UpdateProgressDisplay(ByVal Value_for_progress_bar As Integer)
ProgressBar1.Value = Value_for_progress_bar
Application.DoEvents()
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

worker = New Thread(AddressOf
worker_obj.DoRemoteCommunications)

' Now actually start the comms thread.
worker.Start()
End Sub
End Class

Public Class worker_class

' Set up delegate for assync function call.
--------------------------------
Public Delegate Sub Async_Update_Progress_caller(ByVal
Value_for_progress_bar As Integer)


Public Sub DoRemoteCommunications()
Console.WriteLine("Comms worker thread started.....")

' Set up delegate to allow asynchronous calls between threads.
Dim caller As New Async_Update_Progress_caller(AddressOf
Form1.UpdateProgressDisplay)

' Initiate the asynchronous call.
Dim result As IAsyncResult

result = caller.BeginInvoke(100, Nothing, Nothing)
Console.WriteLine("Progress value 25 passed ")
Thread.Sleep(3000)


Console.WriteLine("Comms worker thread Finished.")

End Sub

End Class

--------------------------------------------------------------------------------------------


Many thanks for any help.

Denis
_______________________

  Reply With Quote
Old 05-12-2007, 03:33 AM   #2
Mattias Sjögren
Guest
 
Posts: n/a
Re: Progress bar update from worker thread

>I have the standard progress bar and worker thread scenario with
>progress of the worker thread being fed back to the main UI and
>displayed on the progress bar. I have a delegate and am using
>BeginInvoke. But the progress bar is not updating.


You should use Control.BeginInvoke on a control or form, not
Delegate.BeginInvoke.



Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
|
Please reply only to the newsgroup.
  Reply With Quote
Old 05-12-2007, 04:50 PM   #3
dgleeson3@eircom.net
Guest
 
Posts: n/a
Re: Progress bar update from worker thread

Hi Mattias

Thanks for your help.

Ok Im not familiar with control.begininvoke.

Ive changed the worker class as below using

Form1.ProgressBar1.BeginInvoke(caller)

Now Im getting the following run time error.
"Invoke or BeginInvoke cannot be called on a control until the window
handle has been created."

But Form1.ProgressBar1 is on the form. Im confused.

--------------------------------------------------------------------------------------------------

Public Class worker_class

' Set up delegate for assync function call.
--------------------------------
Public Delegate Sub Async_Update_Progress_caller(ByVal
Value_for_progress_bar As Integer)


Public Sub DoRemoteCommunications()
Console.WriteLine("Comms worker thread started.....")

' Set up delegate to allow asynchronous calls between threads.
Dim caller As New Async_Update_Progress_caller(AddressOf
Form1.UpdateProgressDisplay)

' Initiate the asynchronous call.
Dim result As IAsyncResult

result = Form1.ProgressBar1.BeginInvoke(caller)
Console.WriteLine("Progress value 25 passed ")
Thread.Sleep(3000)


Console.WriteLine("Comms worker thread Finished.")

End Sub

End Class
----------------------------------------------------------------------------------------------------------



Thanks for your help

Denis


On Dec 4, 9:45 pm, Mattias Sjögren <>
wrote:
> >I have the standardprogressbarandworkerthreadscenario with
> >progressof theworkerthreadbeing fed back to the main UI and
> >displayed on theprogressbar. I have a delegate and am using
> >BeginInvoke. But theprogressbaris not updating.

>
> You should use Control.BeginInvoke on a control or form, not
> Delegate.BeginInvoke.
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|
> Please reply only to the newsgroup.


  Reply With Quote
Old 05-12-2007, 08:09 PM   #4
Phill W.
Guest
 
Posts: n/a
Re: Progress bar update from worker thread

Mattias Sjögren wrote:
>> I have the standard progress bar and worker thread scenario with
>> progress of the worker thread being fed back to the main UI and
>> displayed on the progress bar. I have a delegate and am using
>> BeginInvoke. But the progress bar is not updating.


Have you come across the BackgroundWorker class yet? (new to Framework
2.0, IIRC). It is specifically designed for this situation, marshaling
events from the worker thread back onto the UI thread and getting round
all these marshaling problems.

HTH,
Phill W.
  Reply With Quote
Old 06-12-2007, 09:18 AM   #5
surturz
Guest
 
Posts: n/a
Re: Progress bar update from worker thread

I've not really mucked around with threads much, but why not just use
..DoEvents within the processing loop?

-SurturZ



"Phill W." wrote:

> Mattias Sjögren wrote:
> >> I have the standard progress bar and worker thread scenario with
> >> progress of the worker thread being fed back to the main UI and
> >> displayed on the progress bar. I have a delegate and am using
> >> BeginInvoke. But the progress bar is not updating.

>
> Have you come across the BackgroundWorker class yet? (new to Framework
> 2.0, IIRC). It is specifically designed for this situation, marshaling
> events from the worker thread back onto the UI thread and getting round
> all these marshaling problems.
>
> HTH,
> Phill W.
>

  Reply With Quote
Old 15-12-2007, 04:11 AM   #6
Rick Gatewood
Guest
 
Posts: n/a
Re: Progress bar update from worker thread

I am having the same problem. I cannot update the progress bar. How does one
use control.begininvoke? Do I need to create another delegate which ties in
directly to the control? If so, does anyone know how to do this? Please help.

"" wrote:

> Hi Mattias
>
> Thanks for your help.
>
> Ok Im not familiar with control.begininvoke.
>
> Ive changed the worker class as below using
>
> Form1.ProgressBar1.BeginInvoke(caller)
>
> Now Im getting the following run time error.
> "Invoke or BeginInvoke cannot be called on a control until the window
> handle has been created."
>
> But Form1.ProgressBar1 is on the form. Im confused.
>
> --------------------------------------------------------------------------------------------------
>
> Public Class worker_class
>
> ' Set up delegate for assync function call.
> --------------------------------
> Public Delegate Sub Async_Update_Progress_caller(ByVal
> Value_for_progress_bar As Integer)
>
>
> Public Sub DoRemoteCommunications()
> Console.WriteLine("Comms worker thread started.....")
>
> ' Set up delegate to allow asynchronous calls between threads.
> Dim caller As New Async_Update_Progress_caller(AddressOf
> Form1.UpdateProgressDisplay)
>
> ' Initiate the asynchronous call.
> Dim result As IAsyncResult
>
> result = Form1.ProgressBar1.BeginInvoke(caller)
> Console.WriteLine("Progress value 25 passed ")
> Thread.Sleep(3000)
>
>
> Console.WriteLine("Comms worker thread Finished.")
>
> End Sub
>
> End Class
> ----------------------------------------------------------------------------------------------------------
>
>
>
> Thanks for your help
>
> Denis
>
>
> On Dec 4, 9:45 pm, Mattias Sjögren <>
> wrote:
> > >I have the standardprogressbarandworkerthreadscenario with
> > >progressof theworkerthreadbeing fed back to the main UI and
> > >displayed on theprogressbar. I have a delegate and am using
> > >BeginInvoke. But theprogressbaris not updating.

> >
> > You should use Control.BeginInvoke on a control or form, not
> > Delegate.BeginInvoke.
> >
> > Mattias
> >
> > --
> > Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|
> > Please reply only to the newsgroup.

>
>

  Reply With Quote
Reply

Thread Tools
Display Modes



< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 01:04 AM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional