简体   繁体   English

每10秒自动发送一次文本

[英]Automatically send text every 10 seconds

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. 我有一个程序可以获取应用程序名称,将其放置在列表框中,然后单击“发送”按钮将其发送到另一个窗口。

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? 我想知道的是,单击发送按钮后,是否有可能每10秒自动发送一次? 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();
    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

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
}

EDIT: 编辑:

If you're using .NET framework 4.5 or above you can do the same thing in using async/await . 如果您使用的是.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);
    }
}

On button click it will send data and it will keep sending with delay of 10 seconds. 在按钮上单击它将发送数据,并且将持续发送延迟10秒。 When you press the button second time it will stop refreshing interval, third time it will start again and so on.. 当您第二次按下按钮时,它将停止刷新间隔,第三次按下时,将再次开始刷新,依此类推。

// 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
}

Their are many ways one is follow. 他们遵循的方式很多。

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

Other ways are using Timer, etc... 其他方式正在使用Timer等。

Everyone's answer is cool, but as for me if you really need that "click" as start, i'll do it this way. 每个人的回答都很棒,但是对于我来说,如果您真的需要“单击”作为开始,我会这样做。

  1. Initiate events for timer & background worker inside form load. 在表单加载中为计时器和后台工作程序启动事件。
  2. set timer.start(); 设置timer.start(); inside click. 内点击。
  3. Once ticking, if backgroundworker is not busy, execute background worker. 勾选后,如果后台工作人员不忙,请执行后台工作。
  4. Ensure that you don't directly set label1.text = "send some works here." 确保您不直接设置label1.text =“在此处发送一些作品。” inside the background worker, it will cause error. 在后台工作人员内部,将导致错误。

Hope this helps. 希望这可以帮助。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM