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);
}
}
}
香港服务器多少钱一个月?香港服务器租用配置价格一个月多少,现在很多中小型企业在建站时都会租用香港服务器,租用香港服务器可以使网站访问更流畅、稳定性更好,安全性会更高等等。香港服务器的租用和其他地区的服务器租用配置元素都是一样的,那么为什么香港服务器那么受欢迎呢,香港云服务器最便宜价格多少钱一个月呢?阿里云轻量应用服务器最便宜的是1核1G峰值带宽30Mbps,24元/月,288元/年。不过我们一般选...
公司成立于2007年,是国内领先的互联网业务平台服务提供商。公司专注为用户提供低价高性能云计算产品,致力于云计算应用的易用性开发,并引导云计算在国内普及。目前,旅途云公司研发以及运营云服务基础设施服务平台(IaaS),面向全球客户提供基于云计算的IT解决方案与客户服务,拥有丰富的国内BGP、双线高防、香港等优质的IDC资源。点击进入:旅途云官方网商家LOGO优惠方案:CPU内存硬盘带宽/流量/防御...
wordpress高级全行业大气外贸主题,wordpress通用全行业高级外贸企业在线询单自适应主题建站程序,完善的外贸企业建站功能模块 + 高效通用的后台自定义设置,更实用的移动设备特色功能模块 + 更适于欧美国外用户操作体验 大气简洁的网站风格设计 + 高效优化的网站程序结构,更利于Goolge等SEO搜索优化和站点收录排名。点击进入:wordpress高级全行业大气外贸主题主题价格:¥398...