It is necessary to suspend the program for some time in some place. I try to make it so that some time passes between the user's text and the output of the answer (question-time-answer), but when using Thread.Sleep (), the program falls asleep before outputting the question-answer (time-question-answer). Tell me how to do

static void sre_speech(object sender,SpeechRecognizedEventArgs e) { string answer="Извини, не поняла"; if (e.Result.Confidence > 0.82) {t.Text += "Ivan:>"+e.Result.Text+Environment.NewLine; switch (e.Result.Text) { case "Привет": answer = "Привет, Иван"; break; case "Как дела": answer = "У меня неплохо :)"; break; case "Сколько времени": answer = dt.Hour+":"+dt.Minute+":"+dt.Second; break; } } Thread.Sleep(1500); t.Text += "Caroline:>" + answer + Environment.NewLine; }` private void form1_Shown(object sender, EventArgs e) { t = chat; CultureInfo ci = new CultureInfo("ru-ru"); SpeechRecognitionEngine sre = new SpeechRecognitionEngine(ci); sre.SetInputToDefaultAudioDevice(); sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_speech); Choices words = new Choices(); words.Add(new string[] {"Привет","Как дела","Сколько времени" }); GrammarBuilder gb = new GrammarBuilder(); gb.Append(words); Grammar g = new Grammar(gb); sre.LoadGrammar(g); sre.RecognizeAsync(RecognizeMode.Multiple); }` 

A phrase is entered by the user (for example, it is asked how much time), and the answer should not be displayed immediately, after a while (as some kind of simulation of deliberation)

  • It’s not very clear what you want to get and what the problem is (a minimal example would be very useful), but according to the format of the question I can assume that you are writing an application with a graphical interface, sre_speech is a kind of event handler for the application, and t is a text field , and it is not updated until the handler has completed. I guess? - yeputons
  • @yeputons yes, speech recognition software - ZOOM SMASH
  • @yeputons here is a certain example, perhaps it will be clearer - ZOOM SMASH
  • Wpf or Windows Forms? And what's the variable 't'? - klutch1991
  • There is not enough code that triggers an event. Show the call handler code. Again, once visited the question with the tag "multithreading", explain what it is with you. - klutch1991

1 answer 1

Why not make your sre_speech asynchronous? (By the way, are you sure that the handler must be static ? Then what is t and how is it declared?)

 private async void sre_speech(object sender,SpeechRecognizedEventArgs e) { ... await Task.Delay(1500); // вместо Thread.Sleep(1500); ... } 

Update:

As @ klutch1991 correctly noted, if, after all, t is a link to the UI control, then in the end it will look like this:

 private async void sre_speech(object sender,SpeechRecognizedEventArgs e) { string answer="Извини, не поняла"; if (e.Result.Confidence > 0.82) { AppendTextToChat("Ivan", e.Result.Text); switch (e.Result.Text) { ... } } await Task.Delay(1500); AppendTextToChat("Caroline", answer); }` private void AppendTextToChat(string name, string text) { Application.Current.Dispatcher.Invoke( () => t.Text += $"{name}:>{text}{Environment.NewLine}"); } 
  • It seems to me that for this purpose it is better to use the "window timer". Delay will hang the entire application. - nick_n_a
  • Why will the delay hang the entire application if it is a different thread and not the main one? - Andrew
  • @nick_n_a where did you read this? - MihailPw
  • Using a timer for one delay is impractical and violates the readability of the code - MihailPw
  • Apparently t is a link to the control with the text, its value should be updated in the UI stream. And create a delay in the secondary stream. - klutch1991