第一次写博客,请大家见谅。

  C#的Timer控件是一个多线程的控件,当大量的访问其它控件的时候就会不断的消耗内存,虽然C#具备垃圾回收机制,但是也应该不断的进行内存的释放,防止在没有回收垃圾之前造成的系统崩溃,本人亲试,没有问题。代码如下:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

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);
}
}

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄