每10秒自动发送一次文本

我有一个获取应用程序名称的程序,将它们放在列表框中,如果单击“发送”按钮,则将它们发送到另一个窗口。

我想知道的是,它是否可以在单击发送按钮后每10秒自动发送一次? 如果是,我怎么可能这样做?

有代码,以防它带来任何帮助。

private void cmd_send_Click_1(object sender, EventArgs e) { String processID = ""; String processName = ""; String processFileName = ""; String processPath = ""; string hostName = System.Net.Dns.GetHostName(); listBox1.BeginUpdate(); try { for (int i = 0; i < listBox1.Items.Count; i++) { piis = GetAllProcessInfos(); try { // String pno = textBox4.Text.ToString(); // String path = textBox5.Text.ToString(); // String name = textBox6.Text.ToString(); // String user = textBox7.Text.ToString(); // output.Text += "\n Sent data : " + pno + " " + user + " " + name + " " + path ; processID = piis[i].Id.ToString(); processName = piis[i].Name.ToString(); processFileName = piis[i].FileName.ToString(); processPath = piis[i].Path.ToString(); output.Text += "\n\nSENT DATA : \n\t" + processID + "\n\t" + processName + "\n\t" + processFileName + "\n\t" + processPath + "\n"; } catch (Exception ex) { wait.Abort(); output.Text += "Error..... " + ex.StackTrace; } NetworkStream ns = tcpclnt.GetStream(); String data = ""; //data = "--++" + " " + textBox4.Text + " " + textBox5.Text + " " + textBox6.Text + " " + textBox7.Text; data = "--++" + " " + processID + " " + processPath + " " + processFileName + " " + hostName; if (ns.CanWrite) { byte[] bf = new ASCIIEncoding().GetBytes(data); ns.Write(bf, 0, bf.Length); ns.Flush(); } } } finally { listBox1.EndUpdate(); } } 

任何帮助将不胜感激。

您可以将代码放在单个方法中,最初在按钮单击时调用该方法,并根据其当前状态启动/停止计时器。

 private Timer _timer; public Form() // Initialize timer in your form constructor { InitializeComponent(); _timer = new Timer(); _timer.Interval = 10000; // miliseconds _timer.Tick += _timer_Tick; // Subscribe timer to it's tick event } private void _timer_Tick(object sender, EventArgs e) { SendData(); } private void cmd_send_Click_1(object sender, EventArgs e) { if (!_timer.Enabled) // If timer is not running send data and start refresh interval { SendData(); _timer.Enabled = true; } else // Stop timer to prevent further refreshing { _timer.Enabled = false; } } private void SendData() { // Your code here } 

编辑:

如果您使用的是.NET Framework 4.5或更高版本,则可以使用async/await执行相同的操作。

 private bool keepRefreshing; private async void cmd_send_Click_1(object sender, EventArgs e) { if (keepRefreshing) { keepRefreshing = false; return; } keepRefreshing = true; while (keepRefreshing) { // Your code here await Task.Delay(10000); } } 

点击按钮,它将发送数据,并将继续发送延迟10秒。 当您第二次按下按钮时,它将停止刷新间隔,第三次将再次启动,依此类推。

 // Declare a timer Timer tmr = new Timer(); tmr.Interval = 10000; // 10 second tmr.Tick += timerHandler; // We'll write it in a bit tmr.Start(); // The countdown is launched! private void timerHandler(object sender, EventArgs e) { // Here the code what you need each 10 seconds tmr.Stop(); // Manually stop timer, or let run indefinitely } 

他们遵循的方式很多。

 private void cmd_send_Click_1(object sender, EventArgs e) { bool isResend=true; while (isResend==true) { // Put all your here System.Threading.Thread.Sleep(10000); } } 

其他方法是使用Timer等…

每个人的答案都很酷,但至于我,如果你真的需要“点击”作为开始,我会这样做。

  1. 在表单加载内启动计时器和后台工作程序的事件。
  2. set timer.start(); 内部点击。
  3. 勾选后,如果后台工作人员不忙,则执行后台工作人员。
  4. 确保您没有直接设置label1.text =“在这里发送一些作品。” 在后台工作者中,它会导致错误。

希望这可以帮助。

在此处输入图像描述