111,128
社区成员
发帖
与我相关
我的任务
分享 for (int j = 2; j <= 50; j++)
{
if (!(j == 24 || j == 48 || j == 33))
{
for (int i = 2; i <= 254; i++)
{
using (Ping myPing = new Ping())
{ //Ping myPing;
// myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(myPing_PingCompleted);
string pingIP = "192.168." + j.ToString() + "." + i.ToString();
myPing.SendAsync(pingIP, 1000, null);
// myPing = null;
// pingIP = null;
}
}
}
//Application.DoEvents();
}这里面的Ping myPing = new Ping()不释放 加using解决
通过查看本程序使用内存情况就能定位哪里的代码占用内存比较多从而进行优化
private void press()
{
//新建一个Stopwatch变量用来统计程序运行时间
// Stopwatch watch = Stopwatch.StartNew();
//获取本机运行的所有进程ID和进程名,并输出哥进程所使用的工作集和私有工作集
foreach (Process ps in Process.GetProcesses())
{
PerformanceCounter pf1 = new PerformanceCounter("Process", "Working Set - Private", ps.ProcessName);
PerformanceCounter pf2 = new PerformanceCounter("Process", "Working Set", ps.ProcessName);
if (ps.ProcessName == "PcIpAddress")
{
listBox1.Items.Add(ps.ProcessName + "工作集(进程类)" + (ps.WorkingSet64 / 1024).ToString());
listBox1.Items.Add(ps.ProcessName + "工作集" + (pf2.NextValue() / 1024).ToString());
listBox1.Items.Add(ps.ProcessName + "工作集" + (pf1.NextValue() / 1024).ToString());
}
}
// watch.Stop();
// Console.WriteLine(watch.Elapsed);
}
bool complete = true;
bool complete2 = true;
private void timer1_Tick(object sender, EventArgs e)
{
ts = TimeSpan.Parse(DateTime.Now.ToString("HH:mm:ss"));
if (!string.IsNullOrEmpty(comboBox1.Text ))
{
if (ts > TimeSpan.Parse(comboBox1.Text + ":00:00") && ts < TimeSpan.Parse(comboBox1.Text + ":00:03"))
{
if (complete == true)
{
complete = false;
listBoxAddrs.Items.Clear();
kg = true;
try
{
Ping myPing;
myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(myPing_PingCompleted);
for (int j = 2; j <= 50; j++)
{
if (!(j == 24 || j == 48 || j == 33))
{
for (int i = 2; i <= 254; i++)
{
string pingIP = "192.168." + j.ToString() + "." + i.ToString();
myPing.SendAsync(pingIP, 1000, null);
}
}
myPing = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
complete = true;
}
}
}
//if (kg)
//{
// System.Threading.Thread.Sleep(4000);
//}
//DateTime dt = DateTime.Parse(textBox1.Text);
//if (DateTime.Now>=dt && )
//{
// MessageBox.Show("ddd");
//}
// string selectStr1 = "select pc.pcmacadd,pc.pcremark,pc.pcaddress,pc.pcuser,pc.pcygid,dept.deptName from pc,dept where pc.deptID=dept.deptID and pc.pcip not in (SELECT pcIP FROM pc where pcAddress like '%磅%' or pcAddress like '%监控%' or pcAddress like '%开票%' or pcAddress like '%门%' or pcAddress like '%库%' or pcAddress like '%调度%' or pcAddress like '%主控%' or pcAddress like '%取样%') and pcremark<>'路由器' and pcremark<>'倒班' and pcremark<>'值班24小时' and pc.pcIP='";
if (kg)
{
if (listBoxAddrs.Items.Count > 0 && complete2 = true)
{
complete2 = false;
selectStr = string.Empty;
kk = string.Empty;
//IPbutton.Enabled = false;
//databt.Enabled = false;
//label1.Text = "不要关闭程序正在做数据操作";
string strdt = DateTime.Now.ToString("yyyyMMddHHmm");
using (SqlConnection connection = new SqlConnection(strConn))
{
command.Connection.Open();
foreach (string str in listBoxAddrs.Items)
{
selectStr = selectStr1 + str.Substring(8, str.Length - 8) + "'";
try
{
SqlCommand command = new SqlCommand(selectStr, connection);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
//label1.Text = "name:" + reader["name"].ToString(); //数据读取
{
kk = "YG" + reader["pcygid"].ToString() + "," + reader["pcuser"].ToString() + "," + reader["pcremark"].ToString() + "," + reader["deptName"].ToString() + "," + reader["pcaddress"].ToString() + "," + reader["pcmacadd"] + "," + str;
if (!File.Exists(strdt + "PCTxt.txt"))
{
using (FileStream fs1 = new FileStream(strdt + "PCTxt.txt", FileMode.Create, FileAccess.Write))
{//创建写入文件
using (StreamWriter sw = new StreamWriter(fs1))
{
sw.WriteLine(kk + "," + DateTime.Now);//开始写入值
sw.Flush();
}
}
}
else
{
using (FileStream fs = new FileStream(strdt + "PCTxt.txt", FileMode.Append))
{
using (StreamWriter sr = new StreamWriter(fs))
{
sr.WriteLine(kk + "," + DateTime.Now);//开始写入值
sr.Flush();
}
}
}
}
}
}
catch (SqlException ex)
{
throw ex;
}
}
}
//IPbutton.Enabled = true;
//databt.Enabled = true;
//label1.Text = "查询操作完毕请找PCtxt文件";
complete2 = true;
listBoxAddrs.Items.Clear();
kg = false;
num = 0;
}
else
{
lbIPs.Text = "没有查到在线的电脑";
}
recordTime = DateTime.Now.Subtract(startTime);
if (recordTime.Seconds % 3 == 0)
{
//3秒中手动回收一次内存
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
private void myPing_PingCompleted(object sender, PingCompletedEventArgs e)
{
//StringBuilder sb = new StringBuilder();
if (e.Reply.Status == IPStatus.Success)
{
if (e.Reply.Address.ToString().Trim() != "")
{
this.listBoxAddrs.Items.Add(e.Reply.Address.ToString());
//string mac = GetMacAddress(e.Reply.Address.ToString());
//sb.Append(" " + mac );
num++;
}
}
//this.txtAddrs.Text += sb.ToString();
this.lbIPs.Text = "在线IP数:" + num.ToString();
}