2011-01-11 23 views
1

您好所有 我想調用線程的函數,而採取一些參數像線程呼叫使用線程參數化方法C#

FTPService FtpOj = new FTPService(); 
FtpOj.AvtivateFTP(item, ObjFTP, AppHelper.DoEventLog, AppHelper.DoErrorLog, AppHelper.EventMessage, strLableXmlPath, AppHelper.emailfrom); 
  1. 我怎麼能說AvtivateFTP()方法和內部傳遞參數功能?
  2. 我們可以只在線程中調用void類型的函數嗎?

回答

1

我不知道在哪裏FTPService來源於但我希望像

IAsyncReslt BeginActivate () 

在缺乏一些成員,可以使用lambda:

ThreadPool.QueueUserWorkItem(() => FtpOj.AvtivateFTP(item, ...)); 

而且質疑2:是的,但有解決方法,例如在TPL庫中,您可以定義一個返回值的任務。

+0

什麼是()內線程調用..ThreadPool.QueueUserWorkItem(()=> FtpOj.AvtivateFTP(item,...)); – Pankaj 2011-01-11 12:46:14

0

回答你的第二個問題。你可以調用返回任何值的方法。這裏是它的例子:

static void Main() 
{ 
    Func<string, int> method = Work; 
    IAsyncResult cookie = method.BeginInvoke ("test", null, null); 
    // 
    // ... here's where we can do other work in parallel... 
    // 
    int result = method.EndInvoke (cookie); 
    Console.WriteLine ("String length is: " + result); 
} 

static int Work (string s) { return s.Length; } 

此外,如果您使用的是.NET 4.0,我會推薦使用任務並行庫(TPL)。使用TPL要容易得多。

另一種暗示是因爲你有許多參數傳遞給函數,將它們包裝在一個對象中,並將該對象傳遞給你的異步方法。

希望這會有所幫助。