如何使用计时器重复特定任务?

时间:2018-06-03 04:39:17

标签: c# c api timer

我目前在项目中遇到了一些问题。我必须在点击按钮2后间隔2.5分钟更新信息。我很确定我必须对计时器做一些事情,但由于编码技巧不佳,我无法自己解决。< / p>

以下代码的程序就像这样

(button 1 clicked)
(with the given data browse the API in the web browser section)
(button 2 clicked)
(with the x seconds of timer interval, show the data in the conclusion textbox)
(repeat with 2.5 minutes of time interval)

有人能给我一些帮助吗?我过去3天都尽了最大努力,但无法解决......提前致谢!

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = File.ReadAllText(@"C:\\resource\\api.txt");
    textBox2.Text = File.ReadAllText(@"C:\\resource\\user.txt");
    webBrowser1.Navigate("https://ethereum.miningpoolhub.com/index.php?page=api&action=getdashboarddata&api_key=" + textBox1.Text + "&id=" + textBox2.Text);
}

private void button2_Click(object sender, EventArgs e)
{
    timer1.Enabled = true;
}


    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = File.ReadAllText(@"C:\\resource\\api.txt");
        textBox2.Text = File.ReadAllText(@"C:\\resource\\user.txt");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process rg = new System.Diagnostics.Process();
        rg.StartInfo.FileName = "registerapi&userid.exe";
        rg.StartInfo.WorkingDirectory = "C:\\Resource";
        rg.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        rg.Start();
    }

private void timer1_Tick(object sender, EventArgs e)
{
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;

        if (progressBar1.Value < progressBar1.Maximum)
        {
            progressBar1.Value = progressBar1.Value + 5;
        }

        if (progressBar1.Value == 95)
        {

            MessageBox.Show("loading completed");

            string pathmined = "C:\\resource\\24Mined.txt";
            StreamWriter swmined = File.CreateText(pathmined);

            string text4 = webBrowser1.DocumentText;
            string searchmined = @"unt"":";

            swmined.WriteLine(text4.Substring(text4.IndexOf(searchmined), 15));

            swmined.Close();

            label4.Text = File.ReadAllText(@"C:\\resource\\24Mined.txt");

            string mined = label4.Text;
            textBox5.Text = mined.Substring(5, 8) + " ETH";


            string pathconfirm = "C:\\resource\\Confirm.txt";
            StreamWriter swconfirm = File.CreateText(pathconfirm);

            string text5 = webBrowser1.DocumentText;
            string searchconfirm = @"confirmed";

            swconfirm.WriteLine(text5.Substring(text5.IndexOf(searchconfirm), 20));

            swconfirm.Close();

            label8.Text = File.ReadAllText(@"C:\\resource\\confirm.txt");

            string confirm = label8.Text;
            textBox6.Text = confirm.Substring(11, 8) + " ETH";



            string hash = "C:\\resource\\hash.txt";
            StreamWriter hash1 = File.CreateText(hash);

            string text6 = webBrowser1.DocumentText;
            string searchhash = @"hashrate";

            hash1.WriteLine(text6.Substring(text6.IndexOf(searchhash), 18));

            hash1.Close();

            label11.Text = File.ReadAllText(@"C:\\resource\\hash.txt");

            string hasha = label11.Text;
            label12.Text = hasha.Substring(10, 7);

            float hrate = (float.Parse(label12.Text) / float.Parse(label3.Text));
            textBox4.Text = hrate.ToString() + " MH/s";

            textBox3.Text = ("You have mined [" + textBox5.Text + "] during recent 24hours and your current hash rate is [" + textBox4.Text + "]. Until now your total ETH Balance is [" + textBox6.Text + "]");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

using System;

使用System.Windows.Forms;

命名空间timerAttempts {     公共部分类Form1:表格     {         readonly Timer _timer;         private static bool _isTimerOn;

    public Form1()
    {
        InitializeComponent();
        _timer = new Timer { Interval = (int)2.5 * 60 * 1000 };
        _timer.Tick += timer_Tick;

    }

    private static void timer_Tick(object sender, EventArgs e)
    {
        //operation that has to be performed for every 2 and half minutes.
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (_isTimerOn)
        {
            _isTimerOn = false;
            _timer.Stop();
        }
        else
        {
            _isTimerOn = true;
            _timer.Start();
        }
    }
}

}