473,574 Members | 2,669 Online
Bytes | Software Development & Data Engineering Community
+ 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.Collecti ons.Generic
Imports System.Text
Imports System.Threadin g
Imports System.IO
Public Class Form1

Dim worker As System.Threadin g.Thread ' Thread
Dim worker_obj As New worker_class ' Object from
worker class
Public Sub New()
Me.InitializeCo mponent()
'ProgressBar1
ProgressBar1.Na me = "ProgressBa r1"
ProgressBar1.Ma ximum = 100
ProgressBar1.Va lue = 10
End Sub

Sub UpdateProgressD isplay(ByVal Value_for_progr ess_bar As Integer)
ProgressBar1.Va lue = Value_for_progr ess_bar
Application.DoE vents()
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click

worker = New Thread(AddressO f
worker_obj.DoRe moteCommunicati ons)

' 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_Pr ogress_caller(B yVal
Value_for_progr ess_bar As Integer)
Public Sub DoRemoteCommuni cations()
Console.WriteLi ne("Comms worker thread started.....")

' Set up delegate to allow asynchronous calls between threads.
Dim caller As New Async_Update_Pr ogress_caller(A ddressOf
Form1.UpdatePro gressDisplay)

' Initiate the asynchronous call.
Dim result As IAsyncResult

result = caller.BeginInv oke(100, Nothing, Nothing)
Console.WriteLi ne("Progress value 25 passed ")
Thread.Sleep(30 00)
Console.WriteLi ne("Comms worker thread Finished.")

End Sub

End Class

--------------------------------------------------------------------------------------------
Many thanks for any help.

Denis
_______________ ________
http://www.CentronSolutions.com
Dec 4 '07 #1
4 2045
>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.BeginIn voke on a control or form, not
Delegate.BeginI nvoke.

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.beginin voke.

Ive changed the worker class as below using

Form1.ProgressB ar1.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.ProgressB ar1 is on the form. Im confused.

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

Public Class worker_class

' Set up delegate for assync function call.
--------------------------------
Public Delegate Sub Async_Update_Pr ogress_caller(B yVal
Value_for_progr ess_bar As Integer)
Public Sub DoRemoteCommuni cations()
Console.WriteLi ne("Comms worker thread started.....")

' Set up delegate to allow asynchronous calls between threads.
Dim caller As New Async_Update_Pr ogress_caller(A ddressOf
Form1.UpdatePro gressDisplay)

' Initiate the asynchronous call.
Dim result As IAsyncResult

result = Form1.ProgressB ar1.BeginInvoke (caller)
Console.WriteLi ne("Progress value 25 passed ")
Thread.Sleep(30 00)
Console.WriteLi ne("Comms worker thread Finished.")

End Sub

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

Thanks for your help

Denis
On Dec 4, 9:45 pm, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
wrote:
I have the standardprogres sbarandworkerth readscenario with
progressof theworkerthread being fed back to the main UI and
displayed on theprogressbar. I have a delegate and am using
BeginInvoke. But theprogressbari s not updating.

You should use Control.BeginIn voke on a control or form, not
Delegate.BeginI nvoke.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.ne t/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 BackgroundWorke r 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 BackgroundWorke r 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

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

Similar topics

2
2749
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 Smalltalk I would fire of notification exceptions that a progress bar would catch, increment progress, and then allow to continue (or no
5
7371
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 of freezes. Therefore i would like to open a little window with a progress bar in it that shows how far the import has come.
5
2239
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 controls that are associated with window handles? regards Tomaz
8
4732
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 binary files, so have created a second thread to do the processing. This thread is set to be the LOWEST priority. So far so good with all that....
5
10883
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 have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked...
4
2645
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
3222
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 progress event. So I added a bw to the form. The Dowork Calls a method which instantiates a DT and calls its Dataprocessing method.
5
9549
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 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.
10
2040
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 multiple labels and i'm using a text box for status updates. the labels change when a task completes. the text box report what task is running. so 1)...
0
7815
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7738
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8078
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7829
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5634
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3755
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2254
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.