# Passing parameters in threads. C# or Visual C++??



## MrSeanKon (Oct 21, 2007)

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 (Oct 22, 2007)

How we can convert it???? 


```
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 (Oct 22, 2007)

http://www.yoda.arachsys.com/csharp/threads/parameters.shtml


----------



## MrSeanKon (Oct 29, 2007)

Thanks W1zzard for the link.
I read the page you suggested and one version based to their ideas. 
But *here is the OCUK thread* so I don't want to post the same stuff again here.
What can I do??????


----------

