Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PROGRESSBAR1 control on thread in vb.net 2008

Posted on 2010-08-25
5
Medium Priority
?
461 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

618 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