|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Feb 2009
Messaggi: 18
|
C# Problema cross thread
Codice:
Task.Factory.StartNew(() =>
{
listBox1.Items.Add("converto");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "ffmpeg";
proc.StartInfo.Arguments = " -i " + ultimo_file + " -target pal-dvd " + percorsoMPEG + zeri + numfiles2 + "_" + lbldataeora.Text + ".mpg";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; //nasconde la finestra dos
proc.Start();
proc.WaitForExit();
// MessageBox.Show("ffmpeg -i " + ultimo_file + " -target pal-dvd " + percorsoMPEG + zeri + numfiles2 + "_" + lbldataeora.Text + ".mpg");
listBox1.Items.Add("fine conversione");
});
Codice:
Operazione cross-thread non valida: è stato eseguito l'accesso al controllo 'listBox1' da un thread diverso da quello da cui è stata eseguita la creazione. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Feb 2004
Città: Vivere?
Messaggi: 3087
|
edit
|
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Dec 2005
Città: Provincia di Vicenza
Messaggi: 174
|
Non puoi accedere ad oggetti dell'interfaccia grafica da un thread diverso da quello in cui sono stati creati.
La classe Task esegue il codice in un thread separato, quindi non puoi aggiungere un'item alla listbox... per fare cio devi usare il metodo BeginInvoke ciao |
|
|
|
|
|
#4 |
|
Junior Member
Iscritto dal: Feb 2009
Messaggi: 18
|
Codice:
Task.Factory.StartNew(() =>
{ listbox1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(delegate() { listbox1.Items.Add("Converto") });
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "ffmpeg";
proc.StartInfo.Arguments = " -i " + ultimo_file + " -target pal-dvd " + percorsoMPEG + zeri + numfiles2 + "_" + lbldataeora.Text + ".mpg";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; //nasconde la finestra dos
proc.Start();
proc.WaitForExit();
// MessageBox.Show("ffmpeg -i " + ultimo_file + " -target pal-dvd " + percorsoMPEG + zeri + numfiles2 + "_" + lbldataeora.Text + ".mpg");
listbox1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new Action(delegate() { listbox1.Items.Add("Fine conversione") });
});
|
|
|
|
|
|
#5 |
|
Junior Member
Iscritto dal: Feb 2009
Messaggi: 18
|
Niente non ne esco fuori!
|
|
|
|
|
|
#6 |
|
Member
Iscritto dal: Dec 2005
Città: Provincia di Vicenza
Messaggi: 174
|
Se non ci dici quali errori hai è difficile darti una mano
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Feb 2003
Città: Stockholm (SE)
Messaggi: 1343
|
leggiti questi 4 articoli di base sulla TPL. sono veramente facili da capire
http://blogs.msdn.com/b/csharpfaq/ar...g-started.aspx http://blogs.msdn.com/b/csharpfaq/ar...n-context.aspx http://blogs.msdn.com/b/csharpfaq/ar...cellation.aspx http://blogs.msdn.com/b/csharpfaq/ar...r-problem.aspx |
|
|
|
|
|
#8 |
|
Junior Member
Iscritto dal: Feb 2009
Messaggi: 18
|
Nessun errore, solo che non riesco a capire come scrivere nella listbox da un thread diverso!
Ho letto anche le 4 guide, ma non ci ho capito un granchè di roba! Qualcuno cosi gentile da aiutarmi? Thanks |
|
|
|
|
|
#9 |
|
Junior Member
Iscritto dal: Feb 2009
Messaggi: 18
|
A chi potesse interessare ho risolto con:
Codice:
private delegate void AddListBoxItemDelegate(object item);
private void AddListBoxItem(object item)
{
if (this.listBox1.InvokeRequired)
{
// This is a worker thread so delegate the task.
this.listBox1.Invoke(new AddListBoxItemDelegate(this.AddListBoxItem), item);
}
else
{
// This is the UI thread so perform the task.
this.listBox1.Items.Add(item);
}
}
Codice:
AddListBoxItem("quellochevoglioscrivere");
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:02.


















