|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Oct 2008
Messaggi: 71
|
[VB - VB.NET] Threads
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
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... Codice:
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 Codice:
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 Codice:
TextBox1.Text+="2" grazie in anticipo Ultima modifica di ZJack : 26-07-2009 alle 19:06. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2004
Messaggi: 3210
|
Per forza, operazione di cross-threading, che ti aspettavi ?
Ma davvero quel codice l'hai preso paro paro da un sito che "suggerisce" soluzioni di questo genere ? Anzitutto, una sistemata al codice, al di là del fatto che non sia thread safe, mi pare messo giù malaccio. Soluzioni possibili al cross threading in un progetto WinForms ce ne sono parecchie, direi che l'unico limite è la fantasia. Io la risolverei così, con una semplice Delegate : Codice:
Public Class Form1
Private Delegate Sub SetTextCallback(ByVal s As String)
Private Sub AggiungiTesto(ByVal testo As String)
If TextBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf AggiungiTesto)
Me.Invoke(d, New Object() {testo})
Else
TextBox1.Text += testo
End If
End Sub
Private t1 As New System.Threading.Thread(AddressOf funz)
Private t2 As New System.Threading.Thread(AddressOf funz2)
Public Sub funz()
AggiungiTesto("1")
End Sub
Public Sub funz2()
AggiungiTesto("2")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1.Start()
t2.Start()
End Sub
End Class
|
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Oct 2008
Messaggi: 71
|
E' "messo giù malaccio" perchè dovevo solo provarlo al momento per capire come funzionava, poi in qualche modo l'avrei sistemato
Ho già visto i thread in Java, ma di Visual Basic non sono molto esperto ... codice ovviamente provato e funzionante Se posso chiedere avrei una domanda... utilizzare un altro ambiente come SharpDevelop invece che Microsoft Visual Basic mi porta in 'svantaggio' in qualche modo, o posso stare tranquillo ? Grazie mille per l'aiuto Ultima modifica di ZJack : 26-07-2009 alle 22:53. |
|
|
|
|
|
#4 | ||
|
Senior Member
Iscritto dal: Dec 2004
Messaggi: 3210
|
Quote:
http://www.hwupgrade.it/forum/showthread.php?t=2000981 [ mio post #4 ] Cmq, di tecniche per il threading ce ne sono diverse. Puoi dare un'occhiata anche al BackgroundWorker. Altra tecnica possibile nel tuo caso sarebbe stata creare un array di threads ( classi + metodi, non semplici functions ) da interrogare ad intervalli di tempo regolari ( Timer )... Quote:
|
||
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Oct 2008
Messaggi: 71
|
ok grazie, mi va benissimo il metodo già provato
Visual Basic Express certo che l'ho provato, mi ero anche registrato... solo che quando ho avuto necessità di metterlo anche su questo pc, ho usato lo stesso codice e non me lo accettava ... Per sbaglio ho trovato SharpDevelop per provare C# e ho visto che faceva anche VB.NET Contando quello che devo fare al momento non penso di avere problemi Grazie ancora |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:32.












... Per sbaglio ho trovato SharpDevelop per provare C# e ho visto che faceva anche VB.NET








