C# Timer 控件多線程內存釋放


第一次寫博客,請大家見諒。

  C#的Timer控件是一個多線程的控件,當大量的訪問其它控件的時候就會不斷的消耗內存,雖然C#具備垃圾回收機制,但是也應該不斷的進行內存的釋放,防止在沒有回收垃圾之前造成的系統崩潰,本人親試,沒有問題。代碼如下:

private void timer1_Tick(object sender, EventArgs e)
{
//輪巡人員考勤數組,將新刷臉信息進行顯示
if (arr_Pic.Count <= 0)
{
return;
}

if (Arr_Index >= arr_Pic.Count)
{
Arr_Index = 0;
}
else
{
Arr_Index++;
}

if (Arr_Index - 1 < 0)
{
this.pb_zp.Image = Image.FromFile(Zm_Path + "//DisPlayPic//" + arr_Pic[0].ToString());
}
else
{
this.pb_zp.Image = Image.FromFile(Zm_Path + "//DisPlayPic//" + arr_Pic[Arr_Index - 1].ToString());
}

ClearMemory(); //釋放內存。
}

 

[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);

public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM