Beispiel #1
0
 private void NetDetailForm_Load(object sender, EventArgs e)
 {
     #region 启动任务
     IP = GetIP();
     Task.Factory.StartNew(() =>
     {
         try
         {
             while (!this.IsDisposed)
             {
                 GetNetProcess();
                 this.dataGridView1.Invoke(new Action(() =>
                 {
                     foreach (var p in netProcesses)
                     {
                         bool inView = false;
                         for (int i = 0; i < dataGridView1.Rows.Count; i++)
                         {
                             if (dataGridView1.Rows[i].Cells["Column3"].Value.ToString() == p.ProcessID.ToString())
                             {
                                 inView = true;
                                 dataGridView1.Rows[i].Cells["Column4"].Value = ByteConvertTool.Fmt(p.UpLoad) + "/s";
                                 dataGridView1.Rows[i].Cells["Column7"].Value = ByteConvertTool.Fmt(p.UpLoadCount);
                                 dataGridView1.Rows[i].Cells["Column5"].Value = ByteConvertTool.Fmt(p.DownLoad) + "/s";
                                 dataGridView1.Rows[i].Cells["Column8"].Value = ByteConvertTool.Fmt(p.DownLoadCount);
                                 dataGridView1.Rows[i].Cells["Column6"].Value = ByteConvertTool.Fmt(p.UpLoadCount + p.DownLoadCount);
                             }
                         }
                         if (!inView)
                         {
                             dataGridView1.Rows.Add(new object[] {
                                 p.ProcessICon,
                                 ProcessAPI.GetProcessNameByPID(p.ProcessID),
                                 p.ProcessID.ToString(),
                                 ByteConvertTool.Fmt(p.UpLoad) + "/s",
                                 ByteConvertTool.Fmt(p.UpLoadCount),
                                 ByteConvertTool.Fmt(p.DownLoad) + "/s",
                                 ByteConvertTool.Fmt(p.DownLoadCount),
                                 ByteConvertTool.Fmt(p.UpLoadCount + p.DownLoadCount)
                             });
                         }
                     }
                 }));
                 CalcBagFlow();
                 this.status.Invoke(new Action(() =>
                 {
                     status.Text = string.Format("信息:IP:{0},上传流量:{1},下载流量:{2}",
                                                 IP, ByteConvertTool.Fmt(NetWorkService.NowSent), ByteConvertTool.Fmt(NetWorkService.NowReceived));
                     status.Text += string.Format(",单位时间:{0}分钟,上传流量:{1},下载流量:{2},下次刷新时间:{3}",
                                                  NetWorkService.ThresholdTime, ByteConvertTool.Fmt(NetWorkService.UnitSent), ByteConvertTool.Fmt(NetWorkService.UnitReceived), NetWorkService.CalcTime.ToString("yyyy-MM-dd HH:mm:ss"));
                 }));
                 Thread.Sleep(1000);
             }
         }
         catch { }
     });
     CaptureInit();
     Task.Factory.StartNew(() => { try { _rs.Capture(); } catch { } });
     #endregion
 }
Beispiel #2
0
 private static void GetNetBag()
 {
     NS                  = new NativeSocket2(IPAddress.Parse(IP));
     NS.IsStart          = true;
     NS.OnIPPacketCapure = (IPPacket tp) =>
     {
         BagCount++;
         if (tp.SrcAddr.ToString() == IP)
         {
             //源地址是本机-从本机发出
             lock (netProcesses)
             {
                 bool _in = false;
                 foreach (var item in netProcesses)
                 {
                     int inPort = item.Ports.Where(x => x.Port == tp.SrcPort).Count();;
                     if (inPort > 0)
                     {
                         item.UpBag++;
                         NowBag++;
                         _in = true;
                         //item.Upload += tp.DataLen;
                         //item.FlowCount += tp.DataLen;
                     }
                 }
                 if (!_in)
                 {
                     NowBadBag++;
                 }
             }
         }
         if (tp.DestAddr.ToString() == IP)
         {
             //目标地址是本机-本机接收
             lock (netProcesses)
             {
                 bool _in = false;
                 foreach (var item in netProcesses)
                 {
                     int inPort = item.Ports.Where(x => x.Port == tp.DestPort).Count();;
                     if (inPort > 0)
                     {
                         item.DownBag++;
                         NowBag++;
                         _in = true;
                         //item.DownLoad += tp.DataLen;
                         //item.FlowCount += tp.DataLen;
                     }
                 }
                 if (!_in)
                 {
                     NowBadBag++;
                 }
             }
         }
     };
     Task.Factory.StartNew(() => { NS.Capture(); });
 }
Beispiel #3
0
        private void NetReportForm_Load(object sender, EventArgs e)
        {
            #region 启动功能
            this.ShowInTaskbar = false;
            this.Hide();
            IP = GetIP();
            Task.Factory.StartNew(() =>
            {
                try
                {
                    while (!this.IsDisposed)
                    {
                        GetNetProcess();
                        CalcBagFlow();
                        this.Invoke(new Action(() =>
                        {
                            status.Text = string.Format("信息:IP:{0},上传流量:{1},下载流量:{2}",
                                                        IP, ByteConvertTool.Fmt(NetWorkService.NowSent), ByteConvertTool.Fmt(NetWorkService.NowReceived));
                            status.Text += string.Format("\n单位时间:{0}分钟,上传流量:{1},下载流量:{2}",
                                                         NetWorkService.ThresholdTime, ByteConvertTool.Fmt(NetWorkService.UnitSent), ByteConvertTool.Fmt(NetWorkService.UnitReceived));
                            status.Text += string.Format("\n当前时间:{0},下次刷新时间:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), NetWorkService.CalcTime.ToString("yyyy-MM-dd HH:mm:ss"));

                            if ((NetWorkService.UnitSent + NetWorkService.UnitReceived) < NetWorkService.FlowThreshold || DateTime.Now > MaxTime)
                            {
                                NetWorkService.WriteRecord(BeginTime, DateTime.Now, netProcesses, IP);
                                this.Dispose();
                            }
                        }));
                        Thread.Sleep(1000);
                    }
                }
                catch { }
            });
            CaptureInit();
            Task.Factory.StartNew(() => { try { _rs.Capture(); } catch { } });
            #endregion
        }