标签:style blog http io ar os 使用 sp for
今天在云和学院学习了多线程
进程:计算机开启的一个正在运行的软件,在我们的操作系统上成为一个进程。N个进程。
线程:指的是在一个进程里开辟多个功能来同时执行多件任务。
前台线程:我们的UI界面使用的是系统给我们默认的前台线程
后台线程:指的是我们的自己定义的线程对象
前台线程终止之后,后台线程不会结束。
后台线程终止之后,前台线程会结束。
进程:
Process[] prc = Process.GetProcesses();
foreach (var item in prc)
{
Console.WriteLine(item);
}
Console.ReadKey();


bool b = false;
Thread th = null;
private void button1_Click(object sender, EventArgs e)
{
if(!b)
{
button1.Text = "终止";
b = true;
Control.CheckForIllegalCrossThreadCalls = false;
th = new Thread(Priase);
th.Start();
}
else
{
button1.Text = "抽奖";
b = false;
th.Abort();
}
}
private void Priase()
{
Random r = new Random();
while(true)
{
textBox1.Text = r.Next(0,10).ToString();
textBox2.Text = r.Next(0,10).ToString();
textBox3.Text = r.Next(0,10).ToString();
}
}

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 多线程
{
class Program
{
private static int count = 0;
static void Main(string[] args)
{
var t1 = new Thread(AddCount);
var t2 = new Thread(AddCount);
t1.Start();
t2.Start();
Console.ReadKey();
}
static void AddCount()
{
while (true)
{
int temp = count;
Thread.Sleep(1000);
count = temp + 1;
Console.WriteLine("我的托管线程ID为:" + Thread.CurrentThread.ManagedThreadId + " 目前count的值为:" + count);
Thread.Sleep(1000);
}
}
}
}
标签:style blog http io ar os 使用 sp for
原文地址:http://www.cnblogs.com/songfang/p/4147259.html