Results 1 to 2 of 2

Thread: Is this a BUG or am I missing summat?

  1. #1

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Exclamation Is this a BUG or am I missing summat?

    Hi all,

    The code below creates a class object that sources events. It then caches the object so on the next request it can retrieve it.

    Problem is the event it raises runs multiple times (first time round is fine). For every time its cached and retrieved its like it stores the event again.

    If you debug through, you can see that RaiseEvent is only called once, however the event WILL repeat several times. See the debugger window for output.

    I _think_ this is all you need. I've cut this down from a very large chunk of code.

    Paste all this into a webform called "Test1.aspx", you will need a PlaceHolder called phMain on the form.

    VB Code:
    1. Public Class TestClass
    2.  
    3.   Private m_Value As Long
    4.  
    5.   Event TestEvent()
    6.  
    7.   Sub New(ByVal Value As Long)
    8.     m_Value = Value
    9.   End Sub
    10.  
    11.   Public Function PubFunc() As Control
    12.     RaiseEvent TestEvent()
    13.     Return New LiteralControl(CStr(m_Value))
    14.   End Function
    15.  
    16. End Class
    17.  
    18. Public Class Test1
    19.   Inherits System.Web.UI.Page
    20.  
    21. #Region " Web Form Designer Generated Code "
    22.  
    23.   'This call is required by the Web Form Designer.
    24.   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    25.  
    26.   End Sub
    27.  
    28.   Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    29.     'CODEGEN: This method call is required by the Web Form Designer
    30.     'Do not modify it using the code editor.
    31.     InitializeComponent()
    32.   End Sub
    33.   Protected WithEvents phMain As System.Web.UI.WebControls.PlaceHolder
    34.  
    35. #End Region
    36.  
    37.   Protected WithEvents TestObj As TestClass
    38.  
    39.   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    40.  
    41.     'Retrieve from Cache
    42.     TestObj = CType(HttpContext.Current.Cache("TestObj" & HttpContext.Current.Session.SessionID), TestClass)
    43.     If TestObj Is Nothing Then
    44.       'Not in Cache so create the object
    45.       TestObj = New TestClass(123456)
    46.       'Save the Object in the Cache
    47.       HttpContext.Current.Cache.Insert("TestObj" & HttpContext.Current.Session.SessionID, TestObj)
    48.     End If
    49.  
    50.     'Call a function of the Obj to get HTML output
    51.     phMain.Controls.Add(TestObj.PubFunc)
    52.  
    53.   End Sub
    54.  
    55.   Private Sub TestObj_TestEvent() Handles TestObj.TestEvent
    56.     System.Diagnostics.Debug.Write("Event Running " & Now & vbCrLf)
    57.     Try
    58.       Response.Write("Event Running")
    59.     Catch
    60.       'Response only seems to be available
    61.       'for a single instance of the event?
    62.     Finally
    63.     End Try
    64.   End Sub
    65.  
    66. End Class
    Last edited by tailz; Jan 12th, 2005 at 02:23 PM.

  2. #2

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Re: Is this a BUG or am I missing summat?

    I've been working on this for HOURS, the minute I post I solved it.

    At the bottom of Page_Load I added........

    VB Code:
    1. TestObj = Nothing

    ......and it all went away. In vb6 I was religious with clearing up objects.... guess I still should be

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width