As daily develop in C# .Net, I collected some of useful code snippet, including some tricky and system call. And this code snippet list will grow as long as further collecting.
using System.Windows.Forms; privatestatic Boolean isMouseDown; privatestatic Point mouseStartPoint = new Point(0, 0); privatestatic Point formStartPosition = new Point(0, 0); publicstaticvoidMouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { try { formStartPosition = (((Control)sender).TopLevelControl).Location; } catch { return; } isMouseDown = true; mouseStartPoint.X = Control.MousePosition.X; mouseStartPoint.Y = Control.MousePosition.Y; } } publicstaticvoidMouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMouseDown = false; } } publicstaticvoidMouseMove(object sender, MouseEventArgs e) { if (isMouseDown) { Point mouseNowPoint = Control.MousePosition; Point formNowPosition = new Point(formStartPosition.X + mouseNowPoint.X - mouseStartPoint.X, formStartPosition.Y + mouseNowPoint.Y - mouseStartPoint.Y); (((Control)sender).TopLevelControl).Location = formNowPosition; } }
//register the event on the form publicvoidForm_Load(object sender, EventArgs e) { this.MouseDown += new MouseEventHandler(SFC.Common.Common.MouseDown); this.MouseUp += new MouseEventHandler(SFC.Common.Common.MouseUp); this.MouseMove += new MouseEventHandler(SFC.Common.Common.MouseMove); }
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] publicstaticexternintSetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); publicstaticvoidClearMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);//the function removes as many pages as possible from the working set of the specified process. } }
below is a comment on the using of “SetProcessWorkingSetSize”, which demonstrates that this function is useful in two situation:
An application with GUI, this function could be execute after the application initialized.
You want your application to run in swap pages, and occupy less memory.
We have found out that, for a GUI application written in Delphi for Win32/Win64 or written in a similar way that uses large and heavy libraries on top of the Win32 API (GDI, etc), it is worth calling SetProcessWorkingSetSize once.
We call it with (… -1, -1) parameters, within a fraction of second after the application has fully opened and showed the main window to the user. In this case, the SetProcessWorkingSetSize(… -1, -1) releases lots of startup code that seem to not be needed any more. The memory quickly restores to about 1/3 of what it would have been without the SetProcessWorkingSetSize(… -1, -1), and does not grow more since then (unless the application allocates more memory). So we have effectively saved 2/3 of the memory of mostly startup code (loading and parsing of configuration files, initializing GUI, etc) that would not be needed to continue running the application.
If you have a GUI application, you may test on your own application the same way - just call SetProcessWorkingSetSize once and see how many memory have been released definitely.
Even for a server (service) application that don’t have GUI - I think that calling SetProcessWorkingSetSize once after the server has fully loaded and initialized might have been useful.
// check characters bool a = Char.IsDigit(char1); bool b = Char.IsDigit(char2); bool c = Char.IsDigit(str1, 2); // position 3 bool d = Char.IsDigit(str2, 0); // position 1
// print out returned values System.Console.WriteLine(a); System.Console.WriteLine(b); System.Console.WriteLine(c); System.Console.WriteLine(d); } }