求 c# 创建多线程的作用和方法

 我来答
匿名用户
2013-06-03
展开全部
在.NET中,多线程功能是在System.Threading名称空间中定义的。因此,在使用任何线程类之前,必须定义 System.Threading名称空间。定义方法如下:using System.Threading;启动线程System.threading名称空间中的Thread类代表一个线程对象,用这个类对象可以创建新的线程,删除、暂停和恢复线程。 下面的代码使用Thread类创建一个新的线程,然后启动这个线程:thread = new Thread(new ThreadStart( WriteData ));thread.Start();其中WriteData是这个线程要执行的一个函数,代码如下:protected void WriteData(){string str ;for ( int i = 0; i=10000; i++ ){str = "Secondary Thread" + i.ToString();Console.WriteLine(listView1.ListItems.Count, str, 0, new string[]{""} );Update();}}杀死线程Thread类的Abort方法用于永久地杀死一个线程。但是请注意,在调用Abort方法前一定要判断线程是否还激活,也就是判断thread.IsAlive的值:if ( thread.IsAlive ){thread.Abort();}暂停线程Thread.Sleep方法用于将一个线程暂停一段时间,代码如下:thread.Sleep();设置线程的优先权我们可以使用Thread类的ThreadPriority属性设置线程的优先权。线程优先权的取值范围是Normal、AboveNormal、BelowNormal、Highest或者Lowest。请看下面的设置代码:thread.Priority = ThreadPriority.Highest;延迟线程Thread类的Suspend方法可以延迟一个线程。线程被延迟到调用Resume方法为止。if (thread.ThreadState = ThreadState.Running ){thread.Suspend();}恢复被延迟的线程调用Resume方法可以恢复一个被延迟的线程。如果线程没有被延迟,Resume方法就是无效的。if (thread.ThreadState = ThreadState.Suspended ){thread.Resume();}
匿名用户
推荐于2017-06-12
展开全部
首先不是为了多线程而多线程,多线程会极大的带来额外的出错的几率。
C#中第一个打开窗口的线程是主线程,也是处理UI的线程,最好保持这个线程通畅,即不要有阻塞操作,如Thread.Sleep(10);等这样是不好的。
耗时的线程需要打开新的线程来操作,而且最好把IsBackground属性设为True。这样在所有前台线程推出后,这些后台线程也自动退出。 手写俩例子,有错自己查!!一、首先初始化一个线程,
需要一个threadStart实例,
Thread的构造函数 public Thread(ThreadStart threadStart)

那就再看threadStart的构造函数

public threadStart(Delegate delegate)

需要一个委托。直接用函数名也可以。

比如你要另开一线呈执行的一个方法名为 newThread

那就如下

ThreadStart ThreadS=new ThreadStart(newThread);

Thread t=new Thread(ThreadS);

t.Start();二、using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ConsoleApplication3
{
public partial class Form1 : Form
{
private Thread th1, th2;

public Form1()
{
InitializeComponent();
}
public void thOpr1()
{
Application.Run(new Form2());
}
public void thOpr2()
{
Application.Run(new Form3());
}
public static void Main(String[] args)
{
Application.Run(new Form1());
}

private void button1_Click(object sender, EventArgs e)
{
th1 = new Thread(new ThreadStart(thOpr1));
th2 = new Thread(new ThreadStart(thOpr2));
th1.Start();
th2.Start();
}
}
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
摔跤吧蚂蚁
2019-04-14 · TA获得超过688个赞
知道小有建树答主
回答量:418
采纳率:100%
帮助的人:175万
展开全部
thread t1=new thread(new threadstart(你的不带参数方法))
或者
thread t1=newthread(new parsethreadstart(你的代参数方法))

然后
t1.start开始
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式