110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
namespace MemTest
{
class Program
{
Ping ping1 = new Ping();//如果将ping1放在theout方法外面,结果见3.5或4.0_out
static void Main(string[] args)
{
Program p1 = new Program();
System.Timers.Timer t = new System.Timers.Timer();
//订阅t.Elapsed事件发生时执行theout方法
t.Elapsed += new System.Timers.ElapsedEventHandler(p1.theout);
//间隔时间,毫秒
t.Interval = 1000;
//设置是执行一次(false)还是一直执行(true);
t.AutoReset = true;
//是否执行System.Timers.Timer.Elapsed事件;
t.Enabled = true;
Console.Read();
}
private void theout(object source, System.Timers.ElapsedEventArgs e)
{
//Ping ping1 = new Ping();//如果将ping1放在theout方法里面,结果见3.5或4.0_in
ping1.SendAsync("127.0.0.1", null);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
namespace MemTest
{
class Program
{
//Ping ping1 = new Ping();
static void Main(string[] args)
{
Program p1 = new Program();
System.Timers.Timer t = new System.Timers.Timer();
//订阅t.Elapsed事件发生时执行theout方法
t.Elapsed += new System.Timers.ElapsedEventHandler(p1.theout);
//间隔时间,毫秒
t.Interval = 1000;
//设置是执行一次(false)还是一直执行(true);
t.AutoReset = true;
//是否执行System.Timers.Timer.Elapsed事件;
t.Enabled = true;
Console.Read();
}
private void theout(object source, System.Timers.ElapsedEventArgs e)
{
using(Ping ping1 = new Ping()) //将ping1放在此处可完美解决问题,3.5下有效,内存维持低位,且ping间隔可以很小
{
ping1.SendAsync("127.0.0.1", null);
}
}
}
}