2011-04-05 58 views
1

我想在不同的線程中運行我的函數,但問題是函數需要參數。製作新的參數化線程

如果我嘗試它不需要paramters(CPUPlay()),其確定的函數:

private void OpenNewThread(bool open) 
    { 
     Thread thread = new Thread(new ThreadStart(CPUPlay)); 
    } 

但是,如果我有一個需要參數的函數試試吧,它不工作:

private void OpenNewThread(bool open) 
{ 
    Thread thread = new Thread(new ParameterizedThreadStart(CloseOpenAnimation)); 
    thread.Start(open); 
} 

那麼我怎樣才能運行一個函數的參數在不同的線程?

+2

真的,你們什麼時候瞭解到「不起作用」是** no **錯誤描述?! – 2011-04-05 08:10:04

回答

1

作弊和使用委託

private void OpenNewThread(bool open) 
{ 
    Thread thread = new Thread(new ThreadStart(
     () => CloseOpenAnimation(open))); 
    thread.Start(); 
} 
0

嘗試BackgroundWorker的對象。有一個可以使用的對象參數參數。

BackgroundWorker bg = new BackgroundWorker(); 
bg.DoWork+=new DoWorkEventHandler(bg_DoWork); 
bg.RunWorkerAsync(5); 

static void bg_DoWork(object sender, DoWorkEventArgs e) 
{ 
      int j = (int)e.Argument; 
} 

其中e.Argument是對象類型。

2

您的功能是否符合此簽名?

public void CloseOpenAnimation(object argument)