queueuserworkitem如何创建线程

queueuserworkitem  时间:2021-01-17  阅读:()

C# 多线程的使用。如何使用多线程?

你用线程池(ThreadPool)可以实现,也可以用线程(Thread)实现 for (int i = 0; i < int.Parse(times); i++) //number of threads { //Console.WriteLine("thread i = " + i); //Thread oThread = new Thread(new ParameterizedThreadStart(Beta)); bool FIL = fi == "1" ? true :false; para p = new para { Finished = (i == int.Parse(times) - 1), Xml = doc.InnerXml, URL = url, num_msgs_processed = i, num_msgs_sent = i, FI = FIL }; // Start the thread //oThread.Start(p); //oThread.Join(); ThreadPool.QueueUserWorkItem(Beta, p); }

c# Socket 多线程例子

使用ThreadStart委托来衍生三个新线程

using System; using System.Threading; namespace ThreadStartSampleCS { class Program { static void Main() { Thread newThread; ThreadStart threadMethod = new ThreadStart(DoWork); for (int counter = 1; counter < 4; counter++) { Console.WriteLine("Starting Thread {0}", counter); newThread = new Thread(threadMethod); newThread.Name = counter.ToString(); newThread.Start(); } } static void DoWork() { for (int counter = 1; counter < 11; counter++) { Console.WriteLine("Thread {0}: iteration {1}", Thread.CurrentThread.Name, counter); Thread.Sleep(1); } } } } 使用ParameterizedThreadStart委托来衍生三个新线程 using System; using System.Threading; namespace ParameterizedThreadStartSampleCS { class Program { static void Main() { Thread newThread; ParameterizedThreadStart threadMethod = new ParameterizedThreadStart(DoWork); for (int counter = 1; counter < 4; counter++) { Console.WriteLine("Starting Thread {0}", counter); newThread = new Thread(threadMethod); newThread.Name = counter.ToString(); newThread.Start(5); } } static void DoWork(object iterations) { for (int counter = 1; counter < (int)iterations + 1; counter++) { Console.WriteLine("Thread {0}: iteration {1}", Thread.CurrentThread.Name, counter); Thread.Sleep(1); } } } } 使用ThreadPool类从线程池中启动线程 using System; using System.Threading; namespace ThreadPoolSampleCS { class Program { static void Main(string[] args) { ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc)); Console.WriteLine("Main thread starts"); Thread.Sleep(10000); Console.WriteLine("Main thread exits."); } static void ThreadProc(Object stateInfo) { Console.WriteLine("Hello from the thread pool. I will count from 1 to 100"); for (int counter = 1; counter < 101; counter++) { Console.WriteLine(counter); Thread.Sleep(500); } } } } 实现IAsyncResult接口和AsyncCallback委托 using System; using System.Threading; namespace AsynCallbackSampleCS { class Program { delegate int IntAsyncDelegate(int number); static IntAsyncDelegate aDelegate; static int i(int number) { if (number < 1) return 0; else if (number == 1 || number == 2) return number; else return i(number - 2) + i(number - 1); } static void DisplayResult(IAsyncResult ar) { int result = aDelegate.EndInvoke(ar); Console.WriteLine("Elemet number {0} in the i series is {1}", ar.AsyncState.ToString(), result); } static void Main(string[] args) { aDelegate = new IntAsyncDelegate(i); AsyncCallback callback = new AsyncCallback(DisplayResult); Console.Write("Enter a number: "); int number = int.Parse(Console.ReadLine()); aDelegate.BeginInvoke(number, callback, number); Console.WriteLine("wait while we process your request"); Thread.Sleep(5000); } } } 通过异步调用迁移线程的执行上下文 using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Security.Principal; using System.Threading; namespace ExecutionHostSampleCS { class Program { [DllImport("advapi32.dll")] private static extern bool LogonUser( String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken); static void Main() { System.IntPtr pToken; //请将LogonUser方法中的用户名、域名、密码替换成可以登陆本机的值。

if (LogonUser("用户名", "域名", "密码", 2, 0, out pToken)) { WindowsIdentity.Impersonate(pToken); DisplayContext("Main"); ThreadPool.QueueUserWorkItem(Callback, null); ExecutionContext ec = ExecutionContext.Capture(); ExecutionContext.SuppressFlow(); ThreadPool.QueueUserWorkItem(Callback, null); ContextCallback cb = new ContextCallback(Callback); ExecutionContext.Run(ec, cb, 0); ThreadPool.QueueUserWorkItem(Callback, null); ExecutionContext.RestoreFlow(); ThreadPool.QueueUserWorkItem(Callback, null); pToken = IntPtr.Zero; } Console.ReadLine(); } static void Callback(object o) { DisplayContext("Callback"); } static void DisplayContext(string s) { System.Console.WriteLine(s + " Thread#{0} Current user is {1}", Thread.CurrentThread.ManagedThreadId, WindowsIdentity.GetCurrent().Name); } } } 实现SynchronizationContext类和SendOrPostCallback委托 using System; using System.Threading; namespace SynchronizationContextSampleCS { class ount { public decimal balance; public void Deposit(decimal amount) { balance += amount; } public void Withdraw(decimal amount) { balance -= amount; } } class Program { static ount ount; static void Main(string[] args) { ount = new ount(); SendOrPostCallback deposit = new SendOrPostCallback(Deposit); SendOrPostCallback withdraw = new SendOrPostCallback(Withdraw); SynchronizationContext ctx = new SynchronizationContext(); ctx.Post(deposit, 500); ctx.Post(withdraw, 500); Console.ReadLine(); } static void Withdraw(object state) { Console.WriteLine("Withdraw: current balance = {0:C}", ount.balance); ount.Withdraw(decimal.Parse(state.ToString())); Console.WriteLine("Withdraw: new balance = {0:C}", ount.balance); } static void Deposit(object state) { Console.WriteLine("Withdraw: current balance = {0:C}", ount.balance); ount.Deposit(decimal.Parse(state.ToString())); Console.WriteLine("Deposit: new balance = {0:C}", ount.balance); } } }

queueuserworkitem是异步的还是同步的

串行通信是指 使用一条数据线,将数据一位一位地依次传输,每一位数据占据一个固定的时间长度。

其只需要少数几条线就可以在系统间交换信息,特别适用于计算机与计算机、计算机与外设之间的远距离通信。

在计算机和终端之间的数据传输通常是靠电缆或信道上的电流或电压变化实现的。

如果一组数据的各数据位在多条线上同时被传输,这种传输方式称为并行通信。

同步通信与异步通信区别: 1.同步通信要求接收端时钟频率和发送端时钟频率一致,发送端发送连续的比特流;异步通信时不要求接收端时钟和发送端时钟同步,发送端发送完一个字节后,可经过任意长的时间间隔再发送下一个字节。

2.同步通信效率高;异步通信效率较低。

3.同步通信较复杂,双方时钟的允许误差较小;异步通信简单,双方时钟可允许一定误差。

4.同步通信可用于点对多点;异步通信只适用于点对点。

如何创建线程

方式1:继承Thread类 步骤: 1):定义一个类A继承于Java.lang.Thread类. 2):在A类中覆盖Thread类中的run方法. 3):我们在run方法中编写需要执行的操作:run方法里的代码,线程执行体. 4):在main方法(线程)中,创建线程对象,并启动线程. (1)创建线程类

Dynadot多种后缀优惠域名优惠码 ,.COM域名注册$6.99

Dynadot 是一家非常靠谱的域名注册商家,老唐也从来不会掩饰对其的喜爱,目前我个人大部分域名都在 Dynadot,还有一小部分在 NameCheap 和腾讯云。本文分享一下 Dynadot 最新域名优惠码,包括 .COM,.NET 等主流后缀的优惠码,以及一些新顶级后缀的优惠。对于域名优惠,NameCheap 的新后缀促销比较多,而 Dynadot 则是对于主流后缀的促销比较多,所以可以各取所...

2022年最新PHP短网址生成系统/短链接生成系统/URL缩短器系统源码

全新PHP短网址系统URL缩短器平台,它使您可以轻松地缩短链接,根据受众群体的位置或平台来定位受众,并为缩短的链接提供分析见解。系统使用了Laravel框架编写,前后台双语言使用,可以设置多域名,还可以开设套餐等诸多功能,值得使用。链接: https://pan.baidu.com/s/1ti6XqJ22tp1ULTJw7kYHog?pwd=sarg 提取码: sarg文件解压密码 www.wn7...

hosteons:10Gbps带宽,免费Windows授权,自定义上传ISO,VPS低至$21/年,可选洛杉矶达拉斯纽约

hosteons当前对美国洛杉矶、达拉斯、纽约数据中心的VPS进行特别的促销活动:(1)免费从1Gbps升级到10Gbps带宽,(2)Free Blesta License授权,(3)Windows server 2019授权,要求从2G内存起,而且是年付。 官方网站:https://www.hosteons.com 使用优惠码:zhujicepingEDDB10G,可以获得: 免费升级10...

queueuserworkitem为你推荐
阿里云系统安卓系统和阿里云系统比较?那个很好?优点缺点?比较一下,最近想买,不知道选哪个系统的。邮箱群发163的邮箱如何群发伪装微信地理位置怎么伪装微信地理位置?在线漏洞检测网站好像有漏洞,直接看代码可以找出来吗?网店推广网站开网店如何做推广?硬盘人移动硬盘的优缺点安装迅雷看看播放器怎样安装迅雷看看播放器mate8价格华为mate8市场价多少发邮件怎么发发邮件怎么发?网站营运网络运营主要做些什么?
备案域名 大庆服务器租用 谷歌域名邮箱 东莞电信局 gomezpeer 鲜果阅读 好看的桌面背景图 浙江独立 怎么测试下载速度 域名转接 卡巴斯基试用版 33456 能外链的相册 免费网页空间 cloudlink 域名dns 美国凤凰城 防cc攻击 万网注册 卡巴斯基官网下载 更多