如何调用与aruguments螺纹的方法和返回某个值 [英] how to call the method in thread with aruguments and return some value

查看:144
本文介绍了如何调用与aruguments螺纹的方法和返回某个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢把与aruguments螺纹的方法和这里返回一些值例如

i like to call the method in thread with aruguments and return some value here example

class Program
    {
        static void Main()
        {
            Thread FirstThread = new Thread(new ThreadStart(Fun1));
            Thread SecondThread = new Thread(new ThreadStart(Fun2));
            FirstThread.Start();
            SecondThread.Start();        


        }
        public static void Fun1()
        {
            for (int i = 1; i <= 1000; i++)
            {
                Console.WriteLine("Fun1 writes:{0}", i);
            }
        }
        public static void Fun2()
        {
            for (int i = 1000; i >= 6; i--)
            {
                Console.WriteLine("Fun2 writes:{0}", i);
            }
        }

    }

我知道这上面的例子中成功运行,但如果方法FUN1这样

i know this above example run successfully but if method fun1 like this

public int fun1(int i,int j)
{
    int k;
    k=i+j;
    return k;
}

那我怎么才能在线程中调用此。是否有可能。任何身体帮助me..i需要所有可能的方面,这

then how can i call this in thread. Is it possible .Any body Help for me..i need all possible ways this

推荐答案

这可能是另一种方法。在这里,输入参数为主题过去了,返回类型是在委托事件过去了,这样,当线程完成,将调用的委托。这将是罚款线程完成时得到的结果。

This may be another approach. Here input is passed as parameterized Thread and return type is passed in the delegate event, so that when the thread complete that will call the Delegate. This will be fine to get result when the thread completes.

 public class ThreadObject
    {
        public int i;
        public int j;
        public int result;
        public string Name;
    }



    public delegate void ResultDelegate(ThreadObject threadObject);

    public partial class Form1 : Form
    {

        public event ResultDelegate resultDelete;

        public Form1()
        {
            InitializeComponent();

            resultDelete += new ResultDelegate(resultValue);
        }

        void resultValue(ThreadObject threadObject)
        {
            MessageBox.Show("Thread Name : " + threadObject.Name + " Thread Value : " + threadObject.result);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadObject firstThreadObject = new ThreadObject();
            firstThreadObject.i = 0;
            firstThreadObject.j = 100;
            firstThreadObject.Name = "First Thread";

            Thread firstThread = new Thread(Fun);
            firstThread.Start(firstThreadObject);


            ThreadObject secondThreadObject = new ThreadObject();
            secondThreadObject.i = 0;
            secondThreadObject.j = 200;
            secondThreadObject.Name = "Second Thread";

            Thread secondThread = new Thread(Fun);
            secondThread.Start(secondThreadObject);

        }


        private void Fun(object parameter)
        {
            ThreadObject threadObject = parameter as ThreadObject;

            for (; threadObject.i < threadObject.j; threadObject.i++)
            {
                threadObject.result += threadObject.i;

                Thread.Sleep(10);
            }

            resultValue(threadObject);
        }
    }

这篇关于如何调用与aruguments螺纹的方法和返回某个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆