473,470 Members | 1,906 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
_______________________
http://www.CentronSolutions.com
Dec 4 '07 #1
5 9538
>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
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 4 '07 #2
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 <mattias.dont.want.s...@mvps.org>
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/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 5 '07 #3
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.
Dec 5 '07 #4
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.
Dec 6 '07 #5
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.

"dg*******@eircom.net" 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 <mattias.dont.want.s...@mvps.org>
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/|http://www.dotnetinterop.com
Please reply only to the newsgroup.

Dec 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bob | last post by:
Hello, I want to show progress to the user while some method is running so I thought I'd use a progress bar. But I don't see how I can do this without writing GUI code in the model? In...
5
by: Søren Reinke | last post by:
Hi there I am working on a program where the user should be able to import some CSV files. With my set of test data, it takes about 2 minutes to import, while it is importing the program sort...
5
by: Tomaz Koritnik | last post by:
I have a worker thread doing some long calculation and I'd like to report progress (percent done,...). One easy way it to call Control.Invoke(). But is there another way of doing this without using...
8
by: Dino M. Buljubasic | last post by:
I am trying to create a pop up progress bar (on a form) that would show progress from downloading files from an FTP server. any help will be appreciated
8
by: WhiteWizard | last post by:
I guess it's my turn to ASK a question ;) Briefly my problem: I am developing a Windows app that has several User Controls. On one of these controls, I am copying/processing some rather large...
5
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
4
by: Joachim | last post by:
Is there an efficient way of copying a file and at the same time updating a progress bar with short intervals to see how much is still to copy of the file? Like some FTP programs have for instance.
1
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a...
4
by: dgleeson3 | last post by:
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...
10
by: =?Utf-8?B?YXVsZGg=?= | last post by:
am having a hard time wrapping my head around "backgroundworker" class and updates to the window form that calls it. i have some questions about how to update windows form controls. i have...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.