Exemple #1
0
        /// <summary>
        ///     菜单项“过滤本机流量”勾选状态改变时的事件。
        /// </summary>
        /// <param name="sender">触发事件的控件对象。</param>
        /// <param name="e">事件的参数。</param>
        private void 过滤本机流量ToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            // 检查监视器是否在工作
            if (开始监视ToolStripMenuItem.Text.Equals("开始监视"))
            {
                return;
            }

            // 暂停列表更新
            ConnectionListUpdateTimer.Stop();
            new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled);

            // 去除已有本机流量数据
            var removeRows = ConnectionList.Rows.Cast <DataGridViewRow>().Where(row =>
                                                                                IPAddress.Parse(row.Cells["SrcAddress"].Value.ToString().Substring(0, row.Cells["SrcAddress"].Value.ToString().LastIndexOf(':'))).Equals(Watcher.Ipv4Address) ||
                                                                                IPAddress.Parse(row.Cells["DstAddress"].Value.ToString().Substring(0, row.Cells["DstAddress"].Value.ToString().LastIndexOf(':')))
                                                                                .Equals(Watcher.Ipv4Address)).ToList();

            foreach (var row in removeRows)
            {
                ConnectionList.Rows.Remove(row);
            }

            // 恢复列表更新
            ConnectionListUpdateTimer.Start();
        }
Exemple #2
0
 /// <summary>
 ///     连接列表鼠标按键事件响应方法,暂停列表更新。
 /// </summary>
 /// <param name="sender">触发事件的控件对象。</param>
 /// <param name="e">事件的参数。</param>
 private void ConnectionList_MouseEvent(object sender, MouseEventArgs e)
 {
     if (ConnectionListUpdateTimer.Enabled)
     {
         ConnectionListUpdateTimer.Stop();
         new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled);
     }
     else if (!ConnectionListMenuStrip.Visible)
     {
         ConnectionListUpdateTimer.Start();
     }
 }
Exemple #3
0
 /// <summary>
 ///     连接列表右键菜单关闭时触发的事件,重新开始更新列表。
 /// </summary>
 /// <param name="sender">触发事件的控件对象。</param>
 /// <param name="e">事件的参数。</param>
 private void ConnectionListMenuStrip_Closed(object sender, ToolStripDropDownClosedEventArgs e)
 {
     ConnectionListUpdateTimer.Start();
 }
Exemple #4
0
        /// <summary>
        ///     菜单项“开始毒化”单击时的事件。
        /// </summary>
        /// <param name="sender">触发事件的控件对象。</param>
        /// <param name="e">事件的参数。</param>
        private void 开始监视ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (开始监视ToolStripMenuItem.Text == "开始监视")
            {
                // 创建载入界面
                var task = new Thread(watching => {
                    try {
                        Watcher.StartWatching();
                    }
                    catch (ThreadAbortException) {
                        // 用户中止了操作
                        Watcher.Stop();
                    }
                })
                {
                    Name = RegisteredThreadName.StartWatching.ToString()
                };
                MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task));
                var loading = new Loading("正在启动监视,请稍候", task);
                loading.ShowDialog();

                // 等待结果
                var result = Message.NoAvailableMessage;
                new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage);

                // 用户取消
                if (result == Message.UserCancel)
                {
                    MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskCancel, task));
                    new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage);

                    switch (result)
                    {
                    case Message.TaskAborted:
                        return;

                    case Message.TaskOut:
                        break;

                    case Message.TaskNotFound:
                        MessageBox.Show("未能找到指定名称的工作线程。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;

                    default:
                        throw new Exception($"无效的消息类型:{result}");
                    }
                }

                MessagePipe.ClearAllMessage(task);
                MessageBox.Show("监视工作已启动。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ConnectionListUpdateTimer.Start();
                开始监视ToolStripMenuItem.Text = "停止监视";
            }
            else
            {
                // 创建载入界面
                var task = new Thread(stop => { Watcher.Stop(); })
                {
                    Name = RegisteredThreadName.StopWatching.ToString()
                };
                MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task));
                var loading = new Loading("正在停止,请稍候", task);
                loading.ShowDialog();

                // 等待结果
                new WaitTimeoutChecker(30000).ThreadSleep(500, () => {
                    var msg = MessagePipe.GetNextOutMessage(task);
                    switch (msg)
                    {
                    case Message.NoAvailableMessage:
                        return(true);

                    case Message.TaskOut:
                        return(false);

                    default:
                        throw new Exception($"无效的消息类型:{msg}");
                    }
                });

                // 模块已停止
                MessagePipe.ClearAllMessage(task);
                ConnectionListUpdateTimer.Stop();
                new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled);
                ConnectionList.Rows.Clear();
                MessageBox.Show("监视工作已停止。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                开始监视ToolStripMenuItem.Text = "开始监视";
            }
        }