Application Management

Application Management makes your application's life easier. It will automatically do memory management, handle and log unhandled exceptions, profile your functions, make your application single instance, and also provide utility functions to get system information.

Application Management makes your application life easier. It will automatic do memory management, handle and log unhandled exceptions, profile your functions, make your application single instance, and also provide utility functions to get system information.

This library works on multiple platforms, and was tested on Windows XP, Windows 7, and Linux Ubuntu 9.10. For Linux, I have supplied Mono compilations that you can recompile or execute. The Debug and Release folders under bin were compiled used Visual Studio 2008 (Windows). The Mono folder under bin was compiled using MonoDevelop (Mono systems compatible).

Classes and functions are protected from incompatibility OS exceptions. All WinAPI functions wont work outside a Windows system.

publicstatic Instance instance;
[STAThread]
staticvoid Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#region Instance Class (Make single instance app)
// Check if that is a single instance, and name mutex as: "{ProductName}Mutex"
instance = new Instance(Application.ProductName);
// instance is created but make sure you call next line:
// First parameter is used to define mutex level (Global or this session only)
// Second parameter is used to check processes equality after check mutex
if (!instance.MakeSingleInstance(true, false))
{
Instance.ShowDuplicateInstanceDialogError();
// Stop and quit App.
return;
}
#endregion
Application.Run(new MainFrm());
// Note: by calling 'Instance.Dispose' method will release mutex,
// and app will be multi instance,
// But still checks for process equality!
// instance.Dispose();
}

Note: To avoid duplicate keys and for fast coding, you can use Profiling.ProfilingData. Again, the Profiling class is optional and is just a wrapper of the Profiling.ProfilingData class that holds a Dictionary<string, Profiling.ProfilingData>. But, if you want to keep and store a profiling function to use later or to get results, it is better to use the Profiling class.

///<summary>/// Under Windows Vista, if current user is not an administrator,
/// try elevate the permissions and exit from current application.
///</summary>if(!VistaSecurity.IsAdmin())
{
if(VistaSecurity.RestartElevated(Application.ExecutablePath))
Application.Exit();
}

Please let me know if you are using my library in any of your projects. You can post your application name and link on discussions. This is also an incentive to me, knowing that people are using my libraries.

Comments and Discussions

Your memory class is poorly done ... you use the SetProcessWorkingSetSize api on a timer...

Three things about this:

1
First do you know what the SetProcessWorkingSetSize does? ... It doesn't really free up memory but instead forces the specified amount of memory to be transferred FROM memory TO the swap file ... effectively slowing down the program when the data has to be transferred back into memory.

2
If you rely on this instead of cleaning up properly you will be more susceptible to other types of memory leaks such as GDI Leaks, User Object Leeks and Handle Leaks.

3
There is also a managed implementation of this:
System.Diagnostics.Process.GetCurrentProcess.MinWorkingSet = new IntPtr(1);

It's got potential to be a decent article, but here are the things I see wrong that, if improved, would make it a good one:

0) There's almost no description of the various classes.

1) There are several misspellings (for instance, "catch" is being spelled "cath" throughout the article)

2) Some of the screen shots aren't in English.

If you improve these things, I'll reconsider the vote of 3 that I gave you. To be completely honest, this article should not have been approved for publication in its current state.

.45 ACP - because shooting twice is just silly-----"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997-----"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

0) I really haven't very experience about documentations, can you give me an example or a link that show me how can i make it better?

1) i'm sorry, my English is not very good, my native language is Portuguese, i will fix some misspellings

2) Some of Screenshots are not in English because my operative system language is Portuguese, and some messages from .NET Framework are affected by your OS Language as you know, i current don't have any English OS to change that and also i can't change the language of my system.
The only string i found in other language is: "O valor não pode ser nulo" -> "The value can't be null"

.45 ACP - because shooting twice is just silly-----"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997-----"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

I don't know if it's against the rules or not - I'm just expressing my opinion.

In my view, if you want donations, you should mention it on your own web page. You can put a link to your home page in your profile.

.45 ACP - because shooting twice is just silly-----"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997-----"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

The single instance bit would be better if it activated the running instance and brought it to the front, rather than just crashing out. Also, you need to check for the session owner as it is no problem to have multiple instances running in separate sessions such as in terminal services.

The memory management section is a bit of a waste of time though. There should never be a need to explicitly garbage collect as the framework should be recycling its available memory automatically. Additionally, calling SetProcessWorkingSetSize is just a fob to incompetent users. It does not free any memory, it just moves it all into the page file, thereby slowing your application down, though it does make the casual user happy as the taskmanager shows less memory in use. Try running a proper performance analysis and you will see that the total memory used remains the same.

True memory management involves ensuring correct and timely disposal of objects, and reusing objects when a new instance is not really needed. If your application truly needs 1GB of memory in use at a time then shifting it to the page file is the worst thing you can do.