在线程中调用具有返回类型或输出参数的方法 [英] Calling a method with return type or output prameter in thread
问题描述
朋友们,
我试图通过线程调用一些方法.我在asp.net中使用它.
方法如下:
私有 无效 MethodName(字符串 strParam1,输出 字符串 strParam2,输出 字符串 strParam3)
私有 int MethodName(字符串 strParam1,字符串 strParam2)
我已尽力而为,但未找到任何解决方案.能给我一些建议吗?
谢谢您的期待.
亲切的问候,
Arshad Alam
在这里看起来像一个类似的线程:
http://stackoverflow. com/questions/2642978/how-to-call-the-method-in-thread-with-aruguments-and-return-some-value [ ^ ]
处理线程的最简单方法是使用BackgroundWorker.请看以下内容:
http://www.dotnetperls.com/backgroundworker [基本背景工 [ class 程序 { 静态 无效 Main(字符串 []参数) { Func< int,> theFunc = 新 Func< int,> (GetThreadId); theFunc.BeginInvoke( 42 ,TheCallback,theFunc); Console.WriteLine(" ); Console.WriteLine(" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString( )); Console.ReadKey(); } 静态 无效 TheCallback(IAsyncResult结果) { Func< int,> theFunc =(Func< int,> )result.AsyncState; Console.WriteLine(" + theFunc.EndInvoke(result)) ; Console.WriteLine(" + System.Threading.Thread.CurrentThread .ManagedThreadId.ToString()); Console.WriteLine(" ); } 静态 字符串 GetThreadId( int justAnInt) { 返回 System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()+ " + justAnInt.ToString(); } }
输出:
工作. . . 起始线程为10 执行该函数的线程为11,仅一个int 42 打印该线程的线程为11 Finsihed. .如您所见,该方法也会在单独的线程上返回结果.您可以使用 ISynchronizeInvoke [ BackgroundWorker [ ^ ].
您可以选择重量级的线程对象 [ ThreadPool [任务 [任务并行库:1 of n [ ^ ].
希望这会有所帮助.
Hi friends,
i am trying to call some methods via threading. I am using it in asp.net.
methods are like:
private void MethodName(string strParam1, out string strParam2,out string strParam3)
private int MethodName(string strParam1, string strParam2)
i have tried my best but not found any solution. can any give me some suggestion.
thanking you in anticipation.
warm regards,
Arshad Alam
Looks like a similar thread here:
http://stackoverflow.com/questions/2642978/how-to-call-the-method-in-thread-with-aruguments-and-return-some-value[^]
The easiest way to deal with threading is to use the BackgroundWorker. Look at the following:
http://www.dotnetperls.com/backgroundworker[^]
or
Basic Backgroundworker[^]
Kind of depends where you need your result. You could make use of a delegate, as the following example shows.
class Program { static void Main(string[] args) { Func<int,> theFunc = new Func<int,>(GetThreadId); theFunc.BeginInvoke(42, TheCallback, theFunc); Console.WriteLine("Working . . ."); Console.WriteLine("Starting thread was " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()); Console.ReadKey(); } static void TheCallback(IAsyncResult result) { Func<int,> theFunc = (Func<int,>)result.AsyncState; Console.WriteLine("The thread on which the function executed was " + theFunc.EndInvoke(result)); Console.WriteLine("The thread on which this was printed was " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString()); Console.WriteLine("Finished . . ."); } static string GetThreadId(int justAnInt) { return System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + ", just an int " + justAnInt.ToString(); } }
Output:
Working . . . Starting thread was 10 The thread on which the function executed was 11, just an int 42 The thread on which this was printed was 11 Finsihed . . .As you see this method returns the result on a seperate thread as well. You could use an ISynchronizeInvoke[^] or something similiar to return to the calling thread though.
An alternative, as someone mentioned, could be a BackgroundWorker[^].
You could go hardcore with the quite heavyweight Thread Object[^].
Or go for the more lightweight ThreadPool[^].
Another option would be Tasks[^].
Sacha Barber actually wrote a nice series of articles on Tasks: Task Parallel Library: 1 of n[^].
Hope this helps.
这篇关于在线程中调用具有返回类型或输出参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!