472,370 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,370 software developers and data experts.

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
4 1917
>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

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: 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...
5
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.