Windows Form applications (Microsoft.Net Desktop applications) sometimes start consuming a lot of memory, which has different drawbacks, but I'll not get into the details of them here. What we'll look into is how to avoid the excess use of the memory resource in a windows application in C#.
In fact, you need to flush memory to release all the unused memory to make more space available for your current and other applications. First of all include following two namespaces:
using Microsoft.Win32;
using System.Runtime.InteropServices;
and then use the following code snippet to flush excesssive use of memory. You can view the currently consumed memory by viewing the application process name in the currently running processes list. For that purpose just go to the Task Manager, and then go to Processes tab and find out your application's process.
Following code imports a win32 dll and flushes the memory. But, before that it also uses Microsoft.Net's garbage collector to force an immidiate garbage collection:
public class MemoryManagement
{
[DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =
CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int
maximumWorkingSetSize);
public static void FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
copy and use this class in your code and have fun reducing the memory size!
Visual Studio 2005, 2008; ASP.NET 2.0, 3.0, 3.5; ADO.NET; C#; Visual Basic.NET; XML; Web Services; Windows Services; AJAX; Silverlight; LINQ; Visual Studio Team System; REST;
Showing posts with label Windows Form. Show all posts
Showing posts with label Windows Form. Show all posts
Thursday, February 07, 2008
Monday, January 28, 2008
Build transparent windows form using C#
Developing a transparent windows form in Visual Studio 2005 using C# is not that big deal! By just setting a few properties, you can design a windows form you can look through. This article elaborates the process of converting a windows form into a transparent form using C#.
Create Visual Studio 2005 project using C# as a language of choice. In the picture of the transparent form I'm about to show you, I just made it borderless by settings its FormBorderStyle property to None. It is just to make it look interesting, otherwise it has nothing to do with the actual process of making it transparent.
To make the windows form look transparent just set its BackColor property to whatever color you like (or the color you don't like...because this color will not be visible due to transparency :) ). I set it to MediumSlateBlue. The second most important property which you need to set to achieve the objective of form transparency is Transparencykey. The important point to note here is that the color should be same in TransparencyKey and Form's BackColor. So, I'll set the TransparencyKey value to MediumSlateBlue.
Create Visual Studio 2005 project using C# as a language of choice. In the picture of the transparent form I'm about to show you, I just made it borderless by settings its FormBorderStyle property to None. It is just to make it look interesting, otherwise it has nothing to do with the actual process of making it transparent.
To make the windows form look transparent just set its BackColor property to whatever color you like (or the color you don't like...because this color will not be visible due to transparency :) ). I set it to MediumSlateBlue. The second most important property which you need to set to achieve the objective of form transparency is Transparencykey. The important point to note here is that the color should be same in TransparencyKey and Form's BackColor. So, I'll set the TransparencyKey value to MediumSlateBlue.
It looks like a broken form, but actually its not! It is a transparent windows form shown on my computer's desktop. The bars at the top and left (if you can see that as well :) ) are the labels with some other background color. And there are two text boxes and one close button on the form that are viewable and working, while you can see through the form.
Labels:
BackColor,
C#,
FormBorderStyle,
TransparencyKey,
Windows Form
Add windows form application in registry to run at startup
If you want to install a windows application on the client machine, specially if you have to deploy a smart client application and you also want to run that application on startup, then you can add the path of the executable of the application in the registry. In this article, I'll show how you can do this.
First of all, import following namespace in your C# Windows Form application project.
using Microsoft.Win32;
Now, you have to add following code on the application load.
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Above line of code opens a registry sub key which allows to run the application at startup. In the following line you have to check that if your required registry key value is not there then add it otherwise you can ignore, so you wouldn't add it every time your application loads.
if (rkApp.GetValue("MyAppRegKey") == null)
{
rkApp.SetValue("MyAppRegKey", Application.ExecutablePath.ToString());
}
After running the application for the first time you'll see MyAppRegKey at the following path in registry editor SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run. You'll have to look for this path in the current user category of the registry editor
Oh...by the way, you can open registry editor as under:
Start -> Run -> Type regedit and Press Enter key.
First of all, import following namespace in your C# Windows Form application project.
using Microsoft.Win32;
Now, you have to add following code on the application load.
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Above line of code opens a registry sub key which allows to run the application at startup. In the following line you have to check that if your required registry key value is not there then add it otherwise you can ignore, so you wouldn't add it every time your application loads.
if (rkApp.GetValue("MyAppRegKey") == null)
{
rkApp.SetValue("MyAppRegKey", Application.ExecutablePath.ToString());
}
After running the application for the first time you'll see MyAppRegKey at the following path in registry editor SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run. You'll have to look for this path in the current user category of the registry editor
Oh...by the way, you can open registry editor as under:
Start -> Run -> Type regedit and Press Enter key.
Labels:
C#,
Microsoft.Win32,
RegistryKey,
Startup,
Windows Form
Subscribe to:
Posts (Atom)