private void button1_Click(object sender, EventArgs e) { try { DisableControls(); var t = new Task(() => { BeginInvoke(new VoidMethod_Delegate(() => { var ip = IPAddress.Parse(comboBox1.Text); var eport = Convert.ToInt32(numericUpDown1.Value); var iport = Convert.ToInt32(numericUpDown2.Value); var description = textBox1.Text; if (TCP_checkBox.Checked || UDP_checkBox.Checked) { if (TCP_checkBox.Checked) { var client = new UPnPClient(ip, eport, iport, ProtocolType.Tcp, description); client.Add(); } if (UDP_checkBox.Checked) { var client = new UPnPClient(ip, eport, iport, ProtocolType.Udp, description); client.Add(); } } })); }); t.Start(); t.ContinueWith(task => { BeginInvoke(new VoidMethod_Delegate(() => { EnableControls(); RestartTimer(); })); }); } catch (Exception ex) { MessageBox.Show(ex.Message, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void remove_MenuItem_Click(object sender, EventArgs e) { try { var t = new Task(() => { StopTimer(); listView1.BeginInvoke(new VoidMethod_Delegate(() => { var items = listView1.SelectedItems; for (var i = 0; i < items.Count; ++i) { var eport = Convert.ToInt32(items[i].SubItems[1].Text); ProtocolType type; if (items[i].SubItems[2].Text == @"TCP") { type = ProtocolType.Tcp; } else if (items[i].SubItems[2].Text == @"UDP") { type = ProtocolType.Udp; } else { throw new Exception(); } UPnPClient.Remove(eport, type); } })); }); t.Start(); t.ContinueWith(task => { RestartTimer(); }); } catch (Exception ex) { MessageBox.Show(ex.Message, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private async void FlushList() { statusStrip1.Invoke(new VoidMethod_Delegate(() => { StatusLabel.Text = @"正在刷新列表..."; })); var mappings = new List <MappingInfo>(); await Task.Run(() => { var upnp = new UPnPClient(); mappings = upnp.Get().ToList(); }); listView1.BeginInvoke(new VoidMethod_Delegate(() => { listView1.Items.Clear(); foreach (var mapping in mappings) { listView1.Items.Add(new ListViewItem { Text = mapping.Description, SubItems = { new ListViewItem.ListViewSubItem { Text = mapping.ExternalPort.ToString() }, new ListViewItem.ListViewSubItem { Text = mapping.Protocol }, new ListViewItem.ListViewSubItem { Text = mapping.InternalPort.ToString() }, new ListViewItem.ListViewSubItem { Text = mapping.InternalClient }, new ListViewItem.ListViewSubItem { Text = mapping.ExternalIPAddress } } }); } })); statusStrip1.Invoke(new VoidMethod_Delegate(() => { StatusLabel.Text = @"列表刷新完成"; })); }