473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Static instance Event fires more than one time

Hi,

I'm facing a problem with static instances. I have created a
class called CustomList by deriving the class List<int>. Inside the
CustomList i have created Remove event (when any item is removed from
the List). After that i have created a staic instance for the
CustomList in my WebService class. The class contains the Start And
Poll Methods. Inside the Start method i have added the Removed event.
Please execute the following code and you will notice that the
"objCustList_Removed" is called more than one time. I'm in a situation
that the "objCustList_Removed" function should not called more than
one time. Please let me know if anybody find solution.

Note : Also i'm in a situation that i want to use only static
instance.
using System;
using System.Collections.Generic;
using System.Text;

namespace TestConsole
{
class Main
{
public delegate void OnRemovedEventHandler(object sender,
System.EventArgs e);
public class CustomList : List<int>
{

public event OnRemovedEventHandler Removed;

protected virtual void OnRemoved(System.EventArgs e)
{
if (Removed != null)
{
Removed(this, e);
}
}

public new void Add(int i)
{
base.Add(i);
}

public new void Remove(int i)
{
base.Remove(i);
Removed(this, System.EventArgs.Empty);
}

public new void RemoveAt(int i)
{
base.RemoveAt(i);
Removed(this, System.EventArgs.Empty);
}

}
public class WebService
{
private static CustomList objCustList = new CustomList();
public WebService()
{
}
public int start(int i)
{
objCustList.Removed += new
OnRemovedEventHandler(objCustList_Removed);
objCustList.Add(i);
return i;
}
void objCustList_Removed(object sender, EventArgs e)
{
Console.WriteLine("Method Called More than one time");
}
public void poll(int i)
{
objCustList.Remove(i);

}

}
internal class Program
{
public static void Main()
{
WebService ws = new WebService();
int i1 = ws.start(1);

WebService ws1 = new WebService();
int i2 = ws1.start(2);

WebService ws2 = new WebService();
int i3 = ws2.start(3);

WebService ws3 = new WebService();
int i4 = ws3.start(4);

WebService ws4 = new WebService();
int i5 = ws4.start(5);
ws1.poll(i1);
Console.WriteLine("---------------------------");
ws2.poll(i2);
Console.WriteLine("---------------------------");
ws3.poll(i3);
Console.WriteLine("---------------------------");
ws4.poll(i4);
Console.WriteLine("---------------------------");
ws4.poll(i5);
Console.WriteLine("---------------------------");
Console.ReadLine();
}
}
}
}
Thanks,
Vinothkumar B

May 24 '07 #1
9 2087
"None" <vi********@gmail.comschrieb im Newsbeitrag
news:11**********************@z28g2000prd.googlegr oups.com...
Hi,

I'm facing a problem with static instances. I have created a
class called CustomList by deriving the class List<int>. Inside the
CustomList i have created Remove event (when any item is removed from
the List). After that i have created a staic instance for the
CustomList in my WebService class. The class contains the Start And
Poll Methods. Inside the Start method i have added the Removed event.
You call the start method 5 times. Each time you add a handler to the event.
After that, on any Remove all this handlers are called.

All instances of WebService use the same CustomList and all listen on the
Remove event. So each WebService react on each occurence of Remove

Wich WebService do you want to react?

Christof
May 24 '07 #2
Note related to the issue at hand, but a general pointer; it might be
desirable to expose this functionality via the standard
IBindingList.ListChanged event; fortunately, BindingList<Tdoes all
of this for you... it might save you some debugging time? Plus make
your list fully usable from MVP...

Marc
May 24 '07 #3
Hi Christof,

Thanks for your reply. I understand what is happening. I
want all the webservice has to reacat to the Remove event but only one
time (i.e, i want the "objCustList_Removed" has to be called only one
time during the Remove), eventhough many handlers are added. Is there
any other possibilites to achive this. (Without changing the static
instance)

Thanks and Regards,
Vinothkumar B
On May 24, 12:50 pm, "Christof Nordiek" <c...@nospam.dewrote:
"None" <vinkumr...@gmail.comschrieb im Newsbeitragnews:11**********************@z28g2000p rd.googlegroups.com...
Hi,
I'm facing a problem with static instances. I have created a
class called CustomList by deriving the class List<int>. Inside the
CustomList i have created Remove event (when any item is removed from
the List). After that i have created a staic instance for the
CustomList in my WebService class. The class contains the Start And
Poll Methods. Inside the Start method i have added the Removed event.

You call the start method 5 times. Each time you add a handler to the event.
After that, on any Remove all this handlers are called.

All instances of WebService use the same CustomList and all listen on the
Remove event. So each WebService react on each occurence of Remove

Wich WebService do you want to react?

Christof

May 24 '07 #4
I'm not sure I fully understand what you are trying to do... if you
don't want to listen to an event any more, then unsubscribe from it.
If each item is interested in a single item from the list then use
instance events and have your Remove code fire removed instance's
event via a public/internal method such as OnRemoved() on the item.

A bigger word of warning: subscribing to static events is a really
easy way to cause a leak[*] if you aren't very careful to
unsubscribe...

*=not a true leak; the lost memory isn't in a disconnected island - it
is still visible to the static instance. But either way - you can
easily start chomping up the mibibytes...

Marc
May 24 '07 #5
On a side note, it's interesting that the term "leak" is used for this
kind of situation. Given that it is a managed environment, you can't have
disconnected islands. If anything, you have "stuff" or "globs" of memory
hanging around which you need to disconnect (to make it eligible for GC), as
opposed to disconnected memory which you need to release.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marc Gravell" <ma**********@gmail.comwrote in message
news:OU**************@TK2MSFTNGP03.phx.gbl...
I'm not sure I fully understand what you are trying to do... if you don't
want to listen to an event any more, then unsubscribe from it. If each
item is interested in a single item from the list then use instance events
and have your Remove code fire removed instance's event via a
public/internal method such as OnRemoved() on the item.

A bigger word of warning: subscribing to static events is a really easy
way to cause a leak[*] if you aren't very careful to unsubscribe...

*=not a true leak; the lost memory isn't in a disconnected island - it is
still visible to the static instance. But either way - you can easily
start chomping up the mibibytes...

Marc

May 24 '07 #6
If you want an event handler to fire once for a particular event, then
you need to subscribe to the event only once. If you keep adding the event
handler, you are going to have that handler called multiple times for the
same event.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"None" <vi********@gmail.comwrote in message
news:11**********************@n15g2000prd.googlegr oups.com...
Hi Christof,

Thanks for your reply. I understand what is happening. I
want all the webservice has to reacat to the Remove event but only one
time (i.e, i want the "objCustList_Removed" has to be called only one
time during the Remove), eventhough many handlers are added. Is there
any other possibilites to achive this. (Without changing the static
instance)

Thanks and Regards,
Vinothkumar B
On May 24, 12:50 pm, "Christof Nordiek" <c...@nospam.dewrote:
>"None" <vinkumr...@gmail.comschrieb im
Newsbeitragnews:11**********************@z28g2000 prd.googlegroups.com...
Hi,
I'm facing a problem with static instances. I have created a
class called CustomList by deriving the class List<int>. Inside the
CustomList i have created Remove event (when any item is removed from
the List). After that i have created a staic instance for the
CustomList in my WebService class. The class contains the Start And
Poll Methods. Inside the Start method i have added the Removed event.

You call the start method 5 times. Each time you add a handler to the
event.
After that, on any Remove all this handlers are called.

All instances of WebService use the same CustomList and all listen on the
Remove event. So each WebService react on each occurence of Remove

Wich WebService do you want to react?

Christof


May 24 '07 #7
I had a long and fruitless disucssion about this some time ago.

I would claim that there are two kinds of memory leaks: true memory
leaks, in which the memory becomes a disconnected island and cannot be
recovered, and what I would call "conceptual memory leaks" in which
memory that you expect to be recovered isn't recovered.

The first kind of leak is theoretically impossible under .NET. The
second kind of leak happens all the time, and points out a
misunderstanding about how the C# language, or the .NET Framework,
operates.

On May 24, 11:39 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
On a side note, it's interesting that the term "leak" is used for this
kind of situation. Given that it is a managed environment, you can't have
disconnected islands. If anything, you have "stuff" or "globs" of memory
hanging around which you need to disconnect (to make it eligible for GC), as
opposed to disconnected memory which you need to release.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Marc Gravell" <marc.grav...@gmail.comwrote in message

news:OU**************@TK2MSFTNGP03.phx.gbl...
I'm not sure I fully understand what you are trying to do... if you don't
want to listen to an event any more, then unsubscribe from it. If each
item is interested in a single item from the list then use instance events
and have your Remove code fire removed instance's event via a
public/internal method such as OnRemoved() on the item.
A bigger word of warning: subscribing to static events is a really easy
way to cause a leak[*] if you aren't very careful to unsubscribe...
*=not a true leak; the lost memory isn't in a disconnected island - it is
still visible to the static instance. But either way - you can easily
start chomping up the mibibytes...
Marc- Hide quoted text -

- Show quoted text -

May 24 '07 #8
Any suggestions on a catch-phrase we could coin? haha

Personally, I like "gunk" or "glob".

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bruce Wood" <br*******@canada.comwrote in message
news:11*********************@a35g2000prd.googlegro ups.com...
>I had a long and fruitless disucssion about this some time ago.

I would claim that there are two kinds of memory leaks: true memory
leaks, in which the memory becomes a disconnected island and cannot be
recovered, and what I would call "conceptual memory leaks" in which
memory that you expect to be recovered isn't recovered.

The first kind of leak is theoretically impossible under .NET. The
second kind of leak happens all the time, and points out a
misunderstanding about how the C# language, or the .NET Framework,
operates.

On May 24, 11:39 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> On a side note, it's interesting that the term "leak" is used for
this
kind of situation. Given that it is a managed environment, you can't
have
disconnected islands. If anything, you have "stuff" or "globs" of memory
hanging around which you need to disconnect (to make it eligible for GC),
as
opposed to disconnected memory which you need to release.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Marc Gravell" <marc.grav...@gmail.comwrote in message

news:OU**************@TK2MSFTNGP03.phx.gbl...
I'm not sure I fully understand what you are trying to do... if you
don't
want to listen to an event any more, then unsubscribe from it. If each
item is interested in a single item from the list then use instance
events
and have your Remove code fire removed instance's event via a
public/internal method such as OnRemoved() on the item.
A bigger word of warning: subscribing to static events is a really easy
way to cause a leak[*] if you aren't very careful to unsubscribe...
*=not a true leak; the lost memory isn't in a disconnected island - it
is
still visible to the static instance. But either way - you can easily
start chomping up the mibibytes...
Marc- Hide quoted text -

- Show quoted text -


May 24 '07 #9
How about a memory "abscess" - still attached and puddling, but
useless waste... or "slag" (presumably, then, the GC in CLR4.0 might
move these to the "slag heap"; I'll get my coat...)?

Neither is very pretty, though ;-p

Marc
May 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Colin Cashman | last post by:
In a program I'm writing, I have a class that generates a certain event. However, there may be dozens or even hundreds of instances of this particular class, which makes adding and removing...
3
by: Peter Row | last post by:
Hi, I have created a user control consisting of a textbox and a separate vertical scroll bar. The textbox is filled with data. However there could be lots of data so I only fill the textbox...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
0
by: Dave L | last post by:
I just upgraded from VS .NET 2002 to 2003. Everything built okay, but strange bugs started appearing. Apparently there is a bug in the managed C++ compiler in regards to handling of static...
19
by: Heidi Hundåla | last post by:
Hi ! I have a Wep App in C#. Page_Unload fires after Page_Load, and it seems totally unreasonable when you want to use this event when you _leave_ the page. In my project we wanted to use...
0
by: ChrisB | last post by:
I'm attempting to open a new window from a LinkButton in a DataGrid. I can set a session variable in the ItemCommand event for the LinkButton like so: // this is used to handle the ItemCommand...
6
by: Joseph Geretz | last post by:
I'm porting a C# Outlook Addin originally engineered as a COM Addin over to use VSTO. I've gotten this to the point where my VSTO Addin installs its Menu items and Toolbar buttons when Outlook...
7
by: FredC | last post by:
I need some insight into timers, the static modifier and instance memory safety. public class myClass { protected static int i = 0; portected float myfloat; protected static Timer staticTimer =...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.