求c#多线程实例
Thread th=new Thread(new ThreadStart(方法));
th.Name="aa" 为线程命名
th.Priority=ThreadPriority.Highest 最高 //运行的优先级
.Normal 缺省
.Lowest 最底
th.Start();
lock(对象)
{
//代码 保证一个线程执行完这段代码之后另外 //一个线才执行这段代码,线程有序
}
Start();
Sleep(毫秒数); //休眠,这个毫秒等待完成后自动继续执行
Suspend(); //挂起,不自动恢复
Resume(); //通过Resume去恢复一个挂起的线程
Abort(); //停止当前线程
示例:
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 WindowsApplication19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread th1;
Thread th2;
Thread th3;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//关闭线程
th1.Abort();
th2.Abort();
th3.Abort();
}
private void button1_Click(object sender, EventArgs e)
{
th1 = new Thread(new ThreadStart(Run1)); //固定写法
th2 = new Thread(new ThreadStart(Run2));
th3 = new Thread(new ThreadStart(Run3));
th1.Priority = ThreadPriority.Highest; //设置优先级
th2.Priority = ThreadPriority.AboveNormal;
th3.Priority = ThreadPriority.Normal;
th1.Name = "aa"; //设置名字
th2.Name = "bb";
th3.Name = "cc";
th1.Start(); //启动线程
th2.Start();
th3.Start();
}
private void Run1()
{
for (int i = 0; i < 100; i++)
{
this.progressBar1.Value = i;
Thread.Sleep(100);
}
}
private void Run2()
{
for (int i = 0; i < 100; i++)
{
this.progressBar2.Value = i;
Thread.Sleep(100);
}
}
private void Run3()
{
for (int i = 0; i < 100; i++)
{
this.progressBar3.Value = i;
Thread.Sleep(100);
}
}
}
}
th.Name="aa" 为线程命名
th.Priority=ThreadPriority.Highest 最高 //运行的优先级
.Normal 缺省
.Lowest 最底
th.Start();
lock(对象)
{
//代码 保证一个线程执行完这段代码之后另外 //一个线才执行这段代码,线程有序
}
Start();
Sleep(毫秒数); //休眠,这个毫秒等待完成后自动继续执行
Suspend(); //挂起,不自动恢复
Resume(); //通过Resume去恢复一个挂起的线程
Abort(); //停止当前线程
示例:
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 WindowsApplication19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread th1;
Thread th2;
Thread th3;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//关闭线程
th1.Abort();
th2.Abort();
th3.Abort();
}
private void button1_Click(object sender, EventArgs e)
{
th1 = new Thread(new ThreadStart(Run1)); //固定写法
th2 = new Thread(new ThreadStart(Run2));
th3 = new Thread(new ThreadStart(Run3));
th1.Priority = ThreadPriority.Highest; //设置优先级
th2.Priority = ThreadPriority.AboveNormal;
th3.Priority = ThreadPriority.Normal;
th1.Name = "aa"; //设置名字
th2.Name = "bb";
th3.Name = "cc";
th1.Start(); //启动线程
th2.Start();
th3.Start();
}
private void Run1()
{
for (int i = 0; i < 100; i++)
{
this.progressBar1.Value = i;
Thread.Sleep(100);
}
}
private void Run2()
{
for (int i = 0; i < 100; i++)
{
this.progressBar2.Value = i;
Thread.Sleep(100);
}
}
private void Run3()
{
for (int i = 0; i < 100; i++)
{
this.progressBar3.Value = i;
Thread.Sleep(100);
}
}
}
}
佚名
2024-06-07 20:08:46
类似问题(10)
-
佚名2024-06-07 16:50:03
问现在想写一个C程序,实现多线程操作,还希望这个多线程操作可以跨平台
答#ifdef OS_WINDOWS条件编译告诉编译器如果是windows操作系统就执行这个if下面的东西
-
佚名2024-06-07 23:23:30
问C 怎么起线程
答CreatProgress()好像是这个!翻翻windows编程方面的书吧!~来黑 吧 安 全。 技术, 论坛 。可以了解更多类似信息哦!我经常去的!推荐你...
-
佚名2024-06-07 05:28:10
问c语言多线程
答因为您传入的是t的地址:rc = pthread_create(&thread[t], NULL, PrintHello, &t);所以在Pri...
-
佚名2024-06-07 04:50:12
问c语言多线程
答main(){if(!fork()) { //代码 //...新线程,与原线程共享数据空间 }else { //代码 //..原线...
-
佚名2024-06-07 16:47:12
问求c 语言自带的线程函数
答用API函数 HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes , SIZE_...
-
佚名2024-06-07 00:02:39
问c的多线程实现
答#include <stdio.h>#include <pthread.h>void thread(void){int i;for(i=0;i<...
-
佚名2024-06-07 22:41:08
问C/C++线程问题
答建两个createthread 分别 运行 两个对象
-
佚名2024-06-07 23:10:57
问C语言多线程编程的一个菜鸟问题
答由于是多线程环境,放一个原子变量,每次调用该函数时,让该原子变量的值递增,同时用一个全局变量记录当该变量值为初始值时的值。不知道能明白我的意思没不用原子操作,或...
-
佚名2024-06-07 08:00:00
问求 C# 多线程 实例
答//应为要判断上一个操作是否完成!用异步调用! //主线程中启动一个支线程,执行doSomething这样的一个方法。 Thread...
-
佚名2024-06-07 08:00:00
问C# 多线程函数用类实例作为参数会不会引发线程访问故障
答如果对象实例是只读的应该没问题如果有的线程读,有的线程修改就会有问题可以在修改对象实例的地方加锁,以确保所有线程读到的对象是一致的.net 提供了三种对象同步机...
风水
起名
网名
- 1 网名狼之类的
- 2 带菲字的英语网名
- 3 名知花有主网名
- 4 用p做符号网名
- 5 qq昵称符号翅膀 女生
- 6 味美的微博昵称
- 7 微博昵称改回之前名字
- 8 对莫人失望心寒的昵称
- 9 带芹字的微信昵称三字
- 10 武装突袭3怎么改昵称
说说
- 1 2016找个人过日子个性签名
- 2 佛学微信个性签名
- 3 qq 签名表情在哪弄?
- 4 家里玩具太多的说说
- 5 丢钱包的心情说说
- 6 爱上一个不可能再一起额人的说说
- 7 陪我的小侄女拍照片的说说
- 8 今天满血复活的说说
- 9 没力气搬东西的幽默说说
- 10 全身骨头疼的说说