Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

PROGRESSBAR1 control on thread in vb.net 2008

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
Fordraiders
Asked:
Fordraiders
  • 3
2 Solutions
 
DhaestCommented:
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
 
DhaestCommented:
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
 
DhaestCommented:
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
 
FordraidersAuthor Commented:
im not in c#
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now