在刷新DataGridView内容时触发事件处理程序

时间:2014-10-17 09:29:34

标签: c# multithreading events datagridview event-handling

我正在创建一个刷新每秒datagridview内容的应用程序。我注意到,当我按下一个单元格时,有时候不会触发CellClick事件。问题是什么,我该如何处理?

刷新线程方法(在单独的线程上):

private void RefreshThread()
{
    var watch = Stopwatch.StartNew();
    while (true)
    {
        this.Invoke((MethodInvoker)delegate
        {
            UpdateDataGridWithChannels();
        });
     }
    watch.Stop();
    int elapsedMs = (int) watch.ElapsedMilliseconds;

    if (elapsedMs < Constants.NetworksRefreshThreadInterval)
        Thread.Sleep(Constants.NetworksRefreshThreadInterval - elapsedMs);
}

UpdateDataGridWithChannels方法:

private void UpdateDataGridWithChannels()
{
    List<Channel> Chnls24 = Channels.Values.Take(14).ToList();
    DataGridViewChannels24List.UpdateChannelsView(Chnls24);
}

UpdateChannelsView(在我的自定义datagridview中):

public void UpdateChannelsView(List<Channel> chls)
{
    foreach (Channel Chnl in chls)
    {
        if (!BindingList.Contains(Chnl))
            BindingList.Add(Chnl);
        else
            BindingList.Update(Chnl);           
    }
    BindingList.RaiseListChangedEvents = true; 

    BindingList.ResetBindings(); 

    BindingList.RaiseListChangedEvents = false; 
}

0 个答案:

没有答案