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);
}
}
}
HostYun 商家以前是玩具主机商,这两年好像发展还挺迅速的,有点在要做点事情的味道。在前面也有多次介绍到HostYun商家新增的多款机房方案,价格相对还是比较便宜的。到目前为止,我们可以看到商家提供的VPS主机包括KVM和XEN架构,数据中心可选日本、韩国、香港和美国的多个地区机房,电信双程CN2 GIA线路,香港和日本机房,均为国内直连线路。近期,HostYun上线低价版美国CN2 GIA ...
PacificRack在本月发布了几款特价产品,其中最低款支持月付仅1.5美元,基于KVM架构,洛杉矶机房,PR-M系列。PacificRack简称PR,QN机房旗下站点,主要提供低价VPS主机产品,基于KVM架构,数据中心为自营洛杉矶机房,现在只有PR-M一个系列,分为了2个类别:常规(Elastic Compute Service)和多IP产品(Multi IP Server)。下面列出几款秒...
zji怎么样?zji最近新上韩国BGP+CN2线路服务器,国内三网访问速度优秀,适用8折优惠码zji,优惠后韩国服务器最低每月440元起。zji主机支持安装Linux或者Windows操作系统,会员中心集成电源管理功能,8折优惠码为终身折扣,续费同价,全场适用。ZJI是原Wordpress圈知名主机商:维翔主机,成立于2011年,2018年9月启用新域名ZJI,提供中国香港、台湾、日本、美国独立服...