全局变量寿命 [英] Global variable lifetime
问题描述
我对asp.net应用程序中的globa变量生命周期有疑问。
我已经宣布了这个类:
Public Class Utils
私有共享_FcdDataManagement为FCD.DataManagement
公共共享ReadOnly属性FcdDataManagement()作为FCD.DataManagement
获取
如果_FcdDataManagement什么都没有那么
_FcdDataManagement =新FCD.DataManagement
结束如果
返回_FcdDataManagement
结束获取
结束物业
结束课程
我认为变量在每个页面的第一次调用时可能没有任何意义
执行。但我已经看到变量不仅仅是第一次调用
的应用程序。然后,如果我重新加载页面(=新执行)变量
并非一无所获。为什么?我认为每个页面执行管道都与
其他人隔离(否则为什么应用程序状态?),但这种行为非常好...... / b $ b奇怪...
>
感谢
I''ve a question about globa variables lifetime in an asp.net app.
I''ve declared this class:
Public Class Utils
Private Shared _FcdDataManagement As FCD.DataManagement
Public Shared ReadOnly Property FcdDataManagement() As FCD.DataManagement
Get
If _FcdDataManagement Is Nothing Then
_FcdDataManagement = New FCD.DataManagement
End If
Return _FcdDataManagement
End Get
End Property
End Class
I think the variable may be nothing at the first call of each page
execution. But I''ve seen that the variable is nothing only for the first call
of the application. Then if I reload the page (=new execution) the variable
is not nothing. Why? I think each page execution pipeline is isolated from
others (otherwise why Application state?), but this behavior is quite
strange...
thanks
推荐答案
这是因为您已将变量''_FcdDataManagement''标记为已共享,
它在类的实例之间是''共享''只有一个变量的实例,因此当它第一次被分配时它仍然是
分配,除非你明确地将值设置为null。
HTH
Ollie Riches
" Trapulo" <无********** @ qui.it>在消息中写道
新闻:2B ********************************** @ microsof t.com ...
it is because you have marked the variable ''_FcdDataManagement'' as Shared,
it is ''shared'' across instances of the class there is only one instance of
the variable and therefore when it is allocated first time it remains
allocated unless you explicitly set the value to null.
HTH
Ollie Riches
"Trapulo" <no**********@qui.it> wrote in message
news:2B**********************************@microsof t.com...
我对asp.net应用程序中的globa变量生命周期有疑问。
我已经宣布了这个类:
Public Class Utils
私有共享_FcdDataManagement As FCD.DataManagement
公共共享ReadOnly属性FcdDataManagement()As
FCD.DataManagement获取
如果_FcdDataManagement什么都没有呢
_FcdDataManagement =新FCD.DataManagement
结束如果
返回_FcdDataManagement
结束获取
结束财产
我认为变量可能在每个页面的第一次调用时都没有执行。但我已经看到变量不仅仅是应用程序的第一个
调用。然后,如果我重新加载页面(=新执行),
变量不是什么。为什么?我认为每个页面执行管道都是孤立的
来自其他人(否则为什么应用程序状态?),但这种行为非常奇怪......
感谢
I''ve a question about globa variables lifetime in an asp.net app.
I''ve declared this class:
Public Class Utils
Private Shared _FcdDataManagement As FCD.DataManagement
Public Shared ReadOnly Property FcdDataManagement() As FCD.DataManagement Get
If _FcdDataManagement Is Nothing Then
_FcdDataManagement = New FCD.DataManagement
End If
Return _FcdDataManagement
End Get
End Property
End Class
I think the variable may be nothing at the first call of each page
execution. But I''ve seen that the variable is nothing only for the first call of the application. Then if I reload the page (=new execution) the variable is not nothing. Why? I think each page execution pipeline is isolated from others (otherwise why Application state?), but this behavior is quite
strange...
thanks
就是这样。共享(静态)变量在回发之间保持不变。
Eliyahu
" Trapulo" <无********** @ qui.it>在消息中写道
新闻:2B ********************************** @ microsof t.com ...
This is how it is. Shared (static) variables persist between postbacks.
Eliyahu
"Trapulo" <no**********@qui.it> wrote in message
news:2B**********************************@microsof t.com...
我对asp.net应用程序中的globa变量生命周期有疑问。
我已经宣布了这个类:
Public Class Utils
私有共享_FcdDataManagement As FCD.DataManagement
公共共享ReadOnly属性FcdDataManagement()As
FCD.DataManagement获取
如果_FcdDataManagement什么都没有呢
_FcdDataManagement =新FCD.DataManagement
结束如果
返回_FcdDataManagement
结束获取
结束财产
我认为变量可能在每个页面的第一次调用时都没有执行。但我已经看到变量不仅仅是应用程序的第一个
调用。然后,如果我重新加载页面(=新执行),
变量不是什么。为什么?我认为每个页面执行管道都是孤立的
来自其他人(否则为什么应用程序状态?),但这种行为非常奇怪......
感谢
I''ve a question about globa variables lifetime in an asp.net app.
I''ve declared this class:
Public Class Utils
Private Shared _FcdDataManagement As FCD.DataManagement
Public Shared ReadOnly Property FcdDataManagement() As FCD.DataManagement Get
If _FcdDataManagement Is Nothing Then
_FcdDataManagement = New FCD.DataManagement
End If
Return _FcdDataManagement
End Get
End Property
End Class
I think the variable may be nothing at the first call of each page
execution. But I''ve seen that the variable is nothing only for the first call of the application. Then if I reload the page (=new execution) the variable is not nothing. Why? I think each page execution pipeline is isolated from others (otherwise why Application state?), but this behavior is quite
strange...
thanks
" Ollie Riches"写道:
"Ollie Riches" wrote:
这是因为你已将变量''_FcdDataManagement''标记为共享,
它是''共享''跨类的实例只有一个变量的实例,因此当它第一次被分配时,除非你明确地将值设置为null,否则它仍然被分配。
HTH
it is because you have marked the variable ''_FcdDataManagement'' as Shared,
it is ''shared'' across instances of the class there is only one instance of
the variable and therefore when it is allocated first time it remains
allocated unless you explicitly set the value to null.
HTH
所以我也可以有多线程问题?
但是如果这是问题并且变量保留在内存中直到
asp.net应用程序是卸载,使用
page.Application-basd解决方案,并使用共享变量有什么区别?
获取我想要的东西(属性返回一个变量instanced,但
,单个页面执行的生命周期)正确的方法是在
a模块上声明它,而不是使用属性chech,这不是什么?
谢谢
So I can have also multithreading problems?
However if this is the problem and the variable is retained in memory until
asp.net application is unloaded, what is the difference from using a
page.Application-basd solutions, and using a shared variable?
To obtain what I want (a property that returns a variable instanced, but
with lifetime of a single page''s execution) the right way is to declare it on
a module, and than use the property to chech that it is not nothing?
thanks
这篇关于全局变量寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!