public int login(){
return 123;
}
用线程启动login怎么获得它的返回值,同时窗体怎么知道该线程结束了,希望有详细点的例子
线程返回值 C#
答案:1 悬赏:70 手机版
解决时间 2021-01-31 11:02
- 提问者网友:人生佛魔见
- 2021-01-30 21:11
最佳答案
- 五星知识达人网友:旧脸谱
- 2021-01-30 22:02
把对象当作参数,把需要返回的对象的指针传过去。
partial class ThreadParameter
{
public int a;
public int b;
public int result;
}
class Program
{
static void Main( string[] args )
{
Thread thread = new Thread( ThreadProc );
ThreadParameter parameter = new ThreadParameter();
parameter.a = 100;
parameter.b = 0;
parameter.result = 0;
thread.Start( parameter );
while ( thread.IsAlive )
{
Console.WriteLine( ", b = ", DateTime.Now, parameter.b );
Thread.Sleep( 1000 );
}
Console.ReadKey();
}
static void ThreadProc( object parameter )
{
ThreadParameter p = ( ThreadParameter ) parameter;
int c;
for ( int i = 0; i < 100; i++ )
{
c = i;
p.b = c;
Thread.Sleep( 500 );
}
p.result = ( int ) DateTime.Now.Ticks;
}
}
partial class ThreadParameter
{
public int a;
public int b;
public int result;
}
class Program
{
static void Main( string[] args )
{
Thread thread = new Thread( ThreadProc );
ThreadParameter parameter = new ThreadParameter();
parameter.a = 100;
parameter.b = 0;
parameter.result = 0;
thread.Start( parameter );
while ( thread.IsAlive )
{
Console.WriteLine( ", b = ", DateTime.Now, parameter.b );
Thread.Sleep( 1000 );
}
Console.ReadKey();
}
static void ThreadProc( object parameter )
{
ThreadParameter p = ( ThreadParameter ) parameter;
int c;
for ( int i = 0; i < 100; i++ )
{
c = i;
p.b = c;
Thread.Sleep( 500 );
}
p.result = ( int ) DateTime.Now.Ticks;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯