X
xuanli317
Guest
Hi,
I have a form program like this:
namespace mynamespace
{
class myclass
{
// Create a class to store some value
public class FinalResultItem
{
public uint R { set; get; }
public uint N { set; get; }
public double Result { set; get; }
public double Error { set; get; }
}
// Use a List<> to store all the results from different threads
public volatile List<FinalResultItem> FinalResult;
// Define a delegate of Calculate function
private delegate void CalculateDelegate(int a);
// main
int main()
{
int[] a = GetValue(); // Get some input value
FinalResult = new List<FinalResultItem> { }; // Initialize the FinalResult List
FinalResult.Clear(); // Clear the FinalResult List
// For a better performance on multi-core PC, I assign one thread for each value of "a" to do the calculation.
for (int i = 0; i < a.Length; i++)
{
CalculateDelegate CD = new CalculateDelegate(Calculate);
CD.BeginInvoke(a, null, null);
}
// Wait Until All Delegate Threads Are Ended
do
{
} while (bIsAllDone == false);
// Check If Error Occured Among All Delegate Threads
// Ensure there is something in the list
if (FinalResult.Count > 0)
{
// Use LINQ to sort the List
IEnumerable<FinalResultItem> result = from item in FinalResult
orderby item.Error, item.R, item.N
select item;
Output(result); // output the result
}
}
// Calculate function
void Calculate(int a)
{
for (int i = 0; i < 10; i++) // Use a for loop to check all possibility
{
FinalResultItem FRI = new FinalResultItem();
FRI = GetResult(a, i); // Get Result
if (FRI.Error < Threshold) // If Result Error is acceptable
{
FinalResult.Add(FRI); // Add the result to FinalResult List
}
}
}
}
}
And it works well except the following part throws a System.NullReferenceException occasionally.
// Use LINQ to sort the List
IEnumerable<FinalResultItem> result = from item in FinalResult
orderby item.Error, item.R, item.N
select item;
The exception is the following,
/*
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=PLL Divider Calculator
StackTrace:
at PLL_Divider_Calculator.Form1.<>c.<StartSingleRun>b__32_0(FinalResultItem item) in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 510
at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
at PLL_Divider_Calculator.Form1.StartSingleRun() in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 521
at PLL_Divider_Calculator.Form1.StartCalculation() in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 368
at PLL_Divider_Calculator.Form1.pictureBox7_Click(Object sender, EventArgs e) in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 680
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at PLL_Divider_Calculator.Program.Main() in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Program.cs:line 19
*/
By checking the value under debug mode, I find out the cause of the exception is the "item" is null.
But I can't tell, according to the logic in the program, why there will be a null value in the list.
And the weird thing is, for the same group of input value "a", sometimes it can give the correct result and sometimes it throws the exception.
Approximate one exception in every 10 runs.
And I just find out,
1. If I set the platform to "x86" (which was "x64"), the program seems not to throw the exception any more.
2. The more items in the FinalResult List, the higher possibility of exception.
But I need this program to run under x64 platform as it contains lots of multiplication of double type data, which is much faster than running under x86.
Could anyone please help me solve it? Thanks a lot!
Continue reading...
I have a form program like this:
namespace mynamespace
{
class myclass
{
// Create a class to store some value
public class FinalResultItem
{
public uint R { set; get; }
public uint N { set; get; }
public double Result { set; get; }
public double Error { set; get; }
}
// Use a List<> to store all the results from different threads
public volatile List<FinalResultItem> FinalResult;
// Define a delegate of Calculate function
private delegate void CalculateDelegate(int a);
// main
int main()
{
int[] a = GetValue(); // Get some input value
FinalResult = new List<FinalResultItem> { }; // Initialize the FinalResult List
FinalResult.Clear(); // Clear the FinalResult List
// For a better performance on multi-core PC, I assign one thread for each value of "a" to do the calculation.
for (int i = 0; i < a.Length; i++)
{
CalculateDelegate CD = new CalculateDelegate(Calculate);
CD.BeginInvoke(a, null, null);
}
// Wait Until All Delegate Threads Are Ended
do
{
} while (bIsAllDone == false);
// Check If Error Occured Among All Delegate Threads
// Ensure there is something in the list
if (FinalResult.Count > 0)
{
// Use LINQ to sort the List
IEnumerable<FinalResultItem> result = from item in FinalResult
orderby item.Error, item.R, item.N
select item;
Output(result); // output the result
}
}
// Calculate function
void Calculate(int a)
{
for (int i = 0; i < 10; i++) // Use a for loop to check all possibility
{
FinalResultItem FRI = new FinalResultItem();
FRI = GetResult(a, i); // Get Result
if (FRI.Error < Threshold) // If Result Error is acceptable
{
FinalResult.Add(FRI); // Add the result to FinalResult List
}
}
}
}
}
And it works well except the following part throws a System.NullReferenceException occasionally.
// Use LINQ to sort the List
IEnumerable<FinalResultItem> result = from item in FinalResult
orderby item.Error, item.R, item.N
select item;
The exception is the following,
/*
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=PLL Divider Calculator
StackTrace:
at PLL_Divider_Calculator.Form1.<>c.<StartSingleRun>b__32_0(FinalResultItem item) in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 510
at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
at PLL_Divider_Calculator.Form1.StartSingleRun() in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 521
at PLL_Divider_Calculator.Form1.StartCalculation() in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 368
at PLL_Divider_Calculator.Form1.pictureBox7_Click(Object sender, EventArgs e) in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Form1.cs:line 680
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at PLL_Divider_Calculator.Program.Main() in D:\SoftwareDevelopment\VS2016\PLL Divider Calculator\PLL Divider Calculator\Program.cs:line 19
*/
By checking the value under debug mode, I find out the cause of the exception is the "item" is null.
But I can't tell, according to the logic in the program, why there will be a null value in the list.
And the weird thing is, for the same group of input value "a", sometimes it can give the correct result and sometimes it throws the exception.
Approximate one exception in every 10 runs.
And I just find out,
1. If I set the platform to "x86" (which was "x64"), the program seems not to throw the exception any more.
2. The more items in the FinalResult List, the higher possibility of exception.
But I need this program to run under x64 platform as it contains lots of multiplication of double type data, which is much faster than running under x86.
Could anyone please help me solve it? Thanks a lot!
Continue reading...