2010-04-15 41 views
5

我想请与aruguments螺纹的方法,然后返回这里的一些值例子如何调用与aruguments螺纹的方法和返回某个值

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像这样

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

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

+0

我觉得这些例子足够了。如果这个答案与你的期望不符,那么给出完整的方案和你需要的东西。 – Mohanavel 2010-04-21 14:25:12

回答

6

这可能是另一种方法。这里的输入是作为参数化的线程传递的,并且返回类型在委托事件中传递,以便当线程完成时调用委托。当线程完成时,这将很好地得到结果。

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

您应该能够使用匿名方法或lambda提供全面静态检查:

Thread FirstThread = new Thread(() => Fun1(5, 12)); 

,或者如果你想做一些事情的结果:

Thread FirstThread = new Thread(() => { 
    int i = Fun1(5, 12); 
    // do something with i 
}); 

但要注意,这种“做什么”仍然运行在新线程的上下文(但访问其他变量外法(Main)“捕获变量的礼貌“)。

如果你有C#2.0(而不是以上),则:

Thread FirstThread = new Thread((ThreadStart)delegate { Fun1(5, 12); }); 

Thread FirstThread = new Thread((ThreadStart)delegate { 
    int i = Fun1(5, 12); 
    // do something with i 
}); 
+0

谢谢,如果有其他方法可用... – ratty 2010-04-15 06:14:33

+0

@ratty - 你究竟想要什么? – 2010-04-15 06:45:54

0

您可以使用ParameterizedThreadStart超负荷Thread构造。它允许你将一个对象作为参数传递给你的线程方法。这将是一个Object参数,所以我通常为这些线程创建一个参数类。此对象还可以存储线程执行的结果,以便在线程结束后读取。

不要忘记,在线程运行时访问此对象是可能的,但不是“线程安全的”。你知道演练:)

这里有一个例子:

void Main() 
{ 
    var thread = new Thread(Fun); 
    var obj = new ThreadObject 
    { 
     i = 1, 
     j = 15, 
    }; 

    thread.Start(obj); 
    thread.Join(); 
    Console.WriteLine(obj.result); 
} 

public static void Fun(Object obj) 
{ 
    var threadObj = obj as ThreadObject; 
    threadObj.result = threadObj.i + threadObj.j; 
} 

public class ThreadObject 
{ 
    public int i; 
    public int j; 
    public int result; 
} 
0

对于一些替代品;柯里:

static ThreadStart CurryForFun(int i, int j) 
{ // also needs a target object if Fun1 not static 
    return() => Fun1(i, j); 
} 

Thread FirstThread = new Thread(CurryForFun(5, 12)); 

或写自己的捕获型(这是大致相若什么编译器为你做,当你使用匿名的方法/缴获变量lambda表达式,但已经实现方式不同):

class MyCaptureClass 
{ 
    private readonly int i, j; 
    int? result; 
    // only available after execution 
    public int Result { get { return result.Value; } } 
    public MyCaptureClass(int i, int j) 
    { 
     this.i = i; 
     this.j = j; 
    } 
    public void Invoke() 
    { // will also need a target object if Fun1 isn't static 
     result = Fun1(i, j); 
    } 
} 
... 
MyCaptureClass capture = new MyCaptureClass(5, 12); 
Thread FirstThread = new Thread(capture.Invoke); 
// then in the future, access capture.Result 
1

有更简单的方法在单独的线程来执行功能:

// Create function delegate (it can be any delegate) 
var FunFunc = new Func<int, int, int>(fun1); 
// Start executing function on thread pool with parameters 
IAsyncResult FunFuncResult = FunFunc.BeginInvoke(1, 5, null, null); 
// Do some stuff 
// Wait for asynchronous call completion and get result 
int Result = FunFunc.EndInvoke(FunFuncResult); 

该功能将在线程池中的线程上执行,而逻辑是完全透明的应用程序。 一般来说,我建议在线程池而不是专用线程上执行这样的小任务。

7

我喜欢Mark Gravell的回答。您可以通过一点修改将结果传回给主线程:

int fun1, fun2; 
Thread FirstThread = new Thread(() => { 
    fun1 = Fun1(5, 12); 
}); 
Thread SecondThread = new Thread(() => { 
    fun2 = Fun2(2, 3); 
}); 

FirstThread.Start(); 
SecondThread.Start(); 
FirstThread.Join(); 
SecondThread.Join(); 

Console.WriteLine("Fun1 returned {0}, Fun2 returned {1}", fun1, fun2);