[C#多线程]如何在线程中调用多个要传递参数并且有返回值的方法?

isee1223 2009-01-07 10:53:24
比如有方法
public string Method_1(int a,int b)
{
//
}
public string Method_2(int a,int b)
{
//
}
...
public string Method_n(int a,int b)
{
//
}
Thread myThread=new Thread...
想在myThread上调用多个方法.
而且a,b参数都必须由主线程传递...

不知道大家有没有理解我的意思?
给个思路!
...全文
3464 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
毅力橙 2011-10-28
  • 打赏
  • 举报
回复
不能提供答案不要乱发
NET_Cyberspace 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hdngo 的回复:]
2.0的话~~
多个参数可以都封到object参数里~
这个方法简单,但不安全~~

C# code

ParameterizedThreadStart ParStart = new ParameterizedThreadStart(ThreadMethod);
Thread myThread = new Thread(ParStart);
object o = "hello";
……
[/Quote]

这里使用了t.join();方法后,如果t进程执行任务的时间为3秒钟,那在这3秒钟内,主线程是不能做任何事情的,也就是主窗体会卡住动不了,表现为就是如果在这3秒内,我想移动主窗体,就动不了,甚至会出现未响应,请问这个问题怎么解决呢?
ljm10 2009-01-08
  • 打赏
  • 举报
回复
先封装一个类,类实现你的所有方法,然后在线程要调用含有的这个类的实例的方法。
[code=C#]
class A
{
private B b;

private class B
{
int a,b;
public int c;

public B(int a,int b)
{
this.a=a;
this.b=b;
}

public void Method_1()
{

c=a+b;
}
public void Method_2()
{
c=a-b;
}
}

void compute()
{
b.Method_1();
b.Method_2();
}

public static void main()
{
b=new B(1,2);
Thread t=new Thread(new ThreadStart(compute));
t.start();
Console.Write(b.c);
}
}
[code]
lunat 2009-01-08
  • 打赏
  • 举报
回复
居然用了return作为变量名....不好意思 你自己改吧
lunat 2009-01-08
  • 打赏
  • 举报
回复
(object[])new Thread(delegate(object ret)
...把(object[])去掉
lunat 2009-01-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 isee1223 的帖子:]
比如有方法
public string Method_1(int a,int b)
{
//
}
public string Method_2(int a,int b)
{
//
}
...
public string Method_n(int a,int b)
{
//
}
Thread myThread=new Thread...
想在myThread上调用多个方法.
而且a,b参数都必须由主线程传递...

不知道大家有没有理解我的意思?
给个思路!
[/Quote]


object[] return = new object[methodCount];
(object[])new Thread(delegate(object ret)
{
object[] rets = (object[])ret;
rets[0]=Method1(a,b);
rets[1]=Method2(c,d);
rets[2]=Method3(e,f)};
//...
}).Start(return);
yhy0611 2009-01-08
  • 打赏
  • 举报
回复
楼上都有答案了,我来接点分^_^
HDNGO 2009-01-07
  • 打赏
  • 举报
回复
2.0的话~~
多个参数可以都封到object参数里~
这个方法简单,但不安全~~

ParameterizedThreadStart ParStart = new ParameterizedThreadStart(ThreadMethod);
Thread myThread = new Thread(ParStart);
object o = "hello";
myThread.Start(o);

//ThreadMethod如下:
public void ThreadMethod(object ParObject)
{
//程序代码
}


推荐安全方式~~
将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。



using System;
using System.Threading;

//ThreadWithState 类里包含了将要执行的任务以及执行任务的方法
public class ThreadWithState {
//要用到的属性,也就是我们要传递的参数
private string boilerplate;
private int value;

//包含参数的构造函数
public ThreadWithState(string text, int number)
{
boilerplate = text;
value = number;
}

//要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用
public void ThreadProc()
{
//这里就是要执行的任务,本处只显示一下传入的参数
Console.WriteLine(boilerplate, value);
}
}

//用来调用上面方法的类,是本例执行的入口
public class Example {
public static void Main()
{
//实例化ThreadWithState类,为线程提供参数
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.", 42);

// 创建执行任务的线程,并执行
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
}
}








much0726 2009-01-07
  • 打赏
  • 举报
回复
使用线程池就可以了。

111,130

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧