时间:2019-03-17 标签:c#winformInvoke抛出NullReferenceException

发布于 2024-11-16 11:11:44 字数 2615 浏览 2 评论 0原文

我正在开发 winform(mdi) pro。当我从另一个线程获取新数据时,我需要更新 dataGridView 控件。当新数据到来并且我拖动 dataGridview 滚动时,它会在 dataGridView.Invoke 中抛出 nullreference 异常。我已经搜索了几天,让谷歌疯狂,但没有帮助。 代码如下:

  
    public void ReceiveNewData(object sender, UpateEventArgs ex)
    {
        if (this.dataGridView.InvokeRequired)
        {
            dataGridView.Invoke(new UpateEventHandler(ReceiveNewData), new object[] { this, ex });
        }
        else
            this.BindNewData();
    }

    private void BindNewData()
     {

        if (dataGridView!= null && (QuoteMember.listOneClickQuoteItem != null || QuoteMember.listMarketingQuoteItem != null))
        {
            DataTable dataSource = PublicFunction.ToDataTable(QuoteMember.listOneClickQuoteItem);
            if (dataSource != null)
                    dataSource.Merge(PublicFunction.ToDataTable(QuoteMember.listMarketingQuoteItem), true);
                else
                    dataSource = PublicFunction.ToDataTable(QuoteMember.listMarketingQuoteItem);
            dataGridView.DataSource = dataSource;
        }
    }

public PublicFunction
{
        public static DataTable ToDataTable(List dataSource)
        {
            if(dataSource != null)
                return ToDataTable((dataSource.ToArray()), 1);
            return null;
        }

        public static DataTable ToDataTable(List dataSource) 
        {
            if (dataSource != null)
                return ToDataTable((dataSource.ToArray()), 2); 
            return null; 
        }
        private static DataTable ToDataTable(QuoteItemBase[] m, int type)
        {
            DataTable dsTemp = null;

            if (type == 1)
            {
                dsTemp = new DataTable("OneClickQuote");
            }
            else if (type == 2)
            {
                dsTemp = new DataTable("MarketingQuote");
            }
            else
                dsTemp  = new DataTable("temptable");

            dsTemp.Columns.Add("Date");
            dsTemp.Columns.Add("Time");
            dsTemp.Columns.Add("NO");
            dsTemp.Columns.Add("Name");


            if (m == null)
                return dsTemp;

            foreach (var item in m)
            {
                DataRow drTemp = dsTemp.NewRow();
                drTemp["Date"] = item.date;
                drTemp["Time"]  = item.time;
                drTemp["NO"] = item.no;
                drTemp["Name"] = item.name;
                dsTemp.Rows.Add(drTemp);

            }

            return dsTemp;
      }
}






PS: 如果有新数据出现并且我没有拖动滚动条,它就可以正常工作。

有什么想法吗? 谢谢 !

i'm working on an winform(mdi) pro. And I need to update a dataGridView control when i get new data from another thread. and when new data comes and i'm dragging the dataGridview scroll, it throw a nullreference exception in dataGridView.Invoke. i have searched for few days and drove google crazy,but didn't help.
the code like this:

  
    public void ReceiveNewData(object sender, UpateEventArgs ex)
    {
        if (this.dataGridView.InvokeRequired)
        {
            dataGridView.Invoke(new UpateEventHandler(ReceiveNewData), new object[] { this, ex });
        }
        else
            this.BindNewData();
    }

    private void BindNewData()
     {

        if (dataGridView!= null && (QuoteMember.listOneClickQuoteItem != null || QuoteMember.listMarketingQuoteItem != null))
        {
            DataTable dataSource = PublicFunction.ToDataTable(QuoteMember.listOneClickQuoteItem);
            if (dataSource != null)
                    dataSource.Merge(PublicFunction.ToDataTable(QuoteMember.listMarketingQuoteItem), true);
                else
                    dataSource = PublicFunction.ToDataTable(QuoteMember.listMarketingQuoteItem);
            dataGridView.DataSource = dataSource;
        }
    }

public PublicFunction
{
        public static DataTable ToDataTable(List dataSource)
        {
            if(dataSource != null)
                return ToDataTable((dataSource.ToArray()), 1);
            return null;
        }

        public static DataTable ToDataTable(List dataSource) 
        {
            if (dataSource != null)
                return ToDataTable((dataSource.ToArray()), 2); 
            return null; 
        }
        private static DataTable ToDataTable(QuoteItemBase[] m, int type)
        {
            DataTable dsTemp = null;

            if (type == 1)
            {
                dsTemp = new DataTable("OneClickQuote");
            }
            else if (type == 2)
            {
                dsTemp = new DataTable("MarketingQuote");
            }
            else
                dsTemp  = new DataTable("temptable");

            dsTemp.Columns.Add("Date");
            dsTemp.Columns.Add("Time");
            dsTemp.Columns.Add("NO");
            dsTemp.Columns.Add("Name");


            if (m == null)
                return dsTemp;

            foreach (var item in m)
            {
                DataRow drTemp = dsTemp.NewRow();
                drTemp["Date"] = item.date;
                drTemp["Time"]  = item.time;
                drTemp["NO"] = item.no;
                drTemp["Name"] = item.name;
                dsTemp.Rows.Add(drTemp);

            }

            return dsTemp;
      }
}






PS:
if new data comes and i'm not dragging scroll bar, it works fine.

any ideas?
thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风渺 2024-11-23 11:11:44

我发现当您调用控件并设置绑定(或清除它们)时
并且对象设置为 null 这可能会引发空引用异常,这通过调用给出错误反映出来,但是此错误位于代码中的其他位置:

快速示例:

public class test : Form
{
  public test()
  {
    Thread t = new Thread(start);
    t.Start();
  }
  public void start()
  {
    LoadCompleteEvent();
  }
  public void LoadComplete() //fired by LoadCompleteEvent();
  {
    if(this.InvokeIsRequired)
    {
      //do invoke
      //and return
    }

    comboBoxEditBrand.Properties.Items.Clear();
    comboBoxEditBrand.Properties.Items.AddRange(ListOfStuff.ToArray());
  }
  public void comboBoxEditBrand_SelectedItemChanged(object sender, eventargs e) // fired as control is changed
  {
    //error here!!
    if(comboBoxEditBrand.SelectedItem == SomeBrandItem) //<- this is where the error is thrown!! check for null first!
    {
      //do something
    }
  }
}

它是这样的..此代码可能不会抛出错误是因为 A) 这是我的想法,B) 这是编造的。但这是困扰我半个上午的问题,为什么会抛出这个错误。

只需将

if(comboBoxEditBrand.SelectedItem == null)
  return;

其放置在显示//错误的位置即可!它应该会再次起作用。

I found that when you invoke a control and set bindings (or clear them)
and an object is set to null this can throw a null reference exception, this is reflected through invoke giving an error, this error however is somewhere else in your code:

quick example:

public class test : Form
{
  public test()
  {
    Thread t = new Thread(start);
    t.Start();
  }
  public void start()
  {
    LoadCompleteEvent();
  }
  public void LoadComplete() //fired by LoadCompleteEvent();
  {
    if(this.InvokeIsRequired)
    {
      //do invoke
      //and return
    }

    comboBoxEditBrand.Properties.Items.Clear();
    comboBoxEditBrand.Properties.Items.AddRange(ListOfStuff.ToArray());
  }
  public void comboBoxEditBrand_SelectedItemChanged(object sender, eventargs e) // fired as control is changed
  {
    //error here!!
    if(comboBoxEditBrand.SelectedItem == SomeBrandItem) //<- this is where the error is thrown!! check for null first!
    {
      //do something
    }
  }
}

it's something like this.. this code will probably not throw the error because A) it's from the top of my head and B) it's made up. BUT this is kind of what bugged me for half a morning as to WHY this error was thrown.

just place

if(comboBoxEditBrand.SelectedItem == null)
  return;

where it says //error here!! and it should work again.

雅心素梦 2024-11-23 11:11:44

确保在调用之前切换到 Gui 线程

Make sure you switch to the Gui thread before you invoke

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文