1 static DispatcherTimer GCTimer = new DispatcherTimer();
2 public static void BeginGC()
3 {
4 GCTimer.Interval = TimeSpan.FromMinutes(0.5); //垃圾释放定时器 我定为每十分钟释放一次,大家可根据需要修改
5 GCTimer.Start();
6
7 EventsRegistion(); // 注册事件
8 }
9
10 public static void EventsRegistion()
11 {
12 GCTimer.Tick += new EventHandler(OnGarbageCollection);
13 }
14
15 public static void EventDeregistration()
16 {
17 GCTimer.Tick -= new EventHandler(OnGarbageCollection);
18 }
19
20 static void OnGarbageCollection(object sender, EventArgs e)
21 {
22 GC.Collect();
23 GC.WaitForPendingFinalizers();
24 GC.Collect();
25 }