每 10 秒自动发送一次文本 [英] Automatically send text every 10 seconds

查看:34
本文介绍了每 10 秒自动发送一次文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以获取应用程序名称,将它们放在列表框上,如果您单击发送按钮,则将它们发送到另一个窗口.

I have a program that gets the app name, place them on a listbox and sends them to the other window if you click the send button.

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

What I wanted to know is, is it possible for it to automatically send every 10 seconds after a single click on the send button? If yes, how can I possibly do that?

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

There's the codes, in case if it brings of any help.

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();
    }
}

任何帮助将不胜感激.

推荐答案

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

You could place your code inside a single method, call that method initially on button click and start/stop your timer depending on it's current state.

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 做同样的事情.

If you're using .NET framework 4.5 or above you can do the same thing in using 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 秒继续发送.当你第二次按下按钮时,它会停止刷新间隔,第三次它会重新开始,依此类推..

On button click it will send data and it will keep sending with delay of 10 seconds. When you press the button second time it will stop refreshing interval, third time it will start again and so on..

这篇关于每 10 秒自动发送一次文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆