c sharp multithreading
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的更多相关文章
- [.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(三) 利用多线程提高程序性能(下)
[.net 面向对象程序设计进阶] (18) 多线程(Multithreading)(二) 利用多线程提高程序性能(下) 本节导读: 上节说了线程同步中使用线程锁和线程通知的方式来处理资源共享问题,这 ...
- [.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中)
[.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中) 本节要点: 上节介绍了多线程的基本使用方法和基本应用示例,本节深入介绍.NET ...
- [.net 面向对象程序设计进阶] (16) 多线程(Multithreading)(一) 利用多线程提高程序性能(上)
[.net 面向对象程序设计进阶] (16) 多线程(Multithreading)(一) 利用多线程提高程序性能(上) 本节导读: 随着硬件和网络的高速发展,为多线程(Multithreading) ...
- Implicit and Explicit Multithreading MULTITHREADING AND CHIP MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The concept of thread ...
- MULTITHREADING AND CHIP MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The most important me ...
- 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 ...
- 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 ...
- 安装 nodejs图像处理模块 sharp
sudo npm install sharp 报错: ERROR: Please install libvips by running: brew install homebrew/science/v ...
- 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 ...
随机推荐
- 第八篇:Jmeter的分布式测试
一: 由于Jmeter本身的瓶颈,当模拟数以千计的用户并发的时候,使用单台机器会有些力不从心,甚至还会引起Java内存溢出的错误,要解决这个问题,就要使用分布式测试,运行多台机器,也就是所谓的Agen ...
- 【Scheme】cons的过程性实现
(define (cons x y) (define (dispatch m) (cond ((= m 0) x) ((= m 1) y))) dispatch) (define (car z) (z ...
- Oracle的regexp_instr函数简单用法
REGEXP_INSTR函数让你搜索一个正则表达式模式字符串.函数使用输入字符集定义的字符进行字符串的计算. 它返回一个整数,指示开始或结束匹配的子位置,这取决于return_option参数的值. ...
- cf-Global Round2-C. Ramesses and Corner Inversion(思维)
题目链接:http://codeforces.com/contest/1119/problem/C 题意:给两个同型的由0.1组成的矩阵A.B,问A能否经过指定的操作变成B,指定操作为在矩阵A中选定一 ...
- NumPy 字符串函数
NumPy 字符串函数 以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内置库中的标准字符串函数. ...
- Gym - 101911B Glider(前缀和+二分)
传送门:点我 A plane is flying at a constant height of hh meters above the ground surface. Let's consider ...
- FortiGate恢复出厂
1.需求 当需要把设备的所有配置删除,可进行恢复出厂操作.恢复出厂后所有配置都将丢失,若一定要恢复出厂设置建议前先备份当前的配置:备份配置操作步骤请参见"系统管理">> ...
- crm作业知识点集合[二]
知识点1 前面我们实现了这个功能,就是在models中如果有了choice选项,我们可以实现在页面显示这个chocice的value值,而不是key值,我们这个知识点就是在优化一下这个点 首先如果表中 ...
- shell脚本小集锦
1) 如何向脚本传递参数 ? ./script argument 例子: 显示文件名称脚本 ./show.sh file1.txt cat show.sh #!/bin/bash 2) 如何在脚本中使 ...
- httpclient的简单使用
1.通过get请求后台,注意tomcat的编码设置成utf-8; <Connector connectionTimeout="20000" port="808 ...