穿线? [英] threading?

查看:124
本文介绍了穿线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是C#和线程技术的新手.

我想同时调用两个以上的函数.所以我是线程

private void button1_Click(对象发送者,EventArgs e)
{
线程t =新线程(新ThreadStart(连接));
t.Start();
线程t1 =新线程(新ThreadStart(connect1));
t1.Start();


}//click

当这样运行时,它正在工作.

如果是这样,我必须再次重复代码.
我的想法是,我将使用不同的参数更改相同的功能
所以我很喜欢




private void button1_Click(对象发送者,EventArgs e)
{
线程t =新线程(新ThreadStart(connectPara("sp1")));
t.Start();
线程t1 =新线程(新ThreadStart(connectPara("sp2")));
t1.Start();


}//click
我遇到了这个错误

预期方法名称
名称"t"在当前上下文中不存在

1,以后可能需要调用5个或6个以上的线程.因此,我计划使用具有不同参数的相同功能.我需要的是在线程中运行带有参数的函数
如何进行穿线.

2,另一个疑问是我怎么知道线程已完全执行或它卡在中间的某些地方

谁能帮我解决这个问题吗?

Hi all,

I am new to c# and threading.

I want to call more than two functions at the same time . So i am uisng threads

private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(connect));
t.Start();
Thread t1 = new Thread(new ThreadStart(connect1));
t1.Start();


}//click

when run like this , it is working.

If it is like this i have to repeat the code again.
What i thought is , i will change the same function with different parameter
so i did like this




private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(connectPara("sp1")));
t.Start();
Thread t1 = new Thread(new ThreadStart(connectPara("sp2")));
t1.Start();


}//click
i got this errors

Method name expected
The name ''t'' does not exist in the current context

1, I may need to call more than 5 or 6 threads later. So i am planning to use the same function with different parameters. what i need is to run functions with parameters in the thread
How can do with the threading.

2, One more doubt is how do i know that the the thread is executed fully or it got stucked some where in the middle

Can anybody help me resolve this issue

推荐答案

如果要将参数传递给线程中的函数,请使用以下命令:

If you want to pass parameters to a function in a thread use this:

Thread th = new Thread(delegate()
{
      connectPara("sp1");
});
th.Start();
Thread th1 = new Thread(delegate()
{
      connectPara("sp2");
});
th1.Start();


1)您可以使用ParameterizedThreadStart并使用带有参数的Thread.Start的重载.

1) You can use a ParameterizedThreadStart and use the overload of Thread.Start that takes a parameter.

void connectPara( object o )
{
  var s = o as string; // this will be "sp1"

  ...
}

...
var t = new Thread( connectPara );
t.Start( "sp1" );
...



2)有很多方法可以在线程之间进行通信.如果只想检查线程是否已完成,则可以使用Thread.IsAliveThread.ThreadState.

尼克



2) There are many ways to communicate between threads. If you just want to check if a thread has completed, you could use Thread.IsAlive or Thread.ThreadState.

Nick


1.在线程启动时传递参数的最佳方法是使用变量和要公开的方法创建一个类,然后调用对象实例的方法(包含变量)http://www.yoda.arachsys.com/csharp/threadstart.html )

2.可以检查线程的状态以确定线程是否参与竞争. ( C#中的线程简介)
1. The best way to pass parameters on thread start is to create a class with the variables and the method you would like to expose and then, invoke the method of the object instance (which holds the variables) http://www.yoda.arachsys.com/csharp/threadstart.html)

2. The status of the thread can be checked to determine if the thread competed or not. (Introduction to Threads in C#)


这篇关于穿线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆