2014年5月26日 10:26:31 晴
1
2
3
4
5
|
protected
void
Application_Start() {
RegisterRoutes(RouteTable.Routes);
_starter =
new
Starter<IOrchardHost>(HostInitialization, HostBeginRequest, HostEndRequest);
_starter.OnApplicationStart(
this
);
}
|
1
|
public
Starter(Func<HttpApplication, T> initialization, Action<HttpApplication, T> beginRequest, Action<HttpApplication, T> endRequest) {}
|
1
2
3
|
OnApplicationStart-->
//Run the initialization delegate asynchronously in a queued work item
public
void
LaunchStartupThread(HttpApplication application)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/// <summary>
/// Run the initialization delegate asynchronously in a queued work item
/// </summary>
public
void
LaunchStartupThread(HttpApplication application) {
// Make sure incoming requests are queued
WarmupHttpModule.SignalWarmupStart();
ThreadPool.QueueUserWorkItem(
state => {
try
{
var
result = _initialization(application);
_initializationResult = result;
}
catch
(Exception e) {
lock
(_synLock) {
_error = e;
_previousError =
null
;
}
}
finally
{
// Execute pending requests as the initialization is over
WarmupHttpModule.SignalWarmupDone();
}
});
}
|
总之,Application_Start函数主要作用就是对所有可用扩展的异步队列初始化,其中异步与队列的功能由Orchard.WarmupStarter.Starter类来完成,而可用扩展的初始化加载由IOrchardHost来完成,其中扩展包括:module,core,theme以及子站点的概念。
线程池在首次创建 ThreadPool 类的实例时被创建。线程池具有每个可用处理器 25 个线程的默认限制,这可以使用 mscoree.h 文件中定义的 CorSetMaxThreads 来更改。每个线程使用默认的堆栈大小并按照默认的优先级运行。每个进程只能具有一个操作系统线程池。
ThreadPool提供的公共方法都是static方法,因此也不需要生成ThreadPool对象。通过QueueUserWorkItem方法在线程池中添加一个工作项目后,目前没有提供简单的方法取消。你不必建立咨监线程,只需要把相应的函数或方法依托WaitCallback委托传递给ThreadPool.QueueUserWorkItem()方法即可。而线程的创建、管理和运行等等都由系统自动完成,这就是ThreadPool的优点。
*eg: the different between threadpool with normal thread *
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
delegate
void
ShowProgressDelegate (
int
totalMessages,
int
messagesSoFar,
bool
statusDone );
private
void
button1_Click(
object
sender, System.EventArgs e)
{
ShowProgressDelegate showProgress =
new
ShowProgressDelegate(ShowProgress);
int
imsgs = 100;
if
( cbThreadPool.Checked )
{
object
obj =
new
object
[] {
this
, showProgress, imsgs };
WorkerClass wc =
new
WorkerClass();
bool
rc = ThreadPool.QueueUserWorkItem(
new
WaitCallback
(wc.RunProcess), obj);
EnableButton( ! rc );
}
else
{
//another way.. using straight threads
//WorkerClass wc = new WorkerClass( this, showProgress, imsgs);
WorkerClass wc =
new
WorkerClass(
this
,
showProgress,
new
object
[] { imsgs } );
Thread t =
new
Thread(
new
ThreadStart(wc.RunProcess));
//make them a daemon - prevent thread callback issues
t.IsBackground =
true
;
t.Start();
EnableButton (
false
);
}
}
|
可变长数组参数
params 构造函数声明数组 而不知道数组长度 用的 在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
1public
WorkerClass ( ContainerControl sender, Delegate senderDelegate, **
params
object
[] list**);