日期:2014-05-18 浏览次数:20882 次
private void ScanButton_Click(object sender, EventArgs e) { byte[] buffer = new byte[] { 0x55, 0x01, 0xAA }; var tes = new TimerState(); var listenDelegate = new TimerCallback(BeginListen); var timer = new System.Threading.Timer(listenDelegate, tes, 10, 1000); tes.Tmr = timer; }
private void BeginListen(object o) { var ts = (TimerState)o; ts.Counter++; if (ts.Counter == 101) { ts.Tmr.Dispose(); ts.Tmr = null; } byte[] data ; data = udpRec.Receive(ref iep); if (data.Length == 11 && data[0] == 0x55 && data[1] == 0x01 && data[10] == 0xaa) { string ip = data[2].ToString() + "." + data[3].ToString() + "." + data[4].ToString() + "." + data[5].ToString(); string netmask = data[6].ToString() + "." + data[7].ToString() + "." + data[8].ToString() + "." + data[9].ToString(); ListViewItem _item = new ListViewItem(); _item.SubItems[0].Text = ip; _item.SubItems.Add(netmask); UpdateListviewDelegate d_listview = new UpdateListviewDelegate(UpdateListview); listView1.Invoke(d_listview, _item); } UpdateProgressBarDelegate d_progressbar = new UpdateProgressBarDelegate(UpdateProgressBar); progressBar.Invoke(d_progressbar, ts.Counter); }
delegate void UpdateProgressBarDelegate(int value); private void UpdateProgressBar(int value) { progressBar.Value = value; } delegate void UpdateListviewDelegate(ListViewItem item); private void UpdateListview(ListViewItem item) { if (!isExist(item)) listView1.Items.Add(item); }