面对在C#中调用委托的问题

时间:2018-10-07 00:26:49

标签: c# delegates

我面临调用委托的问题。问题是,当我调用server1_d_u()函数时,它会在调用委托时陷入无限循环。

完整代码如下:

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 Nadeem_Library;
using System.Threading;
using System.Threading.Tasks;

namespace Live_Traffic_Monitor
{
    public partial class Live_Traffic : Form
    {

    public Live_Traffic()
    {
    InitializeComponent();
    }

    public class A
    {
    public static string server1Inapsuccessful = "N/A";
    public static string server1Inapfailure = "N/A";
    public static string server1Inaptime = "N/A";
    public static string server1Inaptps = "N/A";
    public static string server1diatps = "N/A";
    public static string server1diafirst = "N/A";
    public static string server1Diasuccessful = "N/A";
    public static string server1Diafailure = "N/A";
    public static string server1Diatime = "N/A";
    public static string server1EOCNsuccessful = "N/A";
    public static string server1EOCNfailure = "N/A";
    public static string server1EOCNtime = "N/A";
    public static string server1EOCNtps = "N/A";

    public static string fa0 = "", fa1 = "", fa2 = "", fa3 = "", fa4 = "", fa5 = "", fa6 = "";
    public static DateTime now;
    }

    private delegate void dserver1_traffic();

    Thread server1thread;

    int nRowIndex = 0;
    int rowindex = 0;

    private void server1_d_u()
    {
    if (this.InvokeRequired)
    {
    this.Invoke(new dserver1_traffic(server1_d_u));
    }
    else
    {
    Health_Checks.SelectedTab = tabSERVER1;
    nRowIndex = DGV_SERVER1.RowCount - 1;
    DGV_SERVER1.Rows.Add(A.fa2.ToUpper(), A.server1Inaptime, A.server1Inapsuccessful, A.server1Inapfailure, A.server1Diatime, A.server1diatps, A.server1Diafailure, A.server1EOCNtime, A.server1EOCNsuccessful, A.server1EOCNfailure);
    rowindex = DGV_SERVER1.CurrentCell.RowIndex;
    DGV_SERVER1.Update();
    DGV_SERVER1.FirstDisplayedScrollingRowIndex = nRowIndex;
    MessageBox.Show(A.fa2);
    }

    }

    private void button_Start_Click(object sender, EventArgs e)
    {
    string line;
    while (true)
    {
    System.IO.StreamReader file = new System.IO.StreamReader("C:\\SERVER1_Stats\\Traffic.txt");
    while ((line = file.ReadLine()) != null)
    {

    string[] a = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
    A.fa0 = a[0];
    A.fa1 = a[1];
    A.fa2 = a[2];
    A.fa3 = a[3];
    A.fa4 = a[4];
    A.fa5 = a[5];
    A.fa6 = a[6];

    server1thread = new Thread(() =>

    {
    server1_traffic();
    Action action = new Action(server1_d_u);
    this.BeginInvoke(action);
    });
    server1thread.Start();
    A.server1Inapsuccessful = "N/A";
    A.server1Inapfailure = "N/A";
    A.server1Inaptime = "N/A";
    A.server1Inaptps = "N/A";
    A.server1diatps = "N/A";
    A.server1diafirst = "N/A";
    A.server1Diasuccessful = "N/A";
    A.server1Diafailure = "N/A";
    A.server1Diatime = "N/A";
    A.server1EOCNsuccessful = "N/A";
    A.server1EOCNfailure = "N/A";
    A.server1EOCNtime = "N/A";
    A.server1EOCNtps = "N/A";

    A.fa0 = "";
    A.fa1 = "";
    A.fa2 = "";
    A.fa3 = "";
    A.fa4 = "";
    A.fa5 = "";
    }
    }
    file.Close();
    DGV_SERVER1.Rows.Clear();

    }



    }
    public void server1_traffic()
    {
    string AB = A.fa2.Substring(A.fa2.Length-1, 1).ToUpper();
    string sserver1_tra = Nadeem_Library.Class1.server1_traffic(A.fa0, A.fa1, A.fa4, A.fa5, AB);
    string[] a = sserver1_tra.Split(new string[] { "\n", "\r","/" }, StringSplitOptions.RemoveEmptyEntries);
    if (a.Length > 10)
    {
    A.server1Inaptime = a[0];
    A.server1Inapsuccessful = a[1];
    A.server1Inapfailure = a[2];
    A.server1Inaptps = a[3];

    A.server1Diatime = a[4];
    A.server1diatps = a[5];
    A.server1diafirst = a[6];
    A.server1Diafailure = a[7];

    A.server1EOCNtime = a[8];
    A.server1EOCNsuccessful = a[9];
    A.server1EOCNfailure = a[10];
    A.server1EOCNtps = a[11];

    }
    }
    }
}

已放弃完整代码。该代码在不使用线程的情况下工作正常,但是我的应用程序在代码运行时没有响应。为了解决这个问题,我添加了线程,但是如果我使用thread.join();然后代码仍然可以正常运行,但应用没有响应。为了解决这个问题,我也使用委托动作,但失败了。在这方面请提供帮助 您能帮我解决问题吗?

0 个答案:

没有答案