/// <summary> /// 运行程序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, RoutedEventArgs e) { if (button1.Content.ToString() == "停止运行") { foreach (var item in states) { //释放内存 ((Timer)item.UserToKen).Dispose(); if (!item.CurrentProcess.HasExited) { item.CurrentProcess.Kill(); } } try { //移除组播 udp.DropMulticastGroup(System.Diagnostics.UDPGroup.group.ep.Address); udp.Close(); states.Clear(); task.Dispose(); } catch (Exception ex) { } button1.Content = "开始运行"; } else { //当前已经采集的索引 if (System.IO.File.Exists(ProgressFileName)) { List <string> arr = System.IO.File.ReadLines(ProgressFileName).ToList(); foreach (var item in Strs) { //得到当前项的状态如果包含则将当前项设置为 true 表示已经处理过了 if (arr.Contains(item.Key)) { item.Value.IsCollectioned = true; } } } //初始化采集进程 System.Linq.Enumerable.Range(0, MaxPorcess).ToList().ForEach((p) => { System.Threading.Thread.Sleep(40); AddStates(); }); //开始监听 task = System.Threading.Tasks.Task.Factory.StartNew(() => { udp = new System.Net.Sockets.UdpClient(System.Diagnostics.UDPGroup.group.ep.Port); udp.JoinMulticastGroup(System.Diagnostics.UDPGroup.group.ep.Address); udp.BeginReceive(AsyncCallback, udp); }); button1.Content = "停止运行"; } }
/// <summary> /// stop listen /// </summary> public void StopListen() { if (IsListening) { UdpClient.DropMulticastGroup(GroupIP); thUDPListener.Abort(); UdpClient.Close(); IsListening = false; } }
public void Close() { try { socket.DropMulticastGroup((System.Net.IPAddress)group); socket.Close(); } catch (System.IO.IOException) { } }