简体   繁体   English

C#.Net中的线程安全网格视图更新

[英]Thread Safe Grid View Update in C# .Net

I am explaining my problem elaborately. 我正在详细解释我的问题。 I have a dataGridView in Windows main form. 我在Windows主窗体中有一个dataGridView I have a timer which is starting when button1 is clicked. 我有一个计时器,当单击button1时该计时器开始计时。 and it stops when button2 is clicked. 当单击button2时,它停止。

Now in the meantime I am calculating something and putting it in the dataGridView . 现在,与此同时,我正在计算某些内容并将其放入dataGridView This is a dynamic calculation. 这是一个动态计算。 In this calculation I am using a Thread. 在此计算中,我使用了线程。 (I am absolutely new in Threads) . (我绝对不是Threads的新手)。 I have used Thread.Sleep(100) to calculate data. 我已经使用Thread.Sleep(100)来计算数据。 Now I am using a Thread.Sleep in a loop where I am calculating data of consecutive things. 现在,我在循环中使用Thread.Sleep来计算连续事物的数据。 I want the respective order of the calculation to remain. 我希望保留计算的各个顺序。 After each iteration of the loop I am using dataGridView.Rows.AddRange(DataGridViewRow) to add new row with new data calculated. 循环的每次迭代后,我都使用dataGridView.Rows.AddRange(DataGridViewRow)添加带有计算出的新数据的新行。

But I am not getting the data in the dataGridView in the order and also it's not coming properly. 但是我没有按顺序在dataGridView中获取数据,并且也无法正常运行。 It's getting blanked out sometime and coming randomly. 它在某个时间变得空白,并且随机出现。

PS - I am clearing the data each time when the timer interval is over. PS-每次计时器间隔结束时,我都会清除数据。

You can only access UI controls from the UI thread. 您只能从UI线程访问UI控件。

If you try to access them from other threads, you get undefined behavior as you have found. 如果尝试从其他线程访问它们,则会发现未定义的行为。

I suggest you start by reading this free ebook: Albahari 我建议您先阅读这本免费电子书: Albahari

Before you attempt to update the datagridview, you need to call datagridview.InvokeRequired. 在尝试更新datagridview之前,您需要调用datagridview.InvokeRequired。 If this is true then, call Invoke passing in the delegate which you wish to execute and any data that you want to pass. 如果是这样,则调用Invoke传递您要执行的委托以及要传递的任何数据。

It has been resolved. 已解决。 Actually you have to set the Thread.Sleep timer such that it gets completed before your Windows.Forms.Timer 's interval is over. 实际上,您必须设置Thread.Sleep计时器,以使其在Windows.Forms.Timer的间隔结束之前完成。

Here is the thread safe method to set Gridview. 这是设置Gridview的线程安全方法。

class Program
{
public static List<string> ChoiceExtension = new List<string>();
static void Main()
{
    ChoiceExtension.Add("One");
    ChoiceExtension.Add("Two");
    ChoiceExtension.Add("Three");
    //Set list as gridsource
    InserttoGrid(ChoiceExtension);

}

public static void SetDataSource(object value)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Path of Extension");
        foreach (var item in value as List<string>)
        {
            dt.Rows.Add(new object[] { item });

        }
        ExtensionList.DataSource = dt;
    }
public static void InserttoGrid(List<string> List)
    {
        if (ChoiceExtension.Count >0)
        {

            if (this.ExtensionList.InvokeRequired)
            {
               ExtensionList.Invoke(new SetDataSourceDelegate(SetDataSource), new Object[] { List });


            }
            else
            {
                SetDataSource(List);

            }

        }
    }

}

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

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