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);
}
}
}
最近上洛杉矶机房联通CUVIP线路主机的商家越来越多了,HostKvm也发来了新节点上线的邮件,适用全场8折优惠码,基于KVM架构,优惠后最低月付5.2美元起。HostKvm是一家成立于2013年的国人主机商,提供基于KVM架构的VPS主机,可选数据中心包括日本、新加坡、韩国、美国、中国香港等多个地区机房,君选择国内直连或优化线路,延迟较低,适合建站或者远程办公等。以洛杉矶CUVIP线路主机为例,...
我们先普及一下常识吧,每年9月的第一个星期一是美国劳工节。于是,有一些服务商会基于这些节日推出吸引用户的促销活动,比如RackNerd有推出四款洛杉矶和犹他州独立服务器,1G带宽、5个独立IP地址,可以配置Windows和Linux系统,如果有需要独立服务器的可以看看。第一、劳工节促销套餐这里有提供2个套餐。两个方案是选择犹他州的,有2个方案是可以选择洛杉矶机房的。CPU内存SSD硬盘配置流量价格...
HostKvm是一家成立于2013年的国外VPS服务商,产品基于KVM架构,数据中心包括日本、新加坡、韩国、美国、俄罗斯、中国香港等多个地区机房,均为国内直连或优化线路,延迟较低,适合建站或者远程办公等。本月,商家旗下俄罗斯、新加坡、美国、香港等节点带宽进行了大幅度升级,俄罗斯机房国内电信/联通直连,CN2线路,150Mbps(原来30Mbps)带宽起,目前俄罗斯和香港高防节点5折骨折码继续优惠中...