There is a need to implement such a thing. It is necessary that in a parallel stream, for example, a message is sent every 5 seconds. Accordingly, whatever the application was working, and did not hang. I tried a whole bunch of the day, but I could not find anything, so that would work. Tried to do through the eternal cycle.

Here on this attempt, I stopped.

async Task send() { while(true) { Thread.Sleep(2000); Messages.Send(false, "ΠΎΠΊΠ΅ΠΉ", textBox.Text, null, null,false, null, null,"256" , null, null); } } private async void button_Click(object sender, RoutedEventArgs e) { await send(); } 

I will be glad to any help.

    3 answers 3

    For such a task with async / await, one can not be wise, but use the good old System.Threading.Timer . Advantages: it does not take an extra thread to wait. Remember only that the callback will be called on the thread from the thread pool. Those. if you have interaction with UI there, then you need to switch context.

     using System.Threading; ... private readonly TimeSpan period = TimeSpan.FromSeconds(5); private Timer timer; ... private async void button_Click(object sender, RoutedEventArgs e) { if (this.timer == null) { this.timer = new Timer(SendMessage, null, this.period, this.period); // ΠΈΠ»ΠΈ Ρ‚Π°ΠΊ, Ρ‡Ρ‚ΠΎΠ± ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠ° ΠΏΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»Π° Π½Π΅ΠΌΠ΅Π΄Π»Π΅Π½Π½ΠΎ //this.timer = new Timer(SendMessage, null, TimeSpan.Zero, this.period); } } private void SendMessage(object state) { try { Messages.Send(false, "ΠΎΠΊΠ΅ΠΉ", text, null, null, false, null, null, "256", null, null); } catch (Exception e) { // ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ³ΠΎ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ } } 
    • Thanks, and how in this case to implement a flow stop? - BwehaaFox
    • @BwehaaFox what do you mean by stopping the thread? Disable periodic timer operation? - andreycha
    • well if i just do timer.Dispose (); subject to a certain condition, the next time you press the button, the timer does not start. made c help. CancellationTokenSource cts; cts.Cancel (); timer.Dispose (); then everything is restarted ok. When the condition is met, cts.Cancel (); timer.Dispose (); otherwise, the timer starts - BwehaaFox

    async and Thread.Sleep not very friendly. In addition, it is not clear how fast the Messages.Send function is.

    If fast, this will do:

     async Task Send(string text) { while (true) { await Task.Delay(2000); Messages.Send(false, "ΠΎΠΊΠ΅ΠΉ", text, null, null, false, null, null, "256", null, null); } } private async void button_Click(object sender, RoutedEventArgs e) { await Send(textBox.Text); } 

    If slow, it is better this way:

     private async void button_Click(object sender, RoutedEventArgs e) { await Task.Run(() => Send(textBox.Text)); } 
    • Um .. And how much does it make sense to apply await to the eternal cycle? - Qwertiy ♦
    • And if slow, then you need to write 3 lines instead of two. - Qwertiy ♦
    • @Qwertiy: Why not? With await, this perpetual loop does not occupy a separate thread. - VladD
    • one
      I do not understand - what is wrong with all about exception handling? This is a separate topic, and it is on the conscience of the author. The variant with Task.Run is more universal anyway. Even the slightest derogation of Messages.Send can make a negative impression on the program on the end user. - Alexis
    • one
      @ z668: The question was essentially to write async void button_Click(...) { await Task.Run(() => Send(textBox.Text)); } async void button_Click(...) { await Task.Run(() => Send(textBox.Text)); } or void button_Click(...) { Task.Run(() => Send(textBox.Text)); } void button_Click(...) { Task.Run(() => Send(textBox.Text)); } . The second option does not forward exceptions. - VladD
     private async void button_Click(object sender, RoutedEventArgs e) { Task SendMsg = new Task(() => { while(true) { Messages.Send(false, "ΠΎΠΊΠ΅ΠΉ", text, null, null, false, null, null, "256", null, null); Thread.Sleep(2000); } }); SendMsg.Start(); } 

    Try this. I have a connection with the server.

    • Such code swallows exceptions. Task is necessary to review. - andreycha