2016-10-15 3 views
0

私はC#とUWPを初めて使っています。これは割り当てであり、厳しい締め切りを迎えています。Async別のフレームから戻って操作をブロックする操作を待ちます。

AMQP接続を作成し、一定のwhileループでAzure IOTハブからメッセージを受信することによって動作する以下のコードがあります。

問題が適切に割り当てられていますが、主な問題は、別のページにアクセスして戻ったときにGUIがテキストフィールドの更新を停止することです。

実際にはOnNavigatedToから更新されますが、Receive Messagesメソッドはメッセージの更新を停止しますが、出力ウィンドウでは、debug.writeがメッセージを受信して​​いることがわかります。

私は別のスレッドでwhileループを実行する必要があると思います。私はこれを下に行ったとは思いません。私はTask.Runなどを読み込み、さまざまな方法で試しましたが、

public MainPage() 
    { 
     this.InitializeComponent(); 
     if (receivedStarted != 1) 
      Receive(); 
    } 

    private async void Receive() 
    { 
     await ReceiveMessages("1"); 
     await ReceiveMessages("0"); 
    } 

    /// <summary> 
    /// On Navigated to function from other pages 
    /// </summary> 
    /// <param name="e"></param> 
    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     Counter.Text = parsedCounter; 
     TimeText.Text = "The last activity was at " + parsedTime; 
     Contact_1.Content = ContactAddPopUp.contact[0, 0]; 
     Contact_2.Content = ContactAddPopUp.contact[1, 0]; 
     Contact_3.Content = ContactAddPopUp.contact[2, 0]; 

     if (Settings.timeChanged == true) 
     { 
      if (DelayTimer != null) 
      { 
       DelayTimer.Cancel(); 
       Timer(); 
      } 
     } 
    } 
    /// <summary> 
    /// Receive messages from specified azure iothub on specified partition. The MessageManager parses the received message and displays it accordingly 
    /// </summary> 
    /// <param name="partition"></param> 
    /// <returns></returns> 
    public async Task ReceiveMessages(string partition) 
    {   
     DateTime offset; 
     offset = DateTime.UtcNow - TimeSpan.FromMinutes(1); 
     String primaryKey = "fghkfwhihelfihefjw;ojwef"; 
     String sharedAccessPolicy = "iothubowner"; 
     //String hubName = "RaspberryPirSensor"; 
     String deviceName = "PIRSensor"; 
     String eventHubEntity = "iothub-ehub-raspberryp-70680-20f0331ccb"; 

     string port = "iakugfdkjhkjhaflhlkhalkfhse.servicebus.windows.net"; 
     Address address = new Address(port, 5671, sharedAccessPolicy, primaryKey, "/", "amqps"); 
     Connection connection = await Connection.Factory.CreateAsync(address); 
     Session session = new Session(connection); 
     string totalMilliseconds = ((long)(offset - new DateTime(StartOfEpoch, DateTimeKind.Utc)).TotalMilliseconds).ToString(); 
     Map filters = new Map(); 
     filters.Add(new Amqp.Types.Symbol("apache.org:selector-filter:string"), 
            new DescribedValue(
             new Amqp.Types.Symbol("apache.org:selector-filter:string"), 
             "amqp.annotation.x-opt-enqueuedtimeutc > " + totalMilliseconds + "")); 
     ReceiverLink receiver = new ReceiverLink(session, 
      "my-receiver", 
      new global::Amqp.Framing.Source() 
      { 
       Address = 
      eventHubEntity + "/ConsumerGroups/$Default/Partitions/" + partition, 
       FilterSet = filters 
      }, null); 

     Amqp.Types.Symbol deviceIdKey = new Amqp.Types.Symbol("iothub-connection-device-id"); 
     string deviceId = deviceName; 
     while (true) 
     { 
      if (timerCreated == false) 
       Timer(); 
      receivedStarted = 1; 
      Amqp.Message m = await receiver.ReceiveAsync(10000); 
      if (m != null) 
      { 
       var id = m.MessageAnnotations.Map[deviceIdKey].ToString(); 
       if (id == deviceId) 
       { 
        Data data = (Data)m.BodySection; 
        string msg = System.Text.Encoding.UTF8.GetString(data.Binary, 0, data.Binary.Length); 
        bool isValid = ValidateMessage(msg); 

        if (isValid) 
        { 
         receiver.Accept(m); 
         Counter.Text = parsedCounter; 
         Debug.Write("Receiving Message " + parsedCounter); 
         TimeText.Text = "The last activity was at " + parsedTime; 

         //Connection String 
         if (connection != null) 
          ConnectedTextBox.Text = "CONNECTED"; 
         else 
          ConnectedTextBox.Text = "DISCONNECTED"; 

         if (DelayTimer != null) 
          DelayTimer.Cancel(); 
        } 
        else 
        { 
         receiver.Release(m); 
        } 
       } 
      } 
     } 
    } 
+0

あなたは少し具体的に従うことができないので、あなたの実行は 'ReceiveMessages'メソッドで中断しますか? – inan

+0

こんにちは、それは見つけることを続ける、uiなどを使用することができます。それが動作する方法は、IOTハブから受信したメッセージがテキストボックスを更新する、それはラズベリーパイに接続されたpirセンサーです。あなたが他のページ、すなわち設定にナビゲートしてメインページに戻ると、テキストボックスの更新は停止しますが、メッセージとコードは継続して実行されます。ループでブレークポイントを実行すると、TimeText.Text = fineで実行されますが、更新されませんが、まだ残りのUIを使用できます。 –

+0

okだから、whileループブロックをasync/awaitメソッドに移動してUIをブロックしないようにしたいのですが、それを試しましたか? – inan

答えて

0

は私が実際に使用してこれを解決 - this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled。 - MainPageのコンストラクタにあります。

私がナビゲートするたびにページを再初期化していました。待っていたタスクとの同期が緩んでいたようです。

+0

ニースあなたはそれを解決することができました – inan

+0

とにかく助けてくれてありがとう –

関連する問題