views:

2281

answers:

4

Say you want to share some resource, like a class or a variable across all threads/sessions within a ASP.NET web application. What is better?

1) A static variable having thread-safe accessors to that static variable?

2) Or a ASP.NET application session variable?

+4  A: 

If you only have one of those, there is little difference.

If you have several, you should use static variables rather than Application variables. The Application.Lock method will lock all Application variables, while you can use separate syncronisition identifiers for your static variables, so that each lock only affects the code that accesses that specific variable.

Guffa
A: 

This is a common scenario in which you are going through multiple pages and collecting data. I would use a Session object for this scenario. Static variables should be used when the complete application is in need for the same object.

If the value you want to hold is dependent on the user then use Session.

azamsharp
A: 

That's true session variables should only be used for if you want to store the value for the whole session, but in the case you want the variables to be intitialized and to be used in between the forms and if changed in betwwen should be available through the whole application for the same object must use static variables.

A: 

Can I use Application Variable for Chat System and how can use , i have used below code but if i open from two browser then after clicking send button its not display on another browser pls help me

Public Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    Application.Lock()
    'Dim nickname As String = txtnickname.Text

    Dim chatmessage As String = TextBox1.Text

    Dim mymessage As String = "Uttam" & "::" & chatmessage

    'Appending the User Entered Data To Application["msg"] object

    Application("msg") = Application("msg") + mymessage + Environment.NewLine

    'Finally Dispaling on the Chat Content

    Me.TextBox2.Text = Application("msg").ToString()
    Me.TextBox1.Text = ""

    Application.UnLock()
End Sub

thanks in Advance

uttam
Downvote for new question and general incomprehensibility. Don't ask a new question as an answer to someone else's question - search the site (and google), and if you're still lost, please use the "Ask Question" at the upper-right of the page. Also, to get better responses, you'll want to cut out irrelevant or commented-out code from your samples. Finally, use the "preview" option to make sure your code/question is formatted the way you want.
mikemanne