1.  静态方法

 using System;
using System.Threading; namespace PlusThread
{
class Program
{
static void Main(string[] args)
{
//创建无参的线程
//Thread thread1 = new Thread(new ThreadStart(Thread1));
Thread thread1 = new Thread( (Thread1));
thread1.Start();
Console.ReadLine();
} static void Thread1()
{
Console.WriteLine("这是无参的方法");
}
}
}

2.实例方法

 using System;
using System.Threading; namespace PlusThread
{
class Program
{
static void Main(string[] args)
{
testThread test = new testThread();
Thread t1 = new Thread(new ThreadStart(test.fun));
t1.Start(); Console.ReadLine();
} } class testThread
{
public void fun()
{
Console.WriteLine("这是实例方法");
}
} }

简洁写法:

 using System;
using System.Threading; namespace PlusThread
{
class Program
{
static void Main(string[] args)
{ Thread t1 = new Thread(delegate() { Console.WriteLine("匿名委托创建线程"); });
Thread t2 = new Thread(()=> { Console.WriteLine("lambda创建线程"); Console.WriteLine("hello"); });
t1.Start();
t2.Start();
Console.ReadLine();
}
}
}

3. 带参数实例

 using System;
using System.Threading; namespace PlusThread
{
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(new ParameterizedThreadStart(testThread ));
t1.Start();
Console.ReadLine();
}
static void testThread(object obj)
{
Console.WriteLine("带参数实例");
}
}
}

4. 线程基本信息

 using System;
using System.Threading; namespace PlusThread
{
class Program
{
static void Main(string[] args)
{
//获取正在运行的线程
Thread thread = Thread.CurrentThread;
//设置线程的名字
thread.Name = "主线程";
//获取当前线程的唯一标识符
int id = thread.ManagedThreadId;
//获取当前线程的状态
ThreadState state = thread.ThreadState;
//获取当前线程的优先级
ThreadPriority priority = thread.Priority;
string strMsg = string.Format("Thread ID:{0}\n" + "Thread Name:{1}\n" +
"Thread State:{2}\n" + "Thread Priority:{3}\n", id, thread.Name,
state, priority); Console.WriteLine(strMsg); Console.ReadKey();
}
}
}

5. 前后台线程

 using System;
using System.Threading; namespace PlusThread
{
class Program
{
static void Main(string[] args)
{
BgTest bg = new BgTest();
Thread fThread = new Thread(new ThreadStart(bg.Run));
fThread.Name = "前台线程"; BgTest bg1 = new BgTest();
Thread bThread = new Thread(new ThreadStart(bg1.Run));
bThread.Name = "后台线程";
bThread.IsBackground = true; fThread.Start();
bThread.Start();
Console.ReadLine();
}
} class BgTest
{
private int Count;
public BgTest(int count)
{
this.Count = count;
}
public void Run()
{
string threadName = Thread.CurrentThread.Name;
for (int i = ; i < Count; i++)
{
Console.WriteLine("{0}计数:{1}", threadName, i.ToString());
Thread.Sleep();
}
Console.WriteLine("{0}完成计数", threadName);
}
}
}

6. 跨线程访问控件

6.1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
} private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(test);
t.Start();
Console.ReadLine(); void test()
{
for(int i=;i<;i++)
{
textBox1.Text = i.ToString();
Thread.Sleep();
}
}
}
}
}

6.2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private delegate void setCallBack(int value);
private setCallBack setcb;
private void button1_Click(object sender, EventArgs e)
{
setcb = new setCallBack(setNum);
Thread t = new Thread (test);
t.Start();
void test()
{
for(int i=;i<;i++)
{
textBox1.Invoke(setcb, i);
}
} void setNum(int i)
{
textBox1.Text = i.ToString();
Thread.Sleep();
}
}
}
}

7.

参考:

https://www.cnblogs.com/dotnet261010/p/6159984.html

c sharp multithreading的更多相关文章

  1. [.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(三) 利用多线程提高程序性能(下)

    [.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(二) 利用多线程提高程序性能(下) 本节导读: 上节说了线程同步中使用线程锁和线程通知的方式来处理资源共享问题,这 ...

  2. [.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中)

    [.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中) 本节要点: 上节介绍了多线程的基本使用方法和基本应用示例,本节深入介绍.NET ...

  3. [.net 面向对象程序设计进阶] (16) 多线程(Multithreading)(一) 利用多线程提高程序性能(上)

    [.net 面向对象程序设计进阶] (16) 多线程(Multithreading)(一) 利用多线程提高程序性能(上) 本节导读: 随着硬件和网络的高速发展,为多线程(Multithreading) ...

  4. Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...

  5. MULTITHREADING AND CHIP MULTIPROCESSORS

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The most important me ...

  6. 16 On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima 1609.04836v1

    Nitish Shirish Keskar, Dheevatsa Mudigere, Jorge Nocedal, Mikhail Smelyanskiy, Ping Tak Peter Tang N ...

  7. Multithreading annd Grand Central Dispatch on ios for Beginners Tutorial-多线程和GCD的入门教程

    原文链接:Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial Have you ever written a ...

  8. 安装 nodejs图像处理模块 sharp

    sudo npm install sharp 报错: ERROR: Please install libvips by running: brew install homebrew/science/v ...

  9. Part 86 to 88 Talking about Multithreading in C#

    Part 86   Multithreading in C# What is a Process: Process is what the operating system uses to facil ...

随机推荐

  1. 导出pdf

    document.getElementById("exportSiteInfoTemp").onclick = function() { var thisMinheight=$(& ...

  2. HTTP协议之请求

    HTTP请求 组成 一个http请求通常由三个部分组成: 请求行(request line) 首部(header) 主体(body) 格式如下所示 <request-line><CR ...

  3. UML中的关联,泛化,依赖,聚集,组合(转)

    转自:http://blog.sina.com.cn/s/blog_5f8b45f20100dzjo.html 关联(association): 这是一种很常见的关系,这种关系在我们的生活中到处可见, ...

  4. BeautifulSoup中查找元素 select() 和find()区别

    从html中查找元素,之前一般都用find(),查找符合条件的第一个,如下 f = open(file, 'r') # 读取文件内容content = f.read()soup= BeautifulS ...

  5. oracle 调试数据库

    转载:https://www.cnblogs.com/liuqiyun/p/6589814.html 工具/原料   PL\SQL Oracle 方法/步骤     首先在PL/SQL的左侧资源栏中展 ...

  6. html中相对(relative),绝对(absolute)位置以及float的学习和使用案例 (转)

    这几天着手于CSS的研究,研究的原因主要是工作需要,最近发现如果做前端仅仅会javascript很难尽善尽美,当然懂样式和html在一定程度上可以让我们更近一步. css较为简单,由于个人擅长编写代码 ...

  7. node搭建简单的本地服务器

    首先要安装node,方法很多,可以去网上找找,可以直接去官网下载安装,新版本的node是自带npm的: 安装好以后,新建一个js文件,名为server.js: let http = require(' ...

  8. 全国高校绿色计算大赛 预赛第一阶段(C++)第3关:旋转数组

    挑战任务 在计算机中,一张数字图像,可以被看做是一个矩阵或者说数组. 学过线性代数的同学对矩阵肯定不陌生.一般来说,图像是一个标准的矩形,有着宽度(width)和高度(height).而矩阵有着行(r ...

  9. Android 心跳动画

    直接上代码  MainActivity public class MainActivity extends AppCompatActivity { private ImageView ivHart; ...

  10. js对键盘输入事件绑定到特定按钮

    转自:https://www.cnblogs.com/liluping860122/archive/2013/05/25/3099103.html<script type="text/ ...