Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Question on events and local variables

Question on events and local variables

Scheduled Pinned Locked Moved C#
questionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Flack
    wrote on last edited by
    #1

    Hello, In a button click method I create an EventLog, set its EnableRaisingEvents property to true and attach an event handler for the EntryWritten event. EventLog eventLog = new EventLog(cbEventLogs.Text,txtMachineName.Text); if(this.txtMachineName.Text == SystemInformation.ComputerName) { eventLog.EnableRaisingEvents = true; eventLog.EntryWritten += new EntryWrittenEventHandler(eventLog_EntryWritten); } My question is, how does the event still get fired when you exit the button click method where the event log was created? Doesn't the EventLog variable fall out of scope when the method exits, causing it to be garbage collected? Thanx for the help, -Flack

    K 1 Reply Last reply
    0
    • F Flack

      Hello, In a button click method I create an EventLog, set its EnableRaisingEvents property to true and attach an event handler for the EntryWritten event. EventLog eventLog = new EventLog(cbEventLogs.Text,txtMachineName.Text); if(this.txtMachineName.Text == SystemInformation.ComputerName) { eventLog.EnableRaisingEvents = true; eventLog.EntryWritten += new EntryWrittenEventHandler(eventLog_EntryWritten); } My question is, how does the event still get fired when you exit the button click method where the event log was created? Doesn't the EventLog variable fall out of scope when the method exits, causing it to be garbage collected? Thanx for the help, -Flack

      K Offline
      K Offline
      Karl 2000
      wrote on last edited by
      #2

      The garbage collector will only collect when there reference count goes down to 0. By subscribing eventLog_EntryWritten to the EntryWritten event you are creating a reference. That reference is enough to keep the eventLog alive. Even though you no longer hold a reference one does exist. To get the garbage collector to collect the EventLog unsubscribe the event before eventLog goes out of scope and you no longer have control of any of the references to it. Karl Baum CEO of KGB Technologies Specializing in custom software development.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups