First of all I must express my great thanks to you Goksly and for Bradzac, JakeG (Overclockers Australia members) for helping me.
I created the same thread there but OCAU is not viewable for guests thus I am posting here my ideas.
Moreover I must clarify that I am not a professional programmer! I just program for OcBible.
Anyway for newbies like me one solution is the following:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Data_modeling
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
delegate int MethodToCall(int i,int j);
BackgroundWorker myWorker1,myWorker2;
private void btn_Fire_Click(object sender,EventArgs e)
{
int result=0;
myWorker1=new BackgroundWorker();
myWorker2=new BackgroundWorker();
myWorker1.DoWork+=new DoWorkEventHandler(delegate
{
MethodToCall callLinear=new MethodToCall(Linear);
result=callLinear.Invoke(3,5); // Here result=8
});
myWorker2.DoWork+=new DoWorkEventHandler(delegate
{
MethodToCall callPower=new MethodToCall(Power);
callPower.Invoke(4,7);
});
myWorker1.RunWorkerAsync();
myWorker2.RunWorkerAsync();
// If we add a statement e.g. MessageBox.Show("result="+result.ToString()); the value is always 0
//......
//...... More statements which use the result parameter such as
//...... if-else conditions thus the parameter mush have the actual value
//......
}
private int Linear(int i,int j)
{
for (int k=0; k<=100; k++) // 2 progress bars added just to show
{ // that 2 threads are running during a session
Thread.Sleep(2); // BTW this statement spends CPU time (assume complex calculations)
progressBar1.Value=k;
}
return i+j;
}
private int Power(int i,int j)
{
for (int k=0; k<=100; k++)
{
Thread.Sleep(3);
progressBar2.Value=k;
}
return i*j;
}
}
}
As you understand the code is a hybrid of Goksly's and JakeG's ideas.
Hopefully the source code:
1) Passes parameters in threads. Not only one like PaParameterizedThreadStart (damn we need type casting as Bradzac posted at OCAU).
2) Loads 2 (similarly more) threads during a session.
3) Threads return a value (if we want).
Unfortunately the source code:
1) We cannot set thread priority. This is useful if a program loads many of them --> thus we can manage which of them are running first etc......
2) The result parameter has wrong value outside of the delegate block.
Apart of the disadvantages if we don't care for priorities and returnable values (a void function for example) this is the best solution IMHO.
This page here covers many topics thank you W1zzard (TechPowerUp!) & AndyCosmonaut (OCAU) for the link.
Here it is:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Data_modeling
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
Thread t1,t2;
private void btn_Fire_Click(object sender,EventArgs e)
{
double dummy=2310.2003; // OcBible's forbidden number!!!!!
Approximation regression1=new Approximation(2,3,4);
Approximation regression2=new Approximation(2,3,(int) dummy);
t1=new Thread(new ThreadStart(regression1.Linear));
t2=new Thread(new ThreadStart(regression2.Power));
t1.Priority=ThreadPriority.BelowNormal;
t2.Priority=ThreadPriority.BelowNormal;
t1.Start();
t2.Start();
int result1=regression1.result1;
int result2=regression2.result2;
//......
//...... More statements which use the result1,result2 parameters such as
//...... if-else conditions thus the parameter mush have the actual value
//......
}
private void btn_Stop_Click(object sender,EventArgs e)
{
t1.Abort();
t2.Abort();
}
}
public class Approximation
{
private int i,j,k;
public int result1,result2;
public Approximation(int i,int j,int k)
{
this.i=i;
this.j=j;
this.k=k;
} // The first procedure uses 3 variables but the second 2 that's why I used
public void Linear() // above the dummy variable. Of course I could write one more public class
{ // but this sucks LOL (never waste time)
for (long m=-90000000; m<=500000000; m++)
; // This statement spends CPU time (assume complex calculations).....
result1=i+j+k;
}
public void Power()
{
for (long m=-900000000; m<=90000000; m++)
;
result2=i*j;
}
}
}
Hopefully the source code:
1) Passes parameters in threads.
2) Loads many threads during a session.
3) Threads return a value.
4) We can play with thread priority property.
Unfortunately the source code:
The result1, result2 parameters has wrong values again!
-->Therefore:
How we can manage the above source codes to get the actual values for the parameters?
One solution is to increase t1 & t2 priorities to Highest (for the second source code) but CPU load will be heavy so the stop button does not respond to terminate the running threads.
Any other ideas????????
