Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Cross-Thread!

  1. #1

    Cross-Thread!

    Questo è il codice!
    codice:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            trd = New Thread(AddressOf Thrd)
            trd.IsBackground = True
            trd.Start()
        End Sub
    
        Public Sub Thrd()
            ToolStripComboBox1.SelectedItem = ToolStripComboBox1.Items.Item(0)
        End Sub
    Mi da il seguente errore:
    Operazione cross-thread non valida: è stato eseguito l'accesso al controllo '' da un thread diverso da quello da cui è stata eseguita la creazione.

    Come faccio a risolvere? Grazie in anticipo!!

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Ho risolto!!!

    codice:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.trd = New Thread(AddressOf Me.Thrd)
            Me.trd.IsBackground = True
            Me.trd.Start()
        End Sub
    
        Private Sub AggiungiTesto(ByVal row1 As String, ByVal row2 As String, ByVal row3 As String, ByVal row4 As String, ByVal row5 As String, ByVal row6 As String, ByVal row7 As String, ByVal row0 As String)
            If ListView1.InvokeRequired Then
                Dim d As New SetTextCallback(AddressOf AggiungiTesto)
                Me.Invoke(d, New Object() {row1, row2, row3, row4, row5, row6, row7, row0})
            Else
                Me.ToolStripComboBox1.SelectedItem = Me.ToolStripComboBox1.Items.Item(0)
                Dim row8 = {row1, row2, row3, row4, row5, row6, row7}
                ListView1.Items.Add(row0).SubItems.AddRange(row8)
                ToolStripStatusLabel1.Text = "Ci sono " & i & " elementi nel database"
            End If
        End Sub
    
        Public Sub Thrd()
            Dim db As New MySqlConnection
            db.ConnectionString = Str_Co
            Try
                db.Open()
                Dim sql As String = "SELECT * FROM wrz"
                Dim cmd As MySqlCommand = New MySqlCommand(sql, db)
                Dim rdr As MySqlDataReader = cmd.ExecuteReader()
                While rdr.Read
                    AggiungiTesto(rdr.Item(2).ToString, rdr.Item(3).ToString, rdr.Item(4).ToString, rdr.Item(5).ToString, rdr.Item(6).ToString, rdr.Item(7).ToString, rdr.Item(0).ToString, rdr.Item(1).ToString)
                    i += 1
                End While
            Catch ex As Exception
                MsgBox("Impossibile Leggere il Database: Errore 0001")
            End Try
        End Sub
    Grazie lo stesso! =)

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.