Solved

PROGRESSBAR1 control on thread in vb.net 2008

Posted on 2010-08-25
5
450 Views
Last Modified: 2013-12-26
vb.net 2008

I had the following code working fine in vb.net 2003

I'm simply have a fake progressbar on a form that I want moving marquee going back and forth..

But I get a error in vb.net 2008
I created a thread because I'm running a long process...



Dim processAccRunXref As Threading.Thread











' START THE TIMER HERE. STARTING A NEW THREAD TO KEEP TIMERS FREE FROM PROGRAM THREAD

        Me.ProgressBar1.Visible = True

        Me.Label15.Visible = True

        Me.TextBox4.Visible = True

        processAccRunXref = New Thread(AddressOf MarqueeProgress)

        processAccRunXref.Start()













Public Sub MarqueeProgress()

        '  Dim strText As String

        RunProgress = Not RunProgress

        '  On Error Resume Next

        Do While RunProgress

            With ProgressBar1

                If .Value = .Maximum Then

                    .Value = .Minimum

                Else

                    .Value += 1 <----------   ERROR Cross-thread operation not valid: Control'ProgressBar1' accessed from a thread other than the thread it was created on.

     



           End If

                Threading.Thread.Sleep(10)

                System.Windows.Forms.Application.DoEvents()

            End With

        Loop

end Sub

Open in new window

0
Comment
Question by:fordraiders
  • 3
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
How to solve "Cross thread operation not valid"
http://www.codeproject.com/KB/recipes/AccessControlFromThread.aspx

How can I update my user interface from a thread that did not create it?
http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx

Updating the UI from a Secondary Thread
http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
Comment Utility
The BackgroundWorker class makes this kind of thing very simple. It does all the marshalling to the UI thread for you.

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

To use winforms control from separate thread you should call Control.Invoke method. In your case this will be TextBox.Invoke

 

The code may look like this:

 

delegate void OutputUpdateDelegate(string data);

public void UpdateTextBox(string data)

{

if ( txtOutput.InvokeRequired )

  txtOutput.Invoke(new OutputUpdateDelegate(OutputUpdateCallback),

  new object[] { data });

else

  OutputUpdateCallback(data); //call directly

}

 

private void OutputUpdateCallback(string data)

{

 txtOutput.Text += data;

}

 

UpdateTextBox method can be called from other threads 

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
You can't access the form's controls through other threads than the one who created it. You can fix it by calling invoke.

Example of using an invoke and delegate can be found here:
How can I update my user interface from a thread that did not create it?
http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx

Update UI the easy way using anonymous delegates
http://staceyw.spaces.live.com/blog/cns!F4A38E96E598161E!652.entry

Perhaps you should take a look at this article: Updating the UI from a Secondary Thread
http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
http://weblogs.asp.net/justin_rogers/articles/126345.aspx
0
 
LVL 3

Author Comment

by:fordraiders
Comment Utility
im not in c#
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
Comment Utility
Just use a regular System.Windows.Forms.Timer from the ToolBox with an Interval of maybe 250 milliseconds and update your progressbar from there.

It's the LONG PROCESS that should be in the separate thread...not the progressbar.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now