• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1222
  • Last Modified:

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.
0
alumwell
Asked:
alumwell
1 Solution
 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

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