0

私の状況を描いてみましょう:

スレッドを作成する WCF サーバーがあります。そのスレッドはアセンブリを実行し、それを呼び出しましょうABC.exe。これABC.exeはこれを行います:

    static void Main(string[] args)
    {
        objClientBase.OnHandshakeCompleted += new EventHandler(objClientBase_OnHandshakeCompleted);
        objClientBase.OnShutdownInitiated += new EventHandler(objClientBase_OnShutdownInitiated);
        objClientBase.Connect();
        objOEEMon = new Main();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }

どこConnectで:

        objClientThread = new Thread(start) { IsBackground = true };
        objClientThread.Start();

start

    /// <summary>
    /// Starts the client program.
    /// </summary>
    private void start()
    {
            //We Open the proxy to let connections happen
            objProxy.Open();
            if (performHandshake())
            {
                IsConnected = true;
                DelayedShutdownBool = false;
                //While connected, the thread keeps the client alive
                while (IsConnected)
                {
                    System.Threading.Thread.Sleep(500);
                    if (DelayedShutdownBool)
                    {
                        System.Threading.Thread.Sleep(500);
                        objProxy.Close();
                        objConfiguration = null;
                        IsConnected = false;
                    }
                }
            }
    }

これABC.exeは、WCF を使用してサーバーに接続するクライアントです。

そのため、 s (またはその他のもの)Sleep(Infinite)を使用する代わりに、はスレッドの終了について通知を受け、実行も終了します。しかし、スレッドを作成するインスタンスの関数を呼び出しているため、通知を受け取る方法がわかりません。manualResetEventMain()ConnectMain

私が望んでいないのは、while(condition) sleep

4

3 に答える 3

1

私のせいです...現在のクラスの代わりにクラスの新しいインスタンスを渡していたため、コールバックが別のインスタンスに対して行われたため、変更された変数は正しいものではありませんでした。

今は完璧に動作します。答えてくれてありがとう、彼らは私がエラーがどこにあったかを考えるためにシーケンスについて考えるのを助けました.

于 2013-08-16T19:19:20.897 に答える
0

Task1 つの解決策は、明示的に作成する代わりにクラスを使用することです。Thread

 //here instead of creating a Thread, create a Task and return it to the caller
 objClientThread = new Thread(start) { IsBackground = true };
 objClientThread.Start();

それからあなたはすることができます

 Task task = objClientBase.Connect();
 task.Wait();
于 2013-08-16T12:27:09.107 に答える
0

手動リセット イベントを使用する場合は、次のようにして、新しいリセット イベントを宣言します。

private static ManualResetEvent finished = new ManualResetEvent(false);

イベントを待ちます:

        //We Open the proxy to let connections happen
        objProxy.Open();
        if (performHandshake())
        {
            IsConnected = true;
            DelayedShutdownBool = false;
            //While connected, the thread keeps the client alive
            finished.WaitOne();
            if (DelayedShutdownBool)
            {
                System.Threading.Thread.Sleep(500);
                objProxy.Close();
                objConfiguration = null;
                IsConnected = false;
            }
        }

そして、objClientBase_OnShutdownInitiated で:

finished.Set();
于 2013-08-16T12:42:53.483 に答える