473,728 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MVC delegates problem...

In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?
Dec 8 '05 #1
8 2173
Dominque,

From what you have shown, it doesn't seem like you should be having that
problem. Is there any point where you remove the delegates and it is
possible that you are removing it?

Is this all of the code?

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

"Dominique" <Do*******@disc ussions.microso ft.com> wrote in message
news:B0******** *************** ***********@mic rosoft.com...
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded
at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of
view.
So in this case I have a Pie Chart View and Bar Chart view of the data (
as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 8 '05 #2
Hi Nicholas,
Thanks for your prompt reply.

RemoveView is only called once in the whole project group and that occurs in
the MVCView destructor as follows...

~MvcView()
{
if ( DataModel != null )
DataModel.Remov eView( this );
}

I planned to make this demo public once I ironed out this last bug and
commented the code for other newbies to hopefully learn by.
The other stuff it also shows...
0. How to create an MDI application
1. How to use GDI+ for Pie Charts and Graphs
2. Double Buffering to avoid flickering
3. Type Enumeration using Relection
4. How create an ArrayList descendent
5. How to use the ArrayList descendent in a DataGrid
6. It shows how you may want to implement a simple plugin system

Dominique

"Nicholas Paldino [.NET/C# MVP]" wrote:
Dominque,

From what you have shown, it doesn't seem like you should be having that
problem. Is there any point where you remove the delegates and it is
possible that you are removing it?

Is this all of the code?

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

"Dominique" <Do*******@disc ussions.microso ft.com> wrote in message
news:B0******** *************** ***********@mic rosoft.com...
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded
at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of
view.
So in this case I have a Pie Chart View and Bar Chart view of the data (
as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?


Dec 8 '05 #3
Dominque,

Don't do this. This is not a destructor. Rather, it is a finalizer,
and it is not called when the object goes out of scope. Rather, it is
called when the object is being reclaimed by a collection by the GC.

Also, I don't think the model you have is really a good one. Since you
are exposing the events, why not just have the views add the event handlers
themselves when they are constructed? You can pass the model to the views,
and they can decide how to hook up to it.

Your views should implement IDisposable (I imagine they do already), and
in that implementatio of Dispose, you can remove the event handler (so that
a reference to you is not kept through the delegate).

I don't know that this will solve your problem, but I think that there
is more to it. I haven't heard of any problems of delegates just being
dropped from event invocation lists...
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Dominique" <Do*******@disc ussions.microso ft.com> wrote in message
news:B5******** *************** ***********@mic rosoft.com...
Hi Nicholas,
Thanks for your prompt reply.

RemoveView is only called once in the whole project group and that occurs
in
the MVCView destructor as follows...

~MvcView()
{
if ( DataModel != null )
DataModel.Remov eView( this );
}

I planned to make this demo public once I ironed out this last bug and
commented the code for other newbies to hopefully learn by.
The other stuff it also shows...
0. How to create an MDI application
1. How to use GDI+ for Pie Charts and Graphs
2. Double Buffering to avoid flickering
3. Type Enumeration using Relection
4. How create an ArrayList descendent
5. How to use the ArrayList descendent in a DataGrid
6. It shows how you may want to implement a simple plugin system

Dominique

"Nicholas Paldino [.NET/C# MVP]" wrote:
Dominque,

From what you have shown, it doesn't seem like you should be having
that
problem. Is there any point where you remove the delegates and it is
possible that you are removing it?

Is this all of the code?

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

"Dominique" <Do*******@disc ussions.microso ft.com> wrote in message
news:B0******** *************** ***********@mic rosoft.com...
> In order to become more familiar with the Model-View-Controller
> pattern, I
> have written a demo where each View is a plugin and the plugins are
> loaded
> at
> startup from the plugins directory. When the View's are loaded they add
> themselves to the DataModel View list...
>
> FDataModel.AddV iew(this);
>
> Where AddView looks like this...
> public void AddView( MvcView view )
> {
> ViewChanged += new ViewChangedHand ler(view.DataMo dified);
> }
>
> and at the right time I invoke the event list via...
> ViewChanged(thi s, e);
>
> This all works fine as long as I only have 1 instance of each type of
> view.
> So in this case I have a Pie Chart View and Bar Chart view of the data
> (
> as
> well as the usual DataGrid view all as plugins ). Then the delegates
> correctly fire and everything works as advertised. The problem arises
> if I
> instantiate a second instance of say the Bar Chart. When that happens
> the
> first Bar Chart stops responding/refreshing while the second Bar Chart
> updates correctly.
>
> Can any C# pattern gurus shed some light on what I might have
> overlooked?


Dec 8 '05 #4
Dominique wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?


I've seen some strange effects similar to this before. You could try
redesigning your chart and its data source to model the observer
pattern. This way you wouldnt need the event and the delegate chain.
Here is a good example of the observer pattern:
http://www.dofactory.com/Patterns/PatternObserver.aspx

Jason
Dec 9 '05 #5
Hi Dominique,

It may sound strange but can you check the id's of the views you've created ?
If the id's are the same than that's the problem. I've had a problem with
mutiple custom controls that raise events, they had the same Id. After
solving that problem things went ok again.

It's of course just a guess.

Good Luck and let us know when you've solved the problem Ok.
--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 9 '05 #6
Hi Rainer,
Can you clarify what you mean by ID's? And how do I get my views to have
separte IDs?

If you mean that I should be generating a GUID or similar for each instance,
I assumed ( possibly wrongly ) that a new instance of a class would make the
instance unique as it is created at a separate date and time to other classes.

Thanks,
Dominique.

Dominique

"Rainier [MCT]" wrote:
Hi Dominique,

It may sound strange but can you check the id's of the views you've created ?
If the id's are the same than that's the problem. I've had a problem with
mutiple custom controls that raise events, they had the same Id. After
solving that problem things went ok again.

It's of course just a guess.

Good Luck and let us know when you've solved the problem Ok.
--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 9 '05 #7
Hi John,
I'm not yet totally familiar with the observer pattern, but it sounds like
it only waits to be notified of changes, but not not actually produce
changes. Is this a correct interpretation of the pattern? If so, then it
won't do in this case as I allow the user to interact with the graph to
change sales values, creative accounting if you will :).

Regards,
Dominique.

"Jason Shortt" wrote:
Dominique wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?


I've seen some strange effects similar to this before. You could try
redesigning your chart and its data source to model the observer
pattern. This way you wouldnt need the event and the delegate chain.
Here is a good example of the observer pattern:
http://www.dofactory.com/Patterns/PatternObserver.aspx

Jason

Dec 9 '05 #8
Hi Dominique,

The thing I had, I was programming Asp.Net Server controls creating
instances of these and adding them to a single page. This didn't work because
of the UniqueId that wasn't Unique.
I created the UniqueId's myself and afther that the code started working.
The real problem here is that the code I'm talking about is from a old
project for a old emplorer on an old laptop. So there really isn't a way for
me to show you the code.

Maybe you can set break points within your views to check there
UniqueId's/Guids just try checking everything that needs to be unique when
creating new instances.

Good luck,

--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
Hi Rainer,
Can you clarify what you mean by ID's? And how do I get my views to have
separte IDs?

If you mean that I should be generating a GUID or similar for each instance,
I assumed ( possibly wrongly ) that a new instance of a class would make the
instance unique as it is created at a separate date and time to other classes.

Thanks,
Dominique.

Dominique

"Rainier [MCT]" wrote:
Hi Dominique,

It may sound strange but can you check the id's of the views you've created ?
If the id's are the same than that's the problem. I've had a problem with
mutiple custom controls that raise events, they had the same Id. After
solving that problem things went ok again.

It's of course just a guess.

Good Luck and let us know when you've solved the problem Ok.
--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Dominique" wrote:
In order to become more familiar with the Model-View-Controller pattern, I
have written a demo where each View is a plugin and the plugins are loaded at
startup from the plugins directory. When the View's are loaded they add
themselves to the DataModel View list...

FDataModel.AddV iew(this);

Where AddView looks like this...
public void AddView( MvcView view )
{
ViewChanged += new ViewChangedHand ler(view.DataMo dified);
}

and at the right time I invoke the event list via...
ViewChanged(thi s, e);

This all works fine as long as I only have 1 instance of each type of view.
So in this case I have a Pie Chart View and Bar Chart view of the data ( as
well as the usual DataGrid view all as plugins ). Then the delegates
correctly fire and everything works as advertised. The problem arises if I
instantiate a second instance of say the Bar Chart. When that happens the
first Bar Chart stops responding/refreshing while the second Bar Chart
updates correctly.

Can any C# pattern gurus shed some light on what I might have overlooked?

Dec 12 '05 #9

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

Similar topics

6
2287
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the Sun stance on lack of support for delegates: .... There are serious semantic problems with trying to add delegates to a language in a consistent way. The main problem is that once you call the delegate, the original class instance is no longer...
0
375
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can fail if the event is emptied of all methods between the two statements, implying that some sort of synchronization between this and removals from EventName is needed. The other problem is that if an event with a set of delegates is in the process...
4
339
by: Stephen | last post by:
I am new to C# and can't get my head round what delegates are and what they are for. can anyone enlighten me?
3
2410
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to built in a class library that had multithreading enabled.
15
1882
by: Marshal | last post by:
First... let's deal with Delegates. Comments welcome. 1) Invoking a NULL delegate is annoying. ** (It should just do nothing rather than crash.) 2) It's too easy to accidently attach multiple redundant handlers onto delegates. 3) A delegate might point to handlers on objects that are out of scope or been disposed, causing those objects to crash.
14
3258
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB asynchronously (could be a timer but then we get Thread C) Upon completion of DelegateB, Thread B would like to callback ThreadA using DelegateA but as we all know the call to DelegateA is running in ThreadB. Is
4
1875
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a link to a site that describes delegates using VB.Net in a manner that doesn't require rocket science to become enlightened. TIA
4
5829
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with the event publisher and subscribe for the events that they are interested in. When a certain event happens, the subscribers are notified about it. I want the clients to return a value after their callback method is called. If any of the client...
4
2817
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to another library and call this list of functions. This could provide a new way to build rule base...
9
3115
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying to store multicast delegates in a hash table, and then fire the delegates one of two ways (after registering/ creating the delegates, etc).
0
8899
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9418
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9196
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8124
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4790
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3235
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.