PDA

View Full Version : Passing parameters in threads. C# or Visual C++??


MrSeanKon
10-21-2007, 01:20 PM
Here is the C# 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 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;
}
}
}

MrSeanKon
10-22-2007, 11:03 AM
How we can convert it???? :rolleyes::rolleyes:


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;
}
}
}

W1zzard
10-22-2007, 11:24 AM
http://www.yoda.arachsys.com/csharp/threads/parameters.shtml

MrSeanKon
10-29-2007, 11:35 AM
Thanks W1zzard for the link.
I read the page you suggested and one version based to their ideas.
But here is the OCUK thread (http://forums.overclockers.co.uk/showthread.php?t=17793289) so I don't want to post the same stuff again here.
What can I do?????? :wtf: