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. 大型运输行业实战_day13_1_定时任务spring-quartz

    1.jar包 拷贝quartz-2.2.3.jar包到项目 2.编写定时任务类TicketQuart.java package com.day02.sation.task; import com.da ...

  2. Jmeter(二十五)常见问题(转载)

    转载自 http://www.cnblogs.com/yangxia-test 收集工作中JMeter遇到的各种问题   1.  JMeter的工作原理是什么? 向服务器提交请求:从服务器取回请求返回 ...

  3. day17 正则表达式 re模块和hashlib模块

    今日内容 1. re&正则表达式(*****) 注:不要将自定义文件命名为re import re re.findall(正则表达式,被匹配的字符串) 拿着正则表达式去字符串中找,返回一个列表 ...

  4. oracle 存储过程模板

    CREATE OR REPLACEPROCEDURE PROCE_NAME(V_IN varchar2, V_OUT out varchar2) ASBEGIN --...  commit;  V_O ...

  5. Java 中的 static 使用

    Java语言基础--static 0.目录 8.static 8.1 Java 中的 static 使用之静态变量 8.2 Java 中的 static 使用之静态方法 8.3 Java 中的 sta ...

  6. 《深入理解java虚拟机》笔记

    二.java内存区域与内存溢出异常 0.在内存管理领域,java与c/c++不同的是,在java虚拟机自动内存管理机制下,java不需要手动去为对象写配对的free内存的代码,不容易出现内存泄漏和内存 ...

  7. Mac IntelliJ IDEA 2017(java开发集成环境)附注册码和破解教程 v2017.3.5破解版

    原文:http://www.orsoon.com/Mac/155938.html 原文中含有软件下载地址 软件介绍 IntelliJ IDEA 2017 Mac激活版是Mac平台上的一款java开发集 ...

  8. SRVE0255E: 尚未定义要处理 ***的 Web 组/虚拟主机。

    原因: 访问的端口未设置,因此无法访问 解决办法: 1.设置端口 控制台-环境-虚拟主机-default_host-主机别名-新建 主机名根据实际情况填写 笔者这里填如下 2.设置好端口后重启对应服务 ...

  9. 在CentOS7.4上手动编译安装Mysql-5.7.20

    实验环境:CentOS 7.4 mysql软件: mysql-boost-5.7.20.tar.gz 1.安装编译工具 yum -y install \ncurses \ncurses-devel \ ...

  10. 《基于Nginx的中间件架构》学习笔记---1.环境配置

    一.环境调试确认 (四项确认) 1.确认系统网络 ping www.baidu.com 2.确认yum可用 yum list|grep gcc 3.确认关闭iptables规则 iptables -L ...