Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从后台线程更新datagridview奇数行为_C#_Datagridview - Fatal编程技术网

C# 从后台线程更新datagridview奇数行为

C# 从后台线程更新datagridview奇数行为,c#,datagridview,C#,Datagridview,我有一些数据,我在一项任务中对其进行了更新:目前该应用程序是对一个想法的黑客攻击,因此我对代码表示歉意 Task.Factory.StartNew(() => { dataGridView1.BeginInvoke((Action)(() => { dataGridView1.SuspendLayout(); })); dataSet1.Reset();

我有一些数据,我在一项任务中对其进行了更新:目前该应用程序是对一个想法的黑客攻击,因此我对代码表示歉意

Task.Factory.StartNew(() =>
    {
        dataGridView1.BeginInvoke((Action)(() =>
            {
                dataGridView1.SuspendLayout();
            }));

        dataSet1.Reset();
        da.Fill(dataSet1);

        dataGridView1.BeginInvoke((Action)(() =>
            {
                dataGridView1.DataSource = dataSet1.Tables[0];
                dataGridView1.Columns[0].Visible = false;
                dataGridView1.Columns[1].Width = 50;
                dataGridView1.ResumeLayout();
            }));
    }
    ).ContinueWith(task =>
        {
            if (dataSet1.Tables[0].Rows.Count > 0)
            {
                if (lastcount != dataSet1.Tables[0].Rows.Count)
                {
                    lastcount = dataSet1.Tables[0].Rows.Count;
                    if (lastcount == 0)
                    {
                        NotifyWithMessage("The items have been cleared", "Items cleared");
                    }
                    else
                    {
                        NotifyWithMessage(String.Format("There are {0} new items in your monitor", dataSet1.Tables[0].Rows.Count));
                    }
                }
            }
        }
    );
现在,代码基本上可以工作了。没有错误,这很好

当它在任务外更新时,datavgridview根本没有闪烁,当我在debug中运行它时,它非常小,在黑客可以接受的范围内。。我在调试之外运行它的那一刻。。。这太明显了!暂停和恢复布局根本没有任何区别。我需要线程中的代码,因为UI的响应速度很慢,没有响应速度,这是可以接受的,但是现在刷新速度很差

我的Datagridview是根据单元格颜色定制颜色的,但是,我不明白为什么调试和发布之间会有区别,我希望性能反过来

(我试着调用并开始调用…)

我看着

在调试下,这一点都不会闪烁,甚至一点也不会。。。在释放条件下,有一个荒谬的闪烁


我能做些什么?

在后台启动一个填充数据集的任务,完成此任务后,您将开始一个InVoke,暂停布局、分配数据并继续

对于现在的版本,在执行代码路径时有很多可能性,很难预测会发生什么


渲染必须在UI线程上进行,因此您所能做的就是尝试为其优化代码。异步部分我将按照本文开头所述做。

最后我做了以下工作: 我将查询重新连接到一个新的数据集中,如果计数相同,那么我不会更新网格,如果计数改变了,我会更新网格

timer1.Stop();

        Task<Boolean>.Factory.StartNew(() =>
            {
                DataSet t = new DataSet();
                //_dataSet1.Reset();
                Boolean ok = false;
                Int16 retries = 0;
                while (!ok && retries<3)
                try
                {
                    da.Fill(t);
                    ok = true;
                }
                catch
                {
                    retries++;
                    Thread.Sleep(1000);
                }
                //if (!ok) throw new Exception("DB error");
                if (!ok) return false;
                try
                {
                    if (t.Tables.Count > 0 && t.Tables[0].Rows.Count != _lastcount)
                    {
                        _dataSet1 = t;
                        _lastcount = t.Tables[0].Rows.Count;
                        return true;
                    }
                }
                catch {  }
                return false;
            }).ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        SQLFailed();
                        return;
                    }
                    if (!task.Result) return;


                    Invoke((Action) (() =>
                                         {
                                             dataGridView1.DataSource = _dataSet1.Tables[0];
                                             dataGridView1.Columns[0].Visible = false;
                                             dataGridView1.Columns[1].Width = 50;
                                         }));

                    if (_lastcount == 0)
                    {
                        NotifyWithMessage("The items have been cleared", "Items cleared");
                    }
                    else
                    {
                        NotifyWithMessage(String.Format("There are {0} new items in your monitor", _lastcount));
                    }
                });


    timer1.Start();
timer1.Stop();
Task.Factory.StartNew(()=>
{
数据集t=新数据集();
//_dataSet1.Reset();
布尔ok=false;
Int16重试次数=0;
而(!确定并重试0&&t.Tables[0].Rows.Count!=\u lastcount)
{
_数据集1=t;
_lastcount=t.Tables[0]。Rows.Count;
返回true;
}
}
捕获{}
返回false;
}).ContinueWith(任务=>
{
if(task.IsFaulted)
{
SQLFailed();
返回;
}
如果(!task.Result)返回;
调用((操作)(()=>
{
dataGridView1.DataSource=_dataSet1.Tables[0];
dataGridView1.Columns[0]。Visible=false;
dataGridView1.Columns[1]。宽度=50;
}));
如果(_lastcount==0)
{
NotifyWithMessage(“项目已清除”,“项目已清除”);
}
其他的
{
NotifyWithMessage(String.Format(“监视器中有{0}个新项目,_lastcount));
}
});
timer1.Start();

我认为上面的代码确实在一个单独的线程中填充了数据集。。因为它是在一项任务中完成的。。