2013-07-13 11 views
17

Sto cercando di capire come aggiornare un'interfaccia utente da un evento mentre si utilizza il modello async/await. Di seguito è riportato il codice di prova che sto utilizzando su un'app WinForm. Non sono nemmeno sicuro che questo sia il modo giusto per farlo. Cosa è necessario per consentire al metodo pwe_StatusUpdate di aggiornare l'IU? L'errore dell'operazione incrociata viene generato lì.Aggiornamento dell'interfaccia utente dagli eventi che utilizzano asyc attendere

Grazie per la lettura.

// calling code 
    ProcessWithEvents pwe = new ProcessWithEvents(); 
    pwe.StatusUpdate += pwe_StatusUpdate; 
    await pwe.Run(); 



void pwe_StatusUpdate(string updateMsg) 
    { 
     // Error Here: Cross-thread operation not valid: Control '_listBox_Output' accessed from a thread other than the thread it was created on. 
     _listBox_Output.Items.Add(updateMsg); 
    } 

-

// Class with long running process and event  
public delegate void StatusUpdateHandler(string updateMsg); 

public class ProcessWithEvents 
    { 
    public event StatusUpdateHandler StatusUpdate; 

    public async Task Run() 
    { 
     await Task.Run(() => 
     { 
      for (int i = 0; i < 10; i++) 
       { 

        RaiseUpdateEvent(String.Format("Update {0}", i)); 

        Thread.Sleep(500); 
       } 
      }); 

     } 

     private void RaiseUpdateEvent(string msg) 
     { 
     if (StatusUpdate != null) 
      StatusUpdate(msg); 
     } 
    } 

-

risposta

20

The async pattern has support for progress updates.

In breve, il tuo metodo async può richiedere un IProgress<T> e il codice chiamante passa in un'implementazione di tale interfaccia (in genere Progress<T>).

public class ProcessWithUpdates 
{ 
    public async Task Run(IProgress<string> progress) 
    { 
    await Task.Run(() => 
    { 
     for (int i = 0; i < 10; i++) 
     { 
     if (progress != null) 
      progress.Report(String.Format("Update {0}", i)); 
     Thread.Sleep(500); 
     } 
    }); 
    } 
} 

// calling code 
ProcessWithUpdates pwp = new ProcessWithUpdates(); 
await pwp.Run(new Progress<string>(pwp_StatusUpdate)); 
+0

È 'ProcessWithProgress' nella sezione' // codice chiamante' che dovrebbe essere 'ProcessWithUpdates', invece? –

+0

@Brock: Yup. Modificato. –

2

Si dovrebbe usare Invoke metodo Control. Esegue del codice nella discussione di Control. Inoltre è possibile controllare la proprietà InvokeRequired per verificare se è necessario chiamare il metodo Invoke (controlla se il chiamante si trova su un thread diverso rispetto a quello su cui è stato creato il controllo).

semplice esempio:

void SomeAsyncMethod() 
{ 
    // Do some work    

    if (this.InvokeRequired) 
    { 
     this.Invoke((MethodInvoker)(() => 
      { 
       DoUpdateUI(); 

      } 
     )); 
    } 
    else 
    { 
     DoUpdateUI(); 
    } 
} 

void DoUpdateUI() 
{ 
    // Your UI update code here 
} 

In alcuni casi si dovrebbe verificare la proprietà IsHandleCreated di Control prima di chiamare Invoke metodo. Se IsHandleCreated restituisce falso, allora avete bisogno attendi che la maniglia di controllo verrà creato

+0

Questa grande opera così come la suggession progresso. Sto usando entrambi gli approcci grazie mille. – ChiliYago

+0

Non dovresti implementarlo da solo quando usi il modello asincrono. 'Progress ' è già stato eseguito su SynchronizationContext in cui è stato creato. – cremor

+0

Soluzione semplice. Funziona alla grande. –

2

// Basta dichiarare un delegato in questo modo

delegate void Add(string msg); 

// Poi dichiarare il metodo delegato in questo modo:

var add = new Add((msg) => { 
    _listBox_Output.Items.Add(msg); 
}); 

// Ora basta chiamare il delegato:

void pwe_StatusUpdate(string updateMsg) 
    { 

     _listBox_Output.Invoke(add,updateMsg); 
    } 
0

Ecco un altro esempio

async void DoExport() 
{ 
    var rMsg = ""; 
    var t = await Task<bool>.Factory.StartNew(() => ExportAsMonthReport(LastMonth.Name, LastYear.Name, out rMsg)); 

    if (t) 
    { 
      BeginInvoke((Action)(() => 
      { 
       spinnerMain.Visible = false; 
       menuItemMonth.Enabled = true; 

       MetroMessageBox.Show(this, rMsg, "Export", MessageBoxButtons.OK, MessageBoxIcon.Information, 200); 
      })); 
    } 
    else 
    { 
      BeginInvoke((Action)(() => 
      { 
       spinnerMain.Visible = false; 
       menuItemMonth.Enabled = true; 

       MetroMessageBox.Show(this, rMsg, "Export", MessageBoxButtons.OK, MessageBoxIcon.Error, 200); 
      })); 
    } 
} 
Problemi correlati