第一次迭代函数后,回调/事件处理程序停止工作

时间:2015-02-13 13:25:18

标签: c# callback

首先,对于我对术语的错误/错误使用表示歉意,因为我实际上并不是程序员,所有这些编程对我来说都是新的。

我有一个不断检查bool值的循环:

class Program
{
    static cliparser connectionstatus = new cliparser();

    static void Main(string[] args)
    {
        Console.WriteLine("MAIN()__ START");

        while (true)
        {
            Console.WriteLine("MAIN()__WHILE, USERS LOGGED IN");

            if (connectionstatus.Disconnected())
            {
                Console.WriteLine("MAIN()__VPN DISCONNECTED");
            }
            Console.WriteLine("MAIN()__SLEEP 3s START");
            Console.WriteLine("MAIN()__SLEEP 3s END" + Environment.NewLine);
        }
    }
}

然后还有另一段代码实际上检查了bool的值:

class cliparser
{
    private bool DisconnectedValue;

    static ProcessStartInfo vpncliStartInfo = new ProcessStartInfo()
    {
        FileName = Path.Combine(parent, "vpncli.exe"),
        RedirectStandardOutput = true,
        RedirectStandardError = false,
        UseShellExecute = false,
        CreateNoWindow = true,
        Arguments = "status"
    };

    private Process vpncliProcess = new Process() {StartInfo = vpncliStartInfo,};

    public cliparser()
    {
        vpncliProcess.ErrorDataReceived += vpncli_Error;            //init callbacks
        vpncliProcess.OutputDataReceived += vpncli_DataReceived;    //init callbacks
        vpncliProcess.EnableRaisingEvents = true;
    }

    public bool Disconnected()
    {
        Console.WriteLine("Disconnected()__BEGIN");
        Console.WriteLine("Disconnected()__DISCO-BOOL: " + DisconnectedValue.ToString());

        vpncliProcess.Start();
        vpncliProcess.BeginOutputReadLine();
        Console.WriteLine("Disconnected()__END");
        return DisconnectedValue;
    }

    private void vpncli_DataReceived(object sender, DataReceivedEventArgs e)
    {
        if (e.Data != null && e.Data.Contains("Disconnected"))
        {
            DisconnectedValue = true;
            Console.WriteLine("vpncli_DataReceived__disconnected, SET DISCO-BOOL TO: " + DisconnectedValue.ToString());
            vpncliProcess.CancelOutputRead();
            Console.WriteLine("vpncli_DataReceived__disconnected E: " + e.Data);
        }       
    }
}

方法断开连接()的第一次调用很顺利。

然后while循环第二次启动 Disconnected()方法。但现在“ vpncli_DataReceived ”回调不再起作用,并且不会引发事件。我不知道这是什么原因。

我知道“ vpncliProcess.BeginOutputReadLine()”流处于活动状态,因为在第三个循环期间,我得到一个例外,即 BeginOutputReadLine 操作已经启动(请注意我在回调中发出了 CancelOutPutRead()

长话短说:

为什么在第一个**断开连接()方法结束后回调无法正常工作?**

我在程序中添加了事件序列的日志,也许有助于更好地理解这个问题:

MAIN()__ START
MAIN()__WHILE, USERS LOGGED IN
Disconnected()__BEGIN
Disconnected()__DISCO-BOOL: False
vpncli_DataReceived__disconnected, SET DISCO-BOOL TO: True
vpncli_DataReceived__disconnected E:   >> state: Disconnected
Disconnected()__END
MAIN()__VPN DISCONNECTED
MAIN()__SLEEP 3s START
MAIN()__SLEEP 3s END

MAIN()__WHILE, USERS LOGGED IN
Disconnected()__BEGIN
Disconnected()__DISCO-BOOL: True
Disconnected()__END
MAIN()__VPN DISCONNECTED
MAIN()__SLEEP 3s START
MAIN()__SLEEP 3s END

MAIN()__WHILE, USERS LOGGED IN
Disconnected()__BEGIN
Disconnected()__DISCO-BOOL: True

有任何想法或建议吗?谢谢!

0 个答案:

没有答案