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)创建线程类

百驰云(19/月),高性能服务器,香港三网CN2 2核2G 10M 国内、香港、美国、日本、VPS、物理机、站群全站7.5折,无理由退换,IP免费换!

百驰云成立于2017年,是一家新国人IDC商家,且正规持证IDC/ISP/CDN,商家主要提供数据中心基础服务、互联网业务解决方案,及专属服务器租用、云服务器、云虚拟主机、专属服务器托管、带宽租用等产品和服务。百驰云提供源自大陆、香港、韩国和美国等地骨干级机房优质资源,包括BGP国际多线网络,CN2点对点直连带宽以及国际顶尖品牌硬件。专注为个人开发者用户,中小型,大型企业用户提供一站式核心网络云端...

妮妮云80元/月,香港站群云服务器 1核1G

妮妮云的来历妮妮云是 789 陈总 张总 三方共同投资建立的网站 本着“良心 便宜 稳定”的初衷 为小白用户避免被坑妮妮云的市场定位妮妮云主要代理市场稳定速度的云服务器产品,避免新手购买云服务器的时候众多商家不知道如何选择,妮妮云就帮你选择好了产品,无需承担购买风险,不用担心出现被跑路 被诈骗的情况。妮妮云的售后保证妮妮云退款 通过于合作商的友好协商,云服务器提供2天内全额退款,超过2天不退款 物...

JustHost俄罗斯VPS有HDD、SSD、NVMe SSD,不限流量低至约9.6元/月

justhost怎么样?justhost服务器好不好?JustHost是一家成立于2006年的俄罗斯服务器提供商,支持支付宝付款,服务器价格便宜,200Mbps大带宽不限流量,支持免费更换5次IP,支持控制面板自由切换机房,目前JustHost有俄罗斯6个机房可以自由切换选择,最重要的还是价格真的特别便宜,最低只需要87卢布/月,约8.5元/月起!总体来说,性价比很高,性价比不错,有需要的朋友可以...

queueuserworkitem为你推荐
无线路由器限速设置无线路由器能设置限速吗?或者说那个牌子的能。拂晓雅阁我对电脑操作不熟悉,想买一本自学的电脑书籍,是电脑入门那一类的,最好还有办公软件应用那一类的邮箱打不开怎么办我的邮箱打不开怎么办直播加速请问哪种播放器的可以播放加速,并且可以保存开机滚动条谁会调开机的滚动条ios7固件下载iOS的固件有正版盗版之分吗?我看到了蜂威网有iOS7的固件想下载试用一下,那里是测试版是正版吗迅雷云点播账号求个迅雷VIP 是VIP就可以 只用来看云点播 改密码是孙子。 谢了 ! 362135668@qq.comiphone6上市时间苹果6是什么时候出的 ?iphone6上市时间苹果六什么时候出的小米手柄小米手柄和飞智手柄哪个好?
greengeeks zpanel 台湾服务器 国内永久免费云服务器 网站挂马检测工具 网站卫士 东莞服务器 根服务器 cloudlink 512mb 海外空间 河南移动梦网 starry 电信网络测速器 购买空间 windowssever2008 weblogic部署 ddos攻击工具 阿里云主机 香港云主机 更多