Cross-thread operation not valid

Hello.

I have this code and it works but i'm trying to output the results to a list box:

Private Sub BtnB17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnB17.Click
        Dim t As Threading.Thread
        t = New Threading.Thread(AddressOf Me.BackgroundProcessB17)
        t.Start()

    End Sub

    Private Sub BackgroundProcessB17()

        Dim p As Process = New Process 'This is the name of the process we want to execute
        p.StartInfo.FileName = ("c:\psshut.bat")
        p.StartInfo.UseShellExecute = False 'need to set this to false to redirect output
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.CreateNoWindow = True
        p.Start()
        ' read all the output
        Dim SROutput As System.IO.StreamReader = p.StandardOutput
        Dim tmp As String
        Do While p.HasExited = False
            tmp = SROutput.ReadLine
            If tmp <> "" Then
              Me.ListBox1.Items.Add(tmp & vbNewLine)
            End If
        Loop
        p.Dispose()

    End Sub

Although i am getting this exception:
Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on

On this line:
Me.ListBox1.Items.Add(tmp & vbNewLine)

How can i get around this.

Thanks in advanced.
alumwellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
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
alumwellAuthor Commented:
Do you have any code examples similar to what i'm doing? This is what i have so far, but i'm unsure what to do with the Me.ListBox1.Items.Add(tmp & vbNewLine) bit.

Thanks for any advice/code.
        Dim p As Process = New Process 'This is the name of the process we want to execute 
        p.StartInfo.FileName = ("c:\psshut.bat")
        p.StartInfo.UseShellExecute = False 'need to set this to false to redirect output
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.CreateNoWindow = True
        p.Start()
        ' read all the output
        Dim SROutput As System.IO.StreamReader = p.StandardOutput
        Dim tmp As String
        Do While p.HasExited = False
            tmp = SROutput.ReadLine
            If tmp <> "" Then
                If (Me.ListBox1.InvokeRequired) Then
                    Me.ListBox1.Items.Add(tmp & vbNewLine)
                End If
            End If
        Loop
        p.Dispose()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.