静态构造函数和多线程 [英] static constructor and multithreading

查看:74
本文介绍了静态构造函数和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在分析与Visual Studio .NET 2003绑定的Duwamish7源代码。

可以解释为什么Monitor.Enter和Monitor。在静态构造函数中使用退出块

?代码可以在模块ApplicationLog.cs中的Project

SystemFramework中找到。以下是样本。


命名空间Duwamish7.SystemFramework {


(部分代码......)


公共类ApplicationLog {

(某些代码..)

static ApplicationLog(){

类型myType = typeof(ApplicationLog);

尝试{

if(!Monitor.TryEnter(myType)){

Monitor.Enter(myType);

返回;

}

(部分代码..)

}

终于{

Monitor.Exit(myType);

}


} //静态构造函数


} //类ApplicationLog


(部分代码......)


} //命名空间Duwamish7.SystemFramework


Mark

解决方案

Marek,


代码中可能还有其他区域那些使用

类型的ApplicationLog作为锁定一段代码的对象。

获取类的类型不会导致s tatic构造函数代码解雇

(使用typeof)。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Marek" <毫安************** @ sbcglobal.net>在留言中写道

新闻:XU ******************** @ newssvr28.news.prodigy .com ...



我正在分析与Visual Studio .NET
2003绑定的Duwamish7源代码。可以解释为什么Monitor.Enter和Monitor.Exit块是
使用的在静态构造函数内?代码可以在模块ApplicationLog.cs中的Project
SystemFramework中找到。以下是样本。

命名空间Duwamish7.SystemFramework {

(一些代码......)

公共类ApplicationLog {
(一些代码..)
静态ApplicationLog(){
输入myType = typeof(ApplicationLog);
尝试{
if(!Monitor.TryEnter(myType)){
Monitor.Enter(myType);
返回;
}
(有些代码..)
}
最后{
Monitor.Exit(myType) ;
}

} //静态构造函数

} //类ApplicationLog

(部分代码..)

} //名称空间Duwamish7.SystemFramework

Mark



>代码的其他区域可能正在使用

类型的ApplicationLog作为锁定一段代码的对象。
获取类的类型不会导致静态构造函数代码触发
(使用typeof)。



这是否意味着可以多次调用静态ctor ?


-

cody


免费工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk

cody,


不,它没有。但是,仅仅因为代码锁定了特定的

类型,并不意味着应用程序其他部分的代码无法在同一类型上锁定




-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" cody" <无**************** @ gmx.net>在消息中写道

news:uR ************** @ tk2msftngp13.phx.gbl ...

代码的其他区域可能正在使用


类型的ApplicationLog作为锁定一段代码的对象。<获取类的类型不会导致静态构造函数代码
fire(使用typeof)。



这是否意味着可以调用静态ctor不止一次?

-


免费工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk


Hi,

I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003.
Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used
inside a static constructor? The code can be found in Project
SystemFramework within module ApplicationLog.cs. Here comes the sample.

namespace Duwamish7.SystemFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog() {
Type myType = typeof(ApplicationLog);
try {
if (!Monitor.TryEnter(myType)) {
Monitor.Enter(myType);
return;
}
(some code..)
}
finally {
Monitor.Exit(myType);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.SystemFramework

Mark

解决方案

Marek,

It is possible that there are other areas of the code that are using the
type of ApplicationLog as the object on which to lock a section of code.
Getting the Type of a class doesn''t cause static constructor code to fire
(using typeof).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Marek" <ma**************@sbcglobal.net> wrote in message
news:XU********************@newssvr28.news.prodigy .com...

Hi,

I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code can be found in Project
SystemFramework within module ApplicationLog.cs. Here comes the sample.

namespace Duwamish7.SystemFramework {

(some code...)

public class ApplicationLog {
(some code..)
static ApplicationLog() {
Type myType = typeof(ApplicationLog);
try {
if (!Monitor.TryEnter(myType)) {
Monitor.Enter(myType);
return;
}
(some code..)
}
finally {
Monitor.Exit(myType);
}

} // static constructor

} // class ApplicationLog

(some code..)

} //namespace Duwamish7.SystemFramework

Mark



> It is possible that there are other areas of the code that are using
the

type of ApplicationLog as the object on which to lock a section of code.
Getting the Type of a class doesn''t cause static constructor code to fire
(using typeof).


Does that mean that a static ctor could be called more than once?

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk


cody,

No, it does not. However, just because code locks on that particular
type, doesn''t mean that code in other sections of the application can''t lock
on that same type.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"cody" <no****************@gmx.net> wrote in message
news:uR**************@tk2msftngp13.phx.gbl...

It is possible that there are other areas of the code that are using


the

type of ApplicationLog as the object on which to lock a section of code.
Getting the Type of a class doesn''t cause static constructor code to fire (using typeof).


Does that mean that a static ctor could be called more than once?

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk



这篇关于静态构造函数和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆