Click here to Skip to main content
15,899,624 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
Questiongeting data from excel Pin
ajayjerone8-Oct-08 2:43
ajayjerone8-Oct-08 2:43 
AnswerRe: geting data from excel Pin
Giorgi Dalakishvili8-Oct-08 3:14
mentorGiorgi Dalakishvili8-Oct-08 3:14 
Questionquicksort? [modified] Pin
Marc Clifton8-Oct-08 2:32
mvaMarc Clifton8-Oct-08 2:32 
AnswerRe: quicksort? Pin
led mike8-Oct-08 10:32
led mike8-Oct-08 10:32 
Question[Message Deleted] Pin
followait7-Oct-08 15:04
followait7-Oct-08 15:04 
AnswerRe: Why not compile a .NET application one time for one machine? Pin
Mark Churchill7-Oct-08 19:28
Mark Churchill7-Oct-08 19:28 
AnswerRe: Why not compile a .NET application one time for one machine? Pin
Brij7-Oct-08 20:44
mentorBrij7-Oct-08 20:44 
QuestionGarbage-collect instances kept alive by event handlers Pin
GP7-Oct-08 12:18
GP7-Oct-08 12:18 
Hello,

I'd appreciate some suggestions how to solve the problem exposed below.
I apologize in case that's too easy and I cannot see the solution myself.

Thank you in advance.


<code>
using System;

namespace KeepAliveEventsExample
{
/// <summary>
/// How to garbage-collect an instance which is kept alive only by
/// event handlers without removing the handlers explicitly?
///
/// Client runs a procedure. It holds a reference to an
/// object able to raise an event. Inside the method (Run)
/// an instance is created which can subsrcibe to the event.
/// The event handler keeps the subscriber alive even if the subscriber
/// reference is not reachable by the client.
/// In order to allow the garbage collection an "Unregister()" method removes
/// explicitly the event handler.
/// We wish a solution in order to get to the same result without calling any explicit method
/// like Unregister()
/// </summary>
/// <remarks>
/// - No Dispose Pattern
/// - No new Threads
/// </remarks>
class Program
{
static void Main(string[] args)
{
Client client = new Client();

client.Run();

Console.WriteLine("RUN FINISHED. PRESS A KEY");

Console.ReadKey();

client = null;

Client.FullCollect("LAST COLLECTION");

Console.WriteLine("FINISHED");

Console.ReadKey();

}
}

/// <summary>
/// A client of publisher / subscriber
/// It keeps a reference to the publisher but not to the subscriber
/// </summary>
/// <remarks>
/// While a reference to the publisher is held, no reference
/// to the subscriber is reachable outside the method "Run()"
/// </remarks>
class Client
{
/// <summary>
/// The client holds a reference to the event publisher
/// </summary>
private ObjectWithEvents obj;

public Client()
{
obj = new ObjectWithEvents();
}

public void Run()
{
Observer _subscriber = new Observer(obj);

//we should see _subscriber reacting
obj.DoSomething(5);
obj.DoSomething(10);

//we would like to eliminate this method
_subscriber.Unregister();

//the subscriber is not reacheable [only through the event in obj]
_subscriber = null;

//here we would like _Subscriber NOT TO REACT without having to do anything
obj.DoSomething(15);

//We would like to be able to collect _Subscriber here
FullCollect("COLLECTION 1");

}

~Client()
{
Console.WriteLine("FINALIZING CLIENT");
}

/// <summary>
/// Try to trigger possible collections in all generations.
/// </summary>
/// <param name="message"></param>
public static void FullCollect(string message)
{
for (int j = 0; j <= GC.MaxGeneration; j++)
{
for (int i = 0; i <= GC.MaxGeneration; i++)
{
GC.Collect(i, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
}
}
Console.WriteLine(message);
}
}
/// <summary>
/// Subscribes to the event of ObjectWithEvents
/// </summary>
/// <remarks>
/// Instances of this object might be kept for the whole application lifetime
/// or just used for performing some operations.
///
/// Implementing the Dispose Pattern is not ok because in the real
/// application this would have a deeper meaning for the user
/// </remarks>
class Observer
{
/// <summary>
/// We can hold a reference to the publisher. This won't generate a problem
/// Also shows that, if we want, we have the publishers available
/// </summary>
ObjectWithEvents _publisher;

public Observer(ObjectWithEvents eventPublisher)
{
_publisher = eventPublisher;

//here we register to the event. This instance is going to
//survive garbage collection because the publisher keeps it alive
//through this handler
_publisher.ValueChanged += new EventDefinition(handler);

}

/// <summary>
/// Unregister the event handler in order to allow GC
/// We would like clients not to have to call this method
/// </summary>
public void Unregister()
{
_publisher.ValueChanged -= new EventDefinition(handler);
}

void handler(object sender, int value)
{
Console.WriteLine("VALUE CHANGED TO: {0}", value);
}

~Observer()
{
Console.WriteLine("FINALIZING SUBSCRIBER");
}
}

/// <summary>
/// Event definition
/// </summary>
/// <param name="sender"></param>
/// <param name="value"></param>
delegate void EventDefinition(object sender, int value);

/// <summary>
/// An object able to raise an event
/// </summary>
/// <remarks>
/// Instances are unaware of subscribers
/// </remarks>
class ObjectWithEvents
{
int _value;

public event EventDefinition ValueChanged;

public void DoSomething(int value)
{
_value = value;

//raise the event.
if (ValueChanged != null)
ValueChanged(this, _value);
}

~ObjectWithEvents()
{
Console.WriteLine("FINALIZING PUBLISHER");
}
}
}

</code>
AnswerRe: Garbage-collect instances kept alive by event handlers Pin
Mark Churchill7-Oct-08 19:32
Mark Churchill7-Oct-08 19:32 
GeneralRe: Garbage-collect instances kept alive by event handlers Pin
GP7-Oct-08 20:58
GP7-Oct-08 20:58 
AnswerRe: Garbage-collect instances kept alive by event handlers Pin
Giorgi Dalakishvili7-Oct-08 20:19
mentorGiorgi Dalakishvili7-Oct-08 20:19 
GeneralRe: Garbage-collect instances kept alive by event handlers Pin
GP7-Oct-08 23:45
GP7-Oct-08 23:45 
GeneralRe: Garbage-collect instances kept alive by event handlers Pin
Giorgi Dalakishvili7-Oct-08 23:55
mentorGiorgi Dalakishvili7-Oct-08 23:55 
QuestionCrystal Report Number Field Formatting Pin
salaikumar6-Oct-08 10:57
salaikumar6-Oct-08 10:57 
AnswerRe: Crystal Report Number Field Formatting Pin
Wendelius6-Oct-08 11:31
mentorWendelius6-Oct-08 11:31 
GeneralRe: Crystal Report Number Field Formatting Pin
salaikumar7-Oct-08 6:56
salaikumar7-Oct-08 6:56 
GeneralRe: Crystal Report Number Field Formatting Pin
Wendelius7-Oct-08 8:03
mentorWendelius7-Oct-08 8:03 
QuestionBest way to read/write DBF files in .Net ? Pin
Jaime Olivares3-Oct-08 20:02
Jaime Olivares3-Oct-08 20:02 
AnswerRe: Best way to read/write DBF files in .Net ? Pin
Thomas Stockwell5-Oct-08 6:06
professionalThomas Stockwell5-Oct-08 6:06 
GeneralRe: Best way to read/write DBF files in .Net ? Pin
Jaime Olivares5-Oct-08 7:56
Jaime Olivares5-Oct-08 7:56 
QuestionValue was too large or small for a decimal Pin
salaikumar2-Oct-08 20:45
salaikumar2-Oct-08 20:45 
AnswerRe: Value was too large or small for a decimal Pin
led mike3-Oct-08 5:23
led mike3-Oct-08 5:23 
GeneralRe: Value was too large or small for a decimal Pin
salaikumar3-Oct-08 11:33
salaikumar3-Oct-08 11:33 
AnswerRe: Value was too large or small for a decimal Pin
Dave Kreskowiak3-Oct-08 12:30
mveDave Kreskowiak3-Oct-08 12:30 
GeneralRe: Value was too large or small for a decimal Pin
salaikumar3-Oct-08 22:05
salaikumar3-Oct-08 22:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.