none
請問有關執行續及委派的用法 RRS feed

  • 問題

  • 各位前輩好,下方有小弟的畫面及程式,想請問前輩們當按下開始監控後程式只會跑一次就結束了,而我想讓程式不斷地執行,另外再執行的同時此視窗感覺是當掉的狀況,後來有想過用執行續搭配委派,參考了很多文件還是無法參透,可以給小弟一些提示開如何做嗎?

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net.NetworkInformation;
    using System.Threading;
    
    namespace Server_Monitor
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private void button3_Click(object sender, EventArgs e)
        {
          listBox1.Items.Clear();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
          listView1.Columns.Add("IP Address", 100);
          listView1.Columns.Add("Server Name", 100);
          listView1.Columns.Add("Status", 450);
        }
    
        public string PingIP(string IP_Adderss)
        {
          string serverStatus = "";
          Ping myPing = new Ping();
          PingReply pingreply = myPing.Send(IP_Adderss);
          if (pingreply.Status == System.Net.NetworkInformation.IPStatus.Success)
          {
            serverStatus = "主機連線正常!!";
          }
          else
          {
            serverStatus = "主機連線異常!!";
            DateTime myDateTime = DateTime.Now;
            serverStatus = IP_Adderss + " 於 " + myDateTime + " 斷線,請通知相關人員處理!!!";
            listBox1.Items.Add(serverStatus);
          }
          return serverStatus;
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          this.Close();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          StreamReader myStreamReader = new StreamReader("C:\\Users\\Justin\\Desktop\\Server_List.csv");
          int counter = 0;
          string line;
          
          while ((line = myStreamReader.ReadLine()) != null)
          {
            String[] strAay = line.Split(',');
            ListViewItem item01 = new ListViewItem();
            item01 = new ListViewItem(strAay[0]);
            item01.Text = strAay[0];
            item01.SubItems.Add(strAay[1]);
            strAay[2] = PingIP(strAay[0]);
            item01.SubItems.Add(strAay[2]);
            listView1.Items.Add(item01);
          }
          counter++;
        }
      }
    }
    
    

    2011年8月18日 上午 08:39

解答

所有回覆