静态方法中的线程锁定 - 如何? [英] Thread Locking In Static Methods - How?

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

问题描述

静态方法中的线程锁定


我需要一个具有静态方法的Log Manger类。通常我将使用lock语句或Monitor语句来使用

引用(this)。在这些静态方法的情况下,我无法做到这一点。


我应该如何在这样的静态方法中执行线程锁定?谢谢

你。

Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods. Normally I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able to
do that.

How should I perform thread locking within static methods like this? Thank
you.

推荐答案

public static void LogText(string text)

{< br $>
lock(typeof(ThisClassType))

{

...

}

}


ShaneB


" McFly Racing" <否**** @ Microsoft.com>在消息中写道

news:ed ************** @ TK2MSFTNGP10.phx.gbl ...
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
静态方法中的线程锁定

我需要一个具有静态方法的Log Manger类。通常
我将使用lock语句或Monitor语句,这两个语句都引用(this)。在这些静态方法的情况下,我无法做到这一点。

我应该如何在这样的静态方法中执行线程锁定?
感谢
你。
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods. Normally
I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able to
do that.

How should I perform thread locking within static methods like this?
Thank
you.



如果我这样做的话似乎我会把整个班级搞砸了

of just just这个方法,是正确的吗?谢谢。


" ShaneB" < ST ******** @ yahoo.com>在消息中写道

news:OR ************** @ TK2MSFTNGP09.phx.gbl ...
If I do it this way it seems like I will be loking the whole class instead
of just this method, is that correct? Thank you.

"ShaneB" <st********@yahoo.com> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
public static void LogText(字符串文本)
{
lock(typeof(ThisClassType))
{
...
}
}

ShaneB

McFly Racing <否**** @ Microsoft.com>在消息中写道
新闻:ed ************** @ TK2MSFTNGP10.phx.gbl ...
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
静态方法中的线程锁定

我需要一个具有静态方法的Log Manger类。
通常我会使用lock语句或Monitor语句,这两个语句都引用(this)。在这些静态方法的情况下,我无法
这样做。

我应该如何在这样的静态方法中执行线程锁定?
感谢
你。
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods. Normally I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able to do that.

How should I perform thread locking within static methods like this?
Thank
you.




没有。这样做会导致锁定使用

相同锁定义的任何代码位置。如果类中的每个方法都有其代码

包含在同一语句中,那么无论何时输入其中一个方法,它都会导致整个类锁定

,但仅限于此是这样的。


如果你想确保只有一种方法被锁定,你可以

为特定方法创建一个私有静态成员变量,然后

锁定。例如:


私有静态字符串_logTextLock ="这用于锁定。" ;;

public static void LogText(string text)

{

lock(_logTextLock)

{

...

}

}


希望有所帮助。


有更好的一个!


John M Deal,MCP

必要软件


McFly Racing写道:
No. What this does is cause a lock of any code location that uses the
same lock definition. If every method in the class had its code
encompassed by the same statement it would cause the whole class to lock
anytime one of the methods was entered, but only if that was the case.

If you want to make sure that only the one method is locked you could
create a private static member variable for the specific method, then
lock against that. For example:

private static string _logTextLock = "This is used to lock.";
public static void LogText(string text)
{
lock (_logTextLock)
{
...
}
}

Hope that helps.

Have A Better One!

John M Deal, MCP
Necessity Software

McFly Racing wrote:
如果我这样做,似乎我将是整个班级而不是这个方法,这是正确的吗?谢谢。

ShaneB < ST ******** @ yahoo.com>在消息中写道
新闻:或************** @ TK2MSFTNGP09.phx.gbl ...
If I do it this way it seems like I will be loking the whole class instead
of just this method, is that correct? Thank you.

"ShaneB" <st********@yahoo.com> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
public static void LogText(字符串文本)
{
lock(typeof(ThisClassType))
{
...
}
}

ShaneB

McFly Racing <否**** @ Microsoft.com>在消息中写道
新闻:ed ************** @ TK2MSFTNGP10.phx.gbl ...
public static void LogText(string text)
{
lock (typeof(ThisClassType))
{
...
}
}

ShaneB

"McFly Racing" <No****@Microsoft.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
静态方法中的线程锁定

我需要一个具有静态方法的Log Manger类。
通常
我会使用lock语句或Monitor语句来获取
参考(这)。在这些静态方法的情况下,我无法做到这一点。

我应该如何在这样的静态方法中执行线程锁定?
感谢
你。
Thread Locking In Static Methods

I have the need for a Log Manger class that has static methods.
Normally
I
would use the lock statement or a Monitor statement both of which take a
reference to (this). In the case of these static methods I am not able
to
do that.

How should I perform thread locking within static methods like this?
Thank
you.





这篇关于静态方法中的线程锁定 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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