?
Solved

PROGRESSBAR1 control on thread in vb.net 2008

Posted on 2010-08-25
5
Medium Priority
?
459 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33528952
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 1000 total points
ID: 33528954
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
ID: 33528955
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
ID: 33533275
im not in c#
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 33533765
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

764 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