private void exp_regression()
{
....
...
...
}
private void power_regression()
{
....
...
...
}
private void Compute_button_click(object sender, e SystemEventArgs)
{
.....
.....
t1.Start();
t2.Start();
}
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 WindowsApplication1
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
Thread t1,t2;
int x,y,result1,result2;
private void btn_Fire_Click(object sender,EventArgs e)
{
Random k=new Random();
x=k.Next(50);
y=k.Next(60);
t1=new Thread(new ThreadStart(Linear));
t2=new Thread(new ThreadStart(Power));
t1.Start();
t2.Start();
System.Threading.Thread.Sleep(0);
label1.Text=result1.ToString();
label2.Text=result2.ToString();
}
private void btn_Stop_Click(object sender,EventArgs e)
{
t1.Abort(); // The stop button terminates the running threads if the user
t2.Abort(); // press it in case of long calculation time (for example).
}
private void Linear()
{
// Assume that the procedure (and the next one) performs many complex calculations
// which are completed after a long time but not always.
result1=x+x;
}
private void Power()
{
result2=y*y;
}
}
}
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 WindowsApplication1
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Fire_Click(object sender,EventArgs e)
{
int x,y;
int result1,result2;
Random k=new Random();
x=k.Next(50);
y=k.Next(60);
/*
Here is the problem........
How can I call the 2 threads, passing the x,y parameters
and the return value of 2 threads to be stored in variables
result1, result2 correspondingly????????
*/
System.Threading.Thread.Sleep(0);
label1.Text=result1.ToString();
label2.Text=result2.ToString();
}
private void btn_Stop_Click(object sender,EventArgs e)
{
// How we can terminate the running threads????
}
private int Linear(int x)
{
// .......... complex calculations
return x+x;
}
private int Power(int y)
{
//....... complex calculations
return y*y;
}
}
}
private delegate double MethodToCall(int input);
MethodToCall callLinear = new MethodToCall(Linear);
MethodToCall callPower = new MethodToCall(Power);
private void UpdateForm(IAsyncResult myData)
{
if (myData.IsCompleted)
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(delegate() { UpdateForm(myData); }));
else
{
// Get a handle on the original delegate and get the results.
MethodToCall bob = ((AsyncResult)myData).AsyncDelegate as MethodToCall ;
label7.Text = bob.EndInvoke(myData).ToString();
}
}
}
callLinear.BeginInvoke(variableToPass, new AsyncCallback(UpdateForm), null);
callPower.BeginInvoke(variableToPass, new AsyncCallback(UpdateForm), null);
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;
}
}
}
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;
}
}
}
t1.Start();
t2.Start();
[COLOR="YellowGreen"]// Need to wait for the thread to complete here, or
// better still get the thread to notify you when it
// has finished.[/COLOR]
int result1=regression1.result1;
int result2=regression2.result2;
I will try this but the stop button will respond immediately?set the thread priority to the highest and then have a sleep in there
I knew this mate that's why I asked how we can get the correct values waiting threads completenessI think you're getting incorrect results because you're not waiting for the thread to complete before you access the return value?
I knew this mate that's why I asked how we can get the correct values waiting threads completeness
private BackgroundWorker _backgroundWorker1 = new BackgroundWorker();
private BackgroundWorker _backgroundWorker2 = new BackgroundWorker();
private bool _worker1Finished = false;
private bool _worker2Finished = false;
private void StartButton_Click(object sender, EventArgs e)
{
_worker1Finished = false;
_worker2Finished = false;
_backgroundWorker1.WorkerSupportsCancellation = true;
_backgroundWorker2.WorkerSupportsCancellation = true;
_backgroundWorker1.DoWork += new DoWorkEventHandler(BackgroundWorker1_DoWork);
_backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
_backgroundWorker2.DoWork += new DoWorkEventHandler(BackgroundWorker2_DoWork);
_backgroundWorker2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
_backgroundWorker1.RunWorkerAsync(10);
_backgroundWorker2.RunWorkerAsync(7);
}
private void CancelButton_Click(object sender, EventArgs args)
{
_backgroundWorker1.CancelAsync();
_backgroundWorker2.CancelAsync();
}
void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs args)
{
int max = (int)args.Argument;
int i = 0;
while (i++ < max && _backgroundWorker1.CancellationPending == false)
{
[COLOR="YellowGreen"]// Do something time consuming.[/COLOR]
Thread.Sleep(1000);
args.Result = Environment.TickCount;
}
if (_backgroundWorker1.CancellationPending == true)
{
args.Cancel = true;
}
}
void BackgroundWorker2_DoWork(object sender, DoWorkEventArgs args)
{
int max = (int)args.Argument;
int i = 0;
while (i++ < max && _backgroundWorker2.CancellationPending == false)
{
[COLOR="yellowgreen"]// Do something time consuming.[/COLOR]
Thread.Sleep(1000);
args.Result = Environment.TickCount;
}
if (_backgroundWorker2.CancellationPending == true)
{
args.Cancel = true;
}
}
void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
if (args.Cancelled)
{
MessageBox.Show("Cancelled");
}
else if (args.Error != null)
{
MessageBox.Show(args.Error.Message);
}
else
{
if (sender == _backgroundWorker1) _worker1Finished = true;
if (sender == _backgroundWorker2) _worker2Finished = true;
if (_worker1Finished && _worker2Finished)
{
MessageBox.Show("Both threads completed");
}
}
}