如何在C#中按下按钮时暂停并开始向数据网格视图添加行

本文关键字:开始 数据 数据网 添加行 视图 网格 暂停 按钮 | 更新日期: 2023-09-27 18:26:52

我有一个应用程序,它可以与连接的硬件通信。当我打开硬件时,硬件会不断地向应用程序发送一些数据。我能够从应用程序中的硬件读取数据,并通过在运行时为每个数据添加行来将数据记录到dataGridView中。

现在我想添加一个"暂停"按钮。当我按下这个按钮时,我的应用程序应该停止向数据网格视图添加新的原始数据

但录制过程应在后台继续

当我再次按下相同的按钮时,所有记录的行都应该显示出来,并且该过程应该继续

请帮帮我,我是C#的新手。

这是我的代码:我正在使用backgroundWorker。

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
     string StrMSGdata="";
     string tStamp="";
     // Here will get my data and process it.
     //Then
     backgroundWorker1.ReportProgress(0, StrMSGdata + "#" + tStamp);

}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{                               
       string recived   = e.UserState.ToString();
       string[] fullMsgData = new string[15];
       fullMsgData      = recived.Split('#');
       string msgData   = fullMsgData[0];
       string timStamp  = fullMsgData[1];
       dataGridView1.Rows.Add(timStamp,msgData);
    }

如何在C#中按下按钮时暂停并开始向数据网格视图添加行

让我们模拟硬件:

public class Hardware
{
    System.Timers.Timer _timer;
    public event EventHandler Send;
    public Hardware()
    {
        _timer = new System.Timers.Timer();
        _timer.Interval = 1000;
        _timer.Elapsed += _timer_Elapsed;
        _timer.Start();
    }
    void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Send(this, new SendArgs(DateTime.Now.Ticks));
    }
    public class SendArgs : EventArgs
    {
        public long Id { get; private set; }
        public SendArgs(long id)
        {
            Id = id;
        }
    }
}

在您的主表单中,您可以添加一个集合对象,该对象将临时保存以下值:

public partial class Form1 : Form
{
    List<object> _queue = new List<object>();
    bool _paused = false;
    public Form1()
    {
        InitializeComponent();
        dataGridView1.Columns.Add("Id", "Id");
        var h = new Hardware();
        h.Send += h_Send;
    }
    void h_Send(object sender, EventArgs e)
    {
        var value = ((Hardware.SendArgs)e).Id;
        if (_paused)
        {
            _queue.Add(value);
        }
        else
        {
            if (this.InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(delegate()
                {
                    dataGridView1.Rows.Add(value);
                }));
            }
        }
    }
    private void pauseButton_Click(object sender, EventArgs e)
    {
        _paused = !_paused;
        pauseButton.Text = (_paused) ? "Start" : "Pause";
        if (!_paused)
            if (this.InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(delegate()
                {
                    _queue.ForEach(item =>
                    {
                        dataGridView1.Rows.Add(item);
                    });
                    _queue.Clear();
                }));
            }
            else
            {
                _queue.ForEach(item =>
                {
                    dataGridView1.Rows.Add(item);
                });
                _queue.Clear();
            }
    }
}

[编辑]以下是您更新问题中代码示例的版本:

List<object> _queue = new List<object>();
bool _paused = false;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    string StrMSGdata = "";
    string tStamp = "";
    for (int i = 0; i < 100; i++)
    {
        StrMSGdata = i.ToString();
        tStamp = DateTime.Now.ToLongTimeString();
        backgroundWorker1.ReportProgress(i, StrMSGdata + "#" + tStamp);
        Thread.Sleep(1000);
    }
}
private void backgroundWorker1_ProgressChanged(
    object sender, ProgressChangedEventArgs e)
{
    string recived = e.UserState.ToString();
    string[] fullMsgData = new string[15];
    fullMsgData = recived.Split('#');
    string msgData = fullMsgData[0];
    string timStamp = fullMsgData[1];
    if (_paused)
    {
        _queue.Add(new[]{ timStamp, msgData });
    }
    else
    {
        dataGridView1.Rows.Add(timStamp, msgData);
    }
}
private void pauseButton_Click(object sender, EventArgs e)
{
    _paused = !_paused;
    pauseButton.Text = (_paused) ? "Start" : "Pause";
    if (!_paused)
        if (this.InvokeRequired)
        {
            BeginInvoke(new MethodInvoker(delegate()
            {
                _queue.ForEach(item =>
                {
                    dataGridView1.Rows.Add(item);
                });
                _queue.Clear();
            }));
        }
        else
        {
            _queue.ForEach(item =>
            {
                dataGridView1.Rows.Add((string[])item);
            });
            _queue.Clear();
        }
}