473,320 Members | 1,832 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,320 software developers and data experts.

Unloadable Plugin Architecture in .NET

This is my first question here, so apologies if I have made a mistake.

I am developing a collection of small games using WPF with C# but have been having real difficulty in being able to load and unload each game into my main viewer application.

The structure of my program is I have a main view application GameViewer.EXE which basically consists of a Frame object to view pages.
Each game is a custom class GamePage.CS, shown below:

Expand|Select|Wrap|Line Numbers
  1. GameLibrary.DLL
  2.  
  3. namespace GameLibrary
  4. {
  5.  public class GamePage : Page
  6.     {
  7.         public Guid GameID;
  8.         public string GameName;
  9.         public string Description;
  10.         public virtual void PauseGame(){}
  11.  
  12.        public event GameEndArgsEventHandler GameEnd;
  13.  
  14.         protected void OnGameEnd(GameEndArgs e)
  15.         {
  16.             if (GameEnd != null)
  17.                 GameEnd(this, e);
  18.         }
  19.    }
  20.  
  21.  public class GameEndArgs : EventArgs
  22.     {
  23.         public double Score;
  24.         public bool Finished;
  25.         public int Level = 1;
  26.     }
  27. }

I also have a ControlLibrary.DLL , which contains a collection of stylized controls, i.e. customs, sliders, etc., which is used in all games and the main interface GameViewer.EXE.

The structure of the application is:
Expand|Select|Wrap|Line Numbers
  1. Home\
  2.          GameViewer.EXE
  3.          GameLibrary.DLL
  4.          ControlLibrary.DLL
  5.  
  6.         Games\
  7.                   Game1\
  8.                              game1.DLL
  9.                              game1Dependency1.DLL
  10.                              game1Dependency2.DLL
  11.                   Game2\
  12.                             game2.DLL
  13.                   Game3\
  14.                             game3.DLL
  15.                             image\
  16.                      ...
  17.                  Game n

My trouble is being able to load then successfully unload each game DLL into my main viewer. I have been reading dozen of threads, but I cannot seem to get anything to work. I am trying to create a new AppDomain load the assembly in there and create the GamePage object.
- I can then unload the appdomain when the game is finished, but on repeated opening and closing the game I see the memory usuage shoot up, so something is going wrong.
- I have also tried creating a separate loader, using the MarshalByRefObject, but on returning a GamePage I am told that GamePage is not Serializable.

This is my current attempt:

In the GameLibrary.DLL I have:
Expand|Select|Wrap|Line Numbers
  1. namespace GameLibrary
  2. {
  3.      [Serializable]
  4.       public class Factory
  5.       {
  6.           public GamePage CreateGamePageObject(string path, string typename)
  7.           {
  8.               return Activator.CreateInstanceFrom(path, typename).Unwrap() as GamePage;
  9.            }
  10.       }
  11. }
Then in the my app I have:
Expand|Select|Wrap|Line Numbers
  1.         private AppDomain domain  =null;
  2.  
  3.          void LoadGame(string GameFile)
  4.           {
  5.                 domain = AppDomain.CreateDomain("Remote Load");
  6.  
  7.                 ObjectHandle oh = domain.CreateInstance("GameLibrary", "GameLibrary.Factory");
  8.                 Object obj = oh.Unwrap();
  9.                 GameLibrary.Factory f = obj as GameLibrary.Factory;
  10.                 GameLibrary.GamePage addIn=null;
  11.  
  12.                 FileInfo fi = new FileInfo(GameFile);
  13.                addIn =f.CreateGamePageObject(fi.FullName, fi.Name.Substring(0, fi.Name.Length - 4) + ".Page1");
  14.  
  15.                 MainFrame.Content = addIn;
  16.           }
  17.          void UnloadGame()
  18.          {
  19.                MainFrame.Content = null;
  20.                AppDomain.Unload(domain);
  21.          }
  22.  
The trouble with this is the memory leak.

I would greatly appreciate any help and suggestions.
Nov 26 '09 #1
1 2164
I have since realised that the reason for the increasing memory usage was due to the use of a Frame, which would store a history of past pages. By setting Frame.JournalOwnership to UsesParentJournal, this problem is avoided.
Nov 27 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jer | last post by:
Hi all, I am trying to get plugins working in my application. I am using the approach of writing an interface and then implementing that interface in each plugin. I would like to be able to...
2
by: Chua Wen Ching | last post by:
Hi there, I had some doubts on creatings plugins. As most example on the internet shows how to write plugins onto a plugin host which is normally a windows form program. 1) Can i replace...
5
by: Tamir Khason | last post by:
Friends, maybe someone knows good references for .NET plugin based program architecture. E.g I want to be able to "put" class library(dll) in some place (where I do not the name of the class, but...
2
by: Matt | last post by:
I'm hoping someone can steer me in the right direction to try to do the following: I am developing an application where we receive files from customers. Right now we receive a variety of...
3
by: CSharpNewBie | last post by:
Hi I am looking to create a Plugin Architecture. I looked at this extend or enhance the User Interface (UI) article http://www.codeproject.com/csharp/extensibleui.asp and It looks good and I...
1
by: Craig Buchanan | last post by:
I am building an application to help researcher record the outcomes of their studies. These outcomes are document across a number of paper forms. The information documented on these forms varies...
0
by: Craig Buchanan | last post by:
I am building an application to help researcher record the outcomes of their studies. These outcomes are document across a number of paper forms. The information documented on these forms varies...
0
by: Dan Dorey | last post by:
I'm in the midst of creating a plugin framework with the goal of making it as easy as possible for myself and other developers to both create new plugins and work with existing ones. Each plugin...
4
by: anglozaxxon | last post by:
I'm making a program that consists of a main engine + plugins. Both are in Python. My question is, how do I go about importing arbitrary code and have it be able to use the engine's functions,...
1
by: Olie | last post by:
I was quite surprised to find virtually nothing about this but I may have been searching for the wrong thing. I want to know the best way to provide a programming interface to a plugin. I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.