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. grabcut 分割 Rect

    #include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src = imread("E:\ ...

  2. python 自然语言处理库https://www.nltk.org/nltk_data/

    https://www.nltk.org/nltk_data/ https://github.com/hankcs/HanLP

  3. IDEA配置 gradle

    下载解压自己需要的gradle版本:https://gradle.org/releases/(免安装)  配置环境变量 打开命令窗口,输入 gradle -v IDEA配置gradle:file-&g ...

  4. webpack-manifest-plugin

    [webpack-manifest-plugin] Webpack plugin for generating an asset manifest. This will generate a mani ...

  5. 修改Eclipse主题与Eclipse中使用SVN

    自从开始IDEA后,很久没用eclipse了,有位老同学问我eclipse中如何使用SVN?我就打开eclipse试试,白光太刺眼了~作为强迫症修改下主题.结果如下: 修改完编辑的背景颜色,并没有很好 ...

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

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

  7. springmvc整合mybatis 配置文件

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  8. 浅谈Session与Cookie的区别与联系

    一.Session的概念 Session 是存放在服务器端的,类似于Session结构来存放用户数据,当浏览器 第一次发送请求时,服务器自动生成了一个Session和一个Session ID用来唯一标 ...

  9. django static 无法正确加载目录下的css

    在static->web目录下添加CSS后该css文件一直报404错误,解决问题: 在setting.py文件添加: STATICFILES_DIRS = [ os.path.join(BASE ...

  10. No appenders could be found for logger

    在运行代码时,出现下面的错误, log4j:WARN No appenders could be found for logger (genericTest.GenericTest). log4j:W ...