怎么么取得线程的返回值啊。。。是不是用callback啊 100分相送

laohan8849 2004-10-18 02:34:49
怎么么取得线程的返回值啊。。。是不是用callback啊,谁有好的方法。最好写一段简单的代码

主要有两种 1返回一个标志用来表明线程已经结束
2取得一些数据

好像可以用委托来实现不知道怎么写啊

...全文
200 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
laohan8849 2004-10-20
  • 打赏
  • 举报
回复
get string sRtnMsg
{
retrun this.global
}
maggiecheung 2004-10-19
  • 打赏
  • 举报
回复
线程的返回值?不解。。。。。
CSTerry 2004-10-19
  • 打赏
  • 举报
回复
用全局变量吧,在线程里面修改全局变量就行了
linaren 2004-10-19
  • 打赏
  • 举报
回复
封装一下线程,这样即可以传任意的参数,也可以控制线程及取得返回值之类的
示例:
public MyThread
{
public object ThreadParam = null;
Thread workThread = null;
object workThreadRet = nulll;

public MyThread(){}
public Start(){
workThread = new Thread( new ThreadStart( proc ) );
workThread.Start();
}
public void Abort(){
if( workThread != null ) workThread.Abort();
}

void proc(){
...
workThreadRet = ...;
}
}
superryu 2004-10-19
  • 打赏
  • 举报
回复
好像线程是没有返回值的吧,还是请高手指点一下
enjoybcb 2004-10-18
  • 打赏
  • 举报
回复
线程没有返回值,参见MSDN。
使用全局变量,等方法传递数据。
laohan8849 2004-10-18
  • 打赏
  • 举报
回复
不太合适
laohan8849 2004-10-18
  • 打赏
  • 举报
回复
我先看。。一下。如果合适。马上给分
wangsaokui 2004-10-18
  • 打赏
  • 举报
回复
public static void receive()
{
try
{
Thread thr = Thread.CurrentThread;
Console.WriteLine("Main Thread State:" + thr.ThreadState);

//IPHostEntry ipHost = Dns.Resolve("127.0.0.1");
//IPAddress ipAddr = ipHost.AddressList[0];
IPAddress ipAddr = IPAddress.Parse("192.16.10.10");
IPEndPoint endPoint = new IPEndPoint(ipAddr,5001);

Socket sClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

sClient.BeginConnect(endPoint,new AsyncCallback(ConnectCallback),sClient);
socketEvent.WaitOne();
Console.WriteLine("connect ok");

sClient.BeginReceive(buffer,0,buffer.Length,0,new AsyncCallback(ReceiveCallback),sClient);
socketEvent.WaitOne();
Console.WriteLine("Response receive:{0}",theResponse);
//sClient.Shutdown(SocketShutdown.Both);
//sClient.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void ConnectCallback(IAsyncResult ar)
{
try
{
Thread thr = Thread.CurrentThread;
Console.WriteLine("ConnectCallback Thread State:" + AppDomain.GetCurrentThreadId());

Socket sClient = (Socket)ar.AsyncState;
sClient.EndConnect(ar);
Console.WriteLine("Socket connected to " + sClient.RemoteEndPoint.ToString());
socketEvent.Set();
}
catch (Exception ex)
{
//string err_message = ex.Message;
//if (err_message.IndexOf("No connection could be made because the target machine actively refused it")> -1)
//{
Console.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")+"||"+AppDomain.GetCurrentThreadId()+"||3--Level 3 Server connection is broken, waiting for Level 3 Server connection......");
// Thread.Sleep(3000);
sClient= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// sClient.BeginConnect(EPServer, new AsyncCallback(ConnectCallback),sClient);
socketEvent.Set();
// return;
// Thread.CurrentThread.Abort();
// Thread.CurrentThread.Abort();
//}
//else
//{
// Console.WriteLine(ex.ToString());
//}
}
}

public static void ReceiveCallback(IAsyncResult ar)
{
try
{
Thread thr = Thread.CurrentThread;
Console.WriteLine("ConnectCallback Thread State:" + AppDomain.GetCurrentThreadId());

Socket sClient = (Socket)ar.AsyncState;

int bytesRead = sClient.EndReceive(ar);//,i=0;

if (bytesRead>0)
{

theResponse = Encoding.ASCII.GetString(buffer,16, bytesRead);
Console.WriteLine(theResponse);
sClient.BeginReceive(buffer,0,buffer.Length,0,new AsyncCallback(ReceiveCallback),sClient);
}
else
{
Console.WriteLine("===============================");
// socketEvent.Set();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

111,126

社区成员

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

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

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