简体   繁体   English

c#winform Invoke抛出NullReferenceException

[英]c# winform Invoke throws NullReferenceException

i'm working on an winform(mdi) pro. 我正在研究winform(mdi)专业版。 And I need to update a dataGridView control when i get new data from another thread. 当我从另一个线程获取新数据时,我需要更新dataGridView控件。 and when new data comes and i'm dragging the dataGridview scroll, it throw a nullreference exception in dataGridView.Invoke. 当新数据出现并且我拖动dataGridview滚动时,它会在dataGridView.Invoke中抛出一个null引用异常。 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. PS:如果新数据来了,我不是拖动滚动条,它工作正常。

any ideas? 有任何想法吗? thank you ! 谢谢 !

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: 我发现当你调用一个控件并设置绑定(或清除它们)并且一个对象被设置为null时,这会抛出一个空引用异常,这通过调用给出错误来反映,但是这个错误在你的代码中的其他地方:

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. 它是这样的......这段代码可能不会抛出错误,因为A)它是从我的头顶而且B)它组成了。 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. 它应该再次工作。

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

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

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