2015-07-28 21 views
5

Capisco che quando Thread.Sleep viene eseguito, i pulsanti sulla GUI non possono essere cliccati.Pulsante impossibile fare clic su Thread.Sleep

Esistono altri modi per ritardare il flusso dei miei codici ma ancora in grado di fare clic sui miei pulsanti sulla GUI?

Ad esempio subito dopo i miei codici eseguono Thread.Sleep (10000); e entro questi 10 secondi non riesco a fare clic sul mio evento button1, c'è comunque che posso ancora fare clic sul mio evento button1 entro questi 10 secondi?

 private void displaydata_event2(object sender, EventArgs e) 
    { 
     txt_data.AppendText(in_data + "\n"); 
     string inStr; 
     inStr = in_data; 

     //MessageBox.Show(inStr.Length.ToString()); 

     if (inStr.Length == 12) 
     { 
      int indexOfSpace = inStr.IndexOf(' '); 
      string Patient = inStr.Substring(indexOfSpace + 1); 

      int rx = 0; 
      int selected = 0; 

      txtData1.Text = Patient; 

      rx = Convert.ToInt16(Patient); 
      selected = Convert.ToInt16(txt_pnorec.Text); 

      if (rx != selected) 
      { 
       MessageBox.Show("Please check patient settings"); 
      } 
     } 
     else if (inStr.Length == 24) 
     { 
      label2.Text = "Patient is not selected!"; 
      label2.BackColor = Color.Red; 
     } 
     else if (inStr.Length == 10) 
     { 
      int indexOfSpace = inStr.IndexOf(':'); 
      string Temp = inStr.Substring(indexOfSpace + 1); 

      txtData2.Text = Temp; 

      double tempflo; 
      tempflo = Convert.ToDouble(Temp); 

      if (tempflo > 20) 
      { 
       lbl_temp.Text = "Fever"; 
       lbl_temp.BackColor = Color.Red; 
      } 
     } 
     else if (inStr.Length == 9) 
     { 
      int indexOfSpace = inStr.IndexOf(':'); 
      string ECG = inStr.Substring(indexOfSpace + 1); 

      txtData3.Text = ECG; 
     } 
     else if (inStr.Length == 19 || inStr.Length == 20) 
     { 
      int indexOfSpace = inStr.IndexOf(':'); 
      string Systolic = inStr.Substring(indexOfSpace + 1); 

      txtData4.Text = Systolic; 
     } 
     else if (inStr.Length == 21 || inStr.Length == 22) 
     { 
      int indexOfSpace = inStr.IndexOf(':'); 
      string Diastolic = inStr.Substring(indexOfSpace + 1); 

      txtData5.Text = Diastolic; 
     } 
     else if (inStr.Length == 16) 
     { 
      int indexOfSpace = inStr.IndexOf(':'); 
      string Pulse = inStr.Substring(indexOfSpace + 1); 

      txtData6.Text = Pulse; 
     } 

     else if (inStr.Length == 23 || inStr.Length == 17 || inStr.Length == 27 || inStr.Length == 30 || inStr.Length == 35 || inStr.Length == 29) 
     { 
      lbl_bp.Text = inStr;//to display status of BP (Normal,prehypotension etc) 

      string bp; 

      bp = inStr; 

      if (bp.Length == 23 || bp.Length == 27 || bp.Length == 31 || bp.Length == 35 || bp.Length == 30) 
      { 
       lbl_bp.BackColor = Color.Red; 
      } 
      else if (bp.Length == 17) 
      { 
       lbl_bp.BackColor = Color.LightGray; 
      } 
     } 

     else if (inStr.Length == 32 || inStr.Length == 25 || inStr.Length == 34 || inStr.Length == 33 || inStr.Length == 26 || inStr.Length == 31) 
     { 
      int indexOfSpace = inStr.IndexOf(':'); 
      string Acc = inStr.Substring(indexOfSpace + 1); 

      txtData7.Text = Acc; 

      string test = inStr; 

      if (test.Length == 25 || test.Length == 34 || test.Length == 33 || test.Length == 26) 
      { 
       label21.Text = "Check on patient!"; 
       label21.BackColor = Color.Red; 
      } 
      else if (test.Length == 32) 
      { 
       label21.Text = ""; 
       label21.BackColor = Color.LightGray; 
      } 
     } 

     else 
     { 

     } 

     if (txtData1.Text != "" && txtData2.Text != "" && txtData3.Text != "" && txtData4.Text != "" && txtData5.Text != "" && txtData6.Text != "" && txtData7.Text != "") 
     { 
      try 
      { 
       connection2.Open(); 
       OleDbCommand command2 = new OleDbCommand(); 
       command2.Connection = connection2; 
       command2.CommandText = "insert into MedicalRecord (PatientNumber,FirstName,LastName,IC,Temperature,ECG,Systolic,Diastolic,Pulse) values('" + txt_pnorec.Text + "','" + txt_fnamerec.Text + "','" + txt_lnamerec.Text + "','" + txt_icrec.Text + "','" + txtData2.Text + "','" + txtData3.Text + "','" + txtData4.Text + "','" + txtData5.Text + "','" + txtData6.Text + "')"; 

       command2.ExecuteNonQuery(); 
       MessageBox.Show("Info Stored"); 
       connection2.Close(); 

       txtData1.Text = ""; 
       txtData2.Text = ""; 
       txtData3.Text = ""; 
       txtData4.Text = ""; 
       txtData5.Text = ""; 
       txtData6.Text = ""; 
       txtData7.Text = ""; 

       Thread.Sleep(interval); 
       MessageBox.Show("Start"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Error: " + ex); 
      } 
      txtData1.Text = ""; 
      txtData2.Text = ""; 
      txtData3.Text = ""; 
      txtData4.Text = ""; 
      txtData5.Text = ""; 
      txtData6.Text = ""; 
      txtData7.Text = ""; 
     } 
    } 

Grazie in anticipo.

+1

Esegui il Thread.Sleep e le azioni associate su un thread diverso. –

+2

Stai ritardando il thread dell'interfaccia utente? Se si divide il thread e si ritarda questo, il thread dell'interfaccia utente rimarrà reattivo. – eX0du5

+0

Avete qualche codice rilevante che potreste mostrare? –

risposta

2
Task.Delay(10000).ContinueWith(x => 
{ 
    //... 
}); 

Dai un'occhiata alla Task.Delay e ContinueWith.

+0

@Gusdor: Grazie, abbastanza giusto. Ho aggiunto qualche descrizione. La domanda non ha avuto il codice quando ho scritto la risposta. Volevo solo mostrare una direzione. – CharithJ

+0

@CharithJ Penso che dovresti menzionare anche che async/await è apparso solo in .Net framework versione 4.5 e quindi non può essere usato per altre versioni di .Net framework – Fabjan

2

è possibile utilizzare asincrona ed attendere

ecco un buon tutorial in combinazione con un interfaccia grafica: https://www.youtube.com/watch?v=MCW_eJA2FeY

è necessario dichiarare la vostra funzione di async

public async void Foo() 
{ 
    ... 
} 

di te può usare:

await Task.Delay(10000); 

verrà rilasciata la GUI per l'intero Delay e sarà possibile utilizzare la GUI mentre lo Delay è in attesa.

4

Ci sono diversi modi per ottenere questo. Un esempio è quello di utilizzare un compito con un ritardo:

Task.Delay(10000).ContinueWith(x => 
{ 
    //Place the code you want delayed here. 
}); 

Un altro esempio potrebbe essere quello di utilizzare un BackgroundWorker che è fatto per questo scopo esatto.

0

È possibile utilizzare Windows.Forms.Timer per eseguire qualcosa dopo il ritardo ... (senza il congelamento del thread dell'interfaccia utente). Dividi il tuo codice in due parti. 1. Prima di ritardo 2. Dopo ritardo

public partial class form1 : Form 
{ 
    // i used namespace to ensure that we use 'correct' Timer and we do not 
    // confuse it with System.Timers.Timer or System.Threading.Timer 
    System.Windows.Forms.Timer tmrDelay; 

    void SomeMethodWithDelay() 
    { 
     // code before delay here 
     tmrDelay.Enabled = true; 
     tmrDelay.Tick += Timer_Tick; 
    } 

    private void Timer_Tick(object sender, EventArgs e) 
    { 
     Thread.Sleep(5000); 

     // your code here 

     // disables timer to stop it 
     tmrDelay.Enabled = false; 
    } 
} 

trarre vantaggio dall'utilizzo di Timer è che si tratta di uno strumento nativo su Windows Form che è stato appositamente progettato per aiutare con questo tipo di problemi. Tuttavia puoi ancora utilizzare cose moderne come Tasks, ad esempio: Task.Factory.StartNew(() => { Thread.Sleep(5000); // your method logic here });

Problemi correlati