title: Cross-thread operation not valid: Control 'ListView1' accessed from a thread other than the thread it was created on
body: <p>i'm trying to export list view item to excel sheet, every thing work perfect 
  but when I'm trying to do this in background worker i face this error :</p>
  
  <p>An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code</p>
  
  <p>Additional information: Cross-thread operation not valid: Control 'ListView1' accessed from a thread other than the thread it was created on.</p>
  
  <p>i dint know how to fix this , please help me :)</p>
  
  <p>this is my code :</p>
  
  <pre><code>Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
      Try
          saveFileDialog1.Filter = "Excel File|*.xlsx"
          saveFileDialog1.Title = "Save an Excel File"
          Application.EnableVisualStyles()
          If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
              If saveFileDialog1.FileName &lt;&gt; "" Then
                  PictureBox1.Visible = True
                  BackgroundWorker1.RunWorkerAsync()
  
              End If
          End If
      Catch ex As Exception
          MsgBox(ex.Message)
      End Try
  End Sub
  
  Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
      saveExcelFile(SaveFileDialog1.FileName)
  End Sub
  
  Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
      PictureBox1.Visible = False
      MessageBox.Show("DONE !!")
  End Sub
  
  Public Sub saveExcelFile(ByVal FileName As String)
      'Try
      Dim xls As New Excel.Application
      Dim sheet As Excel.Worksheet
      Dim i As Integer
      xls.Workbooks.Add()
      sheet = xls.ActiveWorkbook.ActiveSheet
      Dim row As Integer = 1
      Dim col As Integer = 1
      For i = 0 To Me.ListView1.Columns.Count - 1
          sheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
      Next
      For i = 0 To Me.ListView1.Items.Count - 1
          For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1 ' here the ERROR !!
              sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
          Next
      Next
  
      row += 1
      col = 1
  
      ' for the header 
      sheet.Rows(1).Font.Name = "Microsoft Sans Serif"
      sheet.Rows(1).Font.size = 16
      sheet.Rows(1).Font.Bold = True
      sheet.Rows(1).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
      Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#20b2aa")
      sheet.Rows(1).Font.color = mycol
      ' for all the sheet without header
      sheet.Range("a2", "z1000").Font.Name = "Arial"
      sheet.Range("a2", "z1000").Font.Size = 14
      sheet.Range("a2", "z1000").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
  
      sheet.Range("A1:X1").EntireColumn.AutoFit()
      sheet.Range("A1:X1").EntireRow.AutoFit()
  
      xls.ActiveWorkbook.SaveAs(FileName)
      xls.Workbooks.Close()
      xls.Quit()
      'Catch ex As Exception
      '    MsgBox(ex.Message)
      'End Try
  End Sub
  </code></pre>
  
  <p>hello Enigmativity ...</p>
  
  <p>i do what you say but i have small error :</p>
  
  <p>An exception of type 'System.Runtime.InteropServices.COMException' occurred in Noor Phone.exe but was not handled in user code</p>
  
  <p>Additional information: Unable to access the file. Try one of the following actions:</p>
  
  <p>• Make sure that the selected folder.</p>
  
  <p>• Make sure that the folder that contains the file is not read-only.</p>
  
  <p>• Make sure the file name does not contain one of the following codes: &lt;>? []: | Or *</p>
  
  <p>• Make sure the file name and path name does not contain more than 128 characters.</p>
  
  <p>this is my code after editing:</p>
  
  <pre><code> Private Class BgwData
      Public FileName As String
      Public Headers As String()
      Public Data As String()()
  End Class
  Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
      'Try
  
      Dim data As New BgwData() With _
  { _
  .FileName = SaveFileDialog1.FileName, _
  .Headers = _
  Me.ListView1.Columns _
      .Cast(Of System.Windows.Forms.ColumnHeader)() _
      .Select(Function(ch) ch.Name) _
      .ToArray(), _
  .Data = _
  Me.ListView1.Items.Cast(Of ListViewItem)() _
      .Select(Function(lvi) lvi.SubItems _
              .Cast(Of ListViewItem.ListViewSubItem)() _
              .Select(Function(lvsi) lvsi.Text) _
              .ToArray()) _
      .ToArray() _
  }
  
      'BackgroundWorker1.RunWorkerAsync(data)
      SaveFileDialog1.Filter = "Excel File|*.xlsx"
      SaveFileDialog1.Title = "Save an Excel File"
      Application.EnableVisualStyles()
      If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
          If SaveFileDialog1.FileName &lt;&gt; "" Then
              PictureBox1.Visible = True
              'BackgroundWorker1.RunWorkerAsync()
              BackgroundWorker1.RunWorkerAsync(data)
  
          End If
      End If
      'Catch ex As Exception
      '    MsgBox(ex.Message)
      'End Try
  End Sub
  Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
      'saveExcelFile(SaveFileDialog1.FileName)
      saveExcelFile(CType(e.Argument, BgwData))
  End Sub
  
  Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
      PictureBox1.Visible = False
      MessageBox.Show("DONE !!")
  End Sub
  Private Sub saveExcelFile(ByVal data As BgwData)
      'Public Sub saveExcelFile(ByVal FileName As String)
      'Try
      Dim xls As New Excel.Application
      Dim sheet As Excel.Worksheet
      Dim i As Integer
      xls.Workbooks.Add()
      sheet = xls.ActiveWorkbook.ActiveSheet
      'Dim row As Integer = 1
      'Dim col As Integer = 1
      'For i = 0 To Me.ListView1.Columns.Count - 1
      '    sheet.Cells(1, i + 1) = Me.ListView1.Columns(i).Text
      'Next
      'For i = 0 To Me.ListView1.Items.Count - 1
      '    For j = 0 To Me.ListView1.Items(i).SubItems.Count - 1 ' here the ERROR !!
      '        sheet.Cells(i + 2, j + 1) = Me.ListView1.Items(i).SubItems(j).Text
      '    Next
      'Next
  
      Dim row As Integer = 1
      Dim col As Integer = 1
      For i = 0 To data.Headers.Length - 1
          sheet.Cells(1, i + 1) = data.Headers(i)
      Next
      For i = 0 To data.Data.Length - 1
          For j = 0 To data.Data(i).Length - 1
              sheet.Cells(i + 2, j + 1) = data.Data(i)(j)
          Next
      Next
  
      row += 1
      col = 1
  
      ' for the header 
      sheet.Rows(1).Font.Name = "Microsoft Sans Serif"
      sheet.Rows(1).Font.size = 16
      sheet.Rows(1).Font.Bold = True
      sheet.Rows(1).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
      Dim mycol As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#20b2aa")
      sheet.Rows(1).Font.color = mycol
      ' for all the sheet without header
      sheet.Range("a2", "z1000").Font.Name = "Arial"
      sheet.Range("a2", "z1000").Font.Size = 14
      sheet.Range("a2", "z1000").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
  
      sheet.Range("A1:X1").EntireColumn.AutoFit()
      sheet.Range("A1:X1").EntireRow.AutoFit()
  
      'xls.ActiveWorkbook.SaveAs(FileName)
      xls.ActiveWorkbook.SaveAs(data.FileName)'**HERE THE ERROR**
      xls.Workbooks.Close()
      xls.Quit()
  
      Marshal.ReleaseComObject(sheet)
      Marshal.ReleaseComObject(xls)
  
      'Catch ex As Exception
      '    MsgBox(ex.Message)
      'End Try
  End Sub
  </code></pre>