ZJack
26-07-2009, 19:00
Salve a tutti, avrei bisogno di gestire più thread in un programma scritto in Basic (come compilatore uso SharpDevelop3.0 con VB.NET). Ho già cercato su Google ed ho trovato molto materiale... che non mi è stato però d'aiuto :stordita:
ad esempio: http://www.simply4you.it/articoli/net/art0003a.asp
eseguendo questo programma mi segna un'eccezione, dicendo che non posso modificare la ListBox essendo in un Thread differente. E non so come risolvere...
esempio che ho provato a fare, ma con lo stesso problema...
Public Partial Class MainForm
Dim t1 As New System.Threading.Thread(AddressOf funz)
Dim t2 As New System.Threading.Thread(AddressOf funz2)
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
' TODO : Add constructor code after InitializeComponents
End Sub
Public Sub funz()
TextBox1.Text+="1"
End Sub
Public Sub funz2()
TextBox1.Text+="2"
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
End Sub
Sub Button1Click(sender As Object, e As EventArgs)
t1.Start
t2.Start
End Sub
Sub TextBox1TextChanged(sender As Object, e As EventArgs)
End Sub
End Class
il compilatore dice questo System.InvalidOperationException: Operazione cross-thread non valida:
è stato eseguito l'accesso al controllo 'textBox1' da un thread diverso
da quello da cui è stata eseguita la creazione.
at System.Windows.Forms.Control.get_Handle
at System.Windows.Forms.Control.set_WindowText
at System.Windows.Forms.TextBoxBase.set_WindowText
at System.Windows.Forms.Control.set_Text
at System.Windows.Forms.TextBoxBase.set_Text
at System.Windows.Forms.TextBox.set_Text
at provaThread.MainForm.funz2 in ...\provaThread\MainForm.vb:line 34
at System.Threading.ThreadHelper.ThreadStart_Context
at System.Threading.ExecutionContext.runTryCode
at System.Threading.ExecutionContext.RunInternal
at System.Threading.ExecutionContext.Run
at System.Threading.ThreadHelper.ThreadStart
la riga 34 è TextBox1.Text+="2"
Potete darmi un esempio di codice funzionante con i Thread ? oppure sapete dirmi come risolvere questo problema e sto sbagliando qualcosa? :confused:
grazie in anticipo :)
ad esempio: http://www.simply4you.it/articoli/net/art0003a.asp
eseguendo questo programma mi segna un'eccezione, dicendo che non posso modificare la ListBox essendo in un Thread differente. E non so come risolvere...
esempio che ho provato a fare, ma con lo stesso problema...
Public Partial Class MainForm
Dim t1 As New System.Threading.Thread(AddressOf funz)
Dim t2 As New System.Threading.Thread(AddressOf funz2)
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
' TODO : Add constructor code after InitializeComponents
End Sub
Public Sub funz()
TextBox1.Text+="1"
End Sub
Public Sub funz2()
TextBox1.Text+="2"
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
End Sub
Sub Button1Click(sender As Object, e As EventArgs)
t1.Start
t2.Start
End Sub
Sub TextBox1TextChanged(sender As Object, e As EventArgs)
End Sub
End Class
il compilatore dice questo System.InvalidOperationException: Operazione cross-thread non valida:
è stato eseguito l'accesso al controllo 'textBox1' da un thread diverso
da quello da cui è stata eseguita la creazione.
at System.Windows.Forms.Control.get_Handle
at System.Windows.Forms.Control.set_WindowText
at System.Windows.Forms.TextBoxBase.set_WindowText
at System.Windows.Forms.Control.set_Text
at System.Windows.Forms.TextBoxBase.set_Text
at System.Windows.Forms.TextBox.set_Text
at provaThread.MainForm.funz2 in ...\provaThread\MainForm.vb:line 34
at System.Threading.ThreadHelper.ThreadStart_Context
at System.Threading.ExecutionContext.runTryCode
at System.Threading.ExecutionContext.RunInternal
at System.Threading.ExecutionContext.Run
at System.Threading.ThreadHelper.ThreadStart
la riga 34 è TextBox1.Text+="2"
Potete darmi un esempio di codice funzionante con i Thread ? oppure sapete dirmi come risolvere questo problema e sto sbagliando qualcosa? :confused:
grazie in anticipo :)