HI,
I'm trying to create an ArrayList of array's created by the code below. Each line in the ArrayList requires the previous line's array to calculate. However for each line the code below is run 6 times creating 6 arrays which are merged together and added to the Master ArrayList.
Running this piece of code 6 times for each line is time consuming. Therefore I would like to run the code on seperate threads and when finished merge the 6 arrays together and add them to the ArrayList.
Could somebody please help me with this?
Thanks,
Mark
public double[, , , ,] PeriodTree()
{
// My code creates a double using Previous Period numbers and returns double[, , , ,] ThisPeriod
double[, , , ,] ThisPeriod = new double[2, 2, 7, 51, 51];
mycode...
return ThisPeriod;
}
Loading
jingePosted Nov 18, 2009, 7:34 PM
Not using threads correctly will cause bad performance. You know that when you create a new thread, a kernal object will be created and a context switch will happened, all these are expansive operation.
If the work is very short, then we don't need to use multiple threads.
Otherwise, I think APM pattern is situable in your case. You can run the work in the threadpool. Please refer to the following article.
http://msdn.microsoft.com/en-us/magazine/cc164139.aspx
There is one thing not mentioned in that article, actually we can also use Delegate.BeginInvoke to call a method asyc..
If you have any problem, please let me know.
Mark HughesPosted Nov 19, 2009, 5:46 AM
Mark HughesPosted Nov 18, 2009, 5:24 AM
However I had used this method previousley and it resulted in my code running slower because I had to re-create the threads numerous times.
My piece of code does not take a long time to run but it does have to be called alot of times throughout the main procedure. For example:
My Main peice of code does the following:
private ArrayList GetResults()
{
double[, , , ,] ThisPeriod = new double[2, 2, 7, 51, 51];
ArrayList InterimPeriods = new ArrayList();
ArrayList MatchPeriods = new ArrayList();
ThisPeriod[0, 0, 0, 0, 0] = 0.5;
ThisPeriod[1, 0, 0, 0, 0] = 0.5;
MatchPeriods.Add(ThisPeriod); //here I add period 1 to my MathPeriods ArrayList (this is the starting point)
// I now look to calculate the rest of the Array based on Period1 and a set of parameters which are not important for this example.
for (int t = 1; t < 721; t++)
{
double[, , , ,] RollingPeriod = new double[2, 2, 7, 51, 51];
double[, , , ,] PassPreviousPeriod = (double[, , , ,])MatchPeriods[t - 1];
this.createCalculationTrees(t, PassPreviousPeriod); //this is a seprate piece of code that creates 6 instances of a class based on the PreviousPeriod Array values. They are named ctPeriod1, ctPeriod2 etc.
ctPeriod1.RunThisThread();
ctPeriod2.RunThisThread();
ctPeriod3.RunThisThread();
ctPeriod4.RunThisThread();
ctPeriod5.RunThisThread();
ctPeriod6.RunThisThread(); //These 6 lines will run the code to fill the PeriodTree and return in each instance a results array called Interim period which is then added to the ArrayList below. **This is the part I want to multi-thread**
InterimPeriods.Add(ctPeriod1.InterimPeriod);
InterimPeriods.Add(ctPeriod2.InterimPeriod);
InterimPeriods.Add(ctPeriod3.InterimPeriod);
InterimPeriods.Add(ctPeriod4.InterimPeriod);
InterimPeriods.Add(ctPeriod5.InterimPeriod);
InterimPeriods.Add(ctPeriod6.InterimPeriod);
//After this point these six arrays are merged together and added to the MatchPeriods ArrayList
merging...
MatchPeriods.Add(RollingPeriod);
}
What I did try was to create 6 threads each time it looped to the part of my code I wanted multi threaded. I started the 6 threads and placed a piece of code which locked the InterimPeriod Array until the code had run. Once all InterimPeriod Arrays were unlocked I merged the six added them to MatchPeriods and looped for the next period. However this resulted in my code running slower and I believe this was due to the re-creation of the Threads on each loop. Is there a better way to do this?
Thanks
jingePosted Nov 17, 2009, 7:36 PM
You can also use the following code to run the method in a different thread:
If you need to pass the parameter to the callbackmethoed, then you will use :
From msdn: