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 ...
随机推荐
- VR/AR 科技了解
Dream学院学习资料: VR/AR科技学习需要先学习NDK技术 AR/VR->图像学->图像处理(OpenCV->Intel公司在1999年发布3.2).图像绘制渲染(OpenGL ...
- jQuery文档就绪事件
[jQuery文档就绪事件] 为了防止文档在完全加载(就绪)之前运行 jQuery 代码.如果在文档没有完全加载之前就运行函数,操作可能失败. $(document).ready(function() ...
- 为什么要用MarkDown?
[为什么要用MarkDown?] 大部分作家用 Word 或 Pages 写作,过去的文档也大都以 .doc, .docx 格式或是 Pages 格式储存.还有人为了保证文稿发给谁都能正常打开,会用 ...
- kafka集群压力测试--基础。
1.生产者测试 kafka-producer-perf-test.bat --num-records 1000000 --topic test --record-size 200 --throughp ...
- 解决镜像无法删除的问题multiple repositories
Error response from daemon: conflict: unable to delete ea5f89e79b1e (must be forced) - image is refe ...
- Zabbix配置优化
1.zabbix开启中文语言zabbix是一个多语言监控系统,默认使用英文并且也支持中文语言,详见<zabbix汉化方法>,但是安装zabbix里面看不到中文语言.请往下看: 是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成.系统中的各个微服务可被独立部署,各个微服务之间是松耦合的.每个微服务仅关注于完成一件任务并很好地完成该任 ...
- vue总结2
1. 给router-link添加事件 之前用v-link 现在用 router-link 添加事件要用原生的.native修饰v-on <my-component v-on:click.nat ...
- 基于RBAC权限验证, 中间价middleware实现, views 登录视图代码
废话不多说 上代码: 基础实现: rom django.shortcuts import HttpResponse, redirect, render from django.http import ...