我有一个例程,要求后台工作人员完成。如下;
private void BatteryListFetchBackgroundWorkerRunWorkerCompleter(object sender, RunWorkerCompletedEventArgs e)
{
this.Cursor = Cursors.Default;
var sortedList = this.currentBatteries.Values.OrderBy(g => g, new BatteryNameComparer()); //breaks here
this.BatteryBindingSource.DataSource = sortedList;
if (this.batteryListBox.Items.Count > 0)
{
this.batteryListBox.SetSelected(0, true);
}
this.viewScheduleButton.Enabled = true;
this.viewDefaultScheduleButton.Enabled = true;
this.viewEditScheduleLimits.Enabled = true;
}
它突破的界线是;
this.BatteryBindingSource.DataSource = sortedList;
异常是空引用异常,它在设置数据源时发生
BatteryNameComparer
public class BatteryNameComparer : IComparer<Battery>
{
/// <summary>
/// Compares DDSMGroup
/// </summary>
/// <param name="a">first value for comparison</param>
/// <param name="b">second value for comparison</param>
/// <returns>An integer indicating the compare result</returns>
public int Compare(Battery a, Battery b)
{
int aId = int.Parse(a.DeviceName.Substring(BatteryOverviewControl.BatteryPrefixSubstring.Length));
int bId = int.Parse(b.DeviceName.Substring(BatteryOverviewControl.BatteryPrefixSubstring.Length));
return aId.CompareTo(bId);
}
}
答案 0 :(得分:0)
每当看到TargetInvocationException
时,您应立即检查其InnerException
属性以查找实际抛出的异常。
http://msdn.microsoft.com/en-us/library/system.exception.innerexception(v=vs.110).aspx
简单地说,TargetInvocationException
只告诉你在某种调用上下文中抛出了另一个异常 - 这几乎总是一个跨线程操作或通过反射调用。