Solved

PROGRESSBAR1 control on thread in vb.net 2008

Posted on 2010-08-25
5
451 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
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 250 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 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 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 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…

930 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

15 Experts available now in Live!

Get 1:1 Help Now