質問

My code works perfectly when it is an independent form. I insert it in a mdi container as a child form, it compiles and runs well but when i close the form it shows an error "Input string was not in a correct format." on the following code:

    int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
    int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;

the following is the code of the Bandwidth monitor, which is supposed to be the child.

  public partial class FrmBMon : Form
          {


    private const double timerUpdate = 1000;
    private NetworkInterface[] nicArr;
    private Timer timer;

    public FrmBMon()
    {
        InitializeComponent();
        InitializeNetworkInterface();
        InitializeTimer();
    }

    private void InitializeNetworkInterface()
    {
        // Grab all local interfaces to this computer
        nicArr = NetworkInterface.GetAllNetworkInterfaces();

        // Add each interface name to the combo box
        for (int i = 0; i < nicArr.Length; i++)
            cmbInterface.Items.Add(nicArr[i].Name);

        // Change the initial selection to the first interface
        cmbInterface.SelectedIndex = 0;
    }

    private void InitializeTimer()
    {
        timer = new Timer();
        timer.Interval = (int)timerUpdate;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
    }


    private void UpdateNetworkInterface()
    {
        // Grab NetworkInterface object that describes the current interface
        NetworkInterface nic = nicArr[cmbInterface.SelectedIndex];

        // Grab the stats for that interface
        IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();

        // Calculate the speed of bytes going in and out

        int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text)) / 1024;
        int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text)) / 1024;

        // Update the labels
        lblSpeed.Text = nic.Speed.ToString();
        lblInterfaceType.Text = nic.NetworkInterfaceType.ToString();
        lblSpeed.Text = nic.Speed.ToString();
        lblBytesReceived.Text = interfaceStats.BytesReceived.ToString();
        lblBytesSent.Text = interfaceStats.BytesSent.ToString();
        lblUpload.Text = bytesSentSpeed.ToString() + " KB/s";
        lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s";

    }

    /// <summary>
    /// The Timer event for each Tick (second) to update the UI
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void timer_Tick(object sender, EventArgs e)
    {
        UpdateNetworkInterface();
    }
    private void btn_Click(object sender, EventArgs e)
    {
        this.Close();


    }

    private void btnCal_Click(object sender, EventArgs e)
    {
        string url = @"http://www.microsoft.com/downloads/info.aspx?na=41&srcfamilyid=92ced922-d505-457a-8c9c-84036160639f&srcdisplaylang=en&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f2%2f9%2f6%2f296AAFA4-669A-46FE-9509-93753F7B0F46%2fVS-KB-Brochure-CSharp-Letter-HiRez.pdf";
        System.Net.WebClient client = new System.Net.WebClient();
client.DownloadFileAsync(new Uri(url), Path.GetTempFileName());
    }


}

here is the code i inserted in the main parent form.

   private void bandwidthMonitorToolStripMenuItem_Click(object sender, EventArgs e)
    {
        FrmBMon fbm = new FrmBMon();
        fbm.MdiParent = this;
        fbm.Show();
    }
役に立ちましたか?

解決

You should Stop your Timer before closing the form, which will ensure that there's no Tick afterwards. That said, you shouldn't be parsing the Text of a Label anyway. You should be using whatever data you used to populate the Label in the first place. Use a Double variable to store the value and then display that in the Label.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top