2011-01-11 61 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要容易得多。

另一种暗示是因为你有许多参数传递给函数,将它们包装在一个对象中,并将该对象传递给你的异步方法。

希望这会有所帮助。