2017-08-07 56 views
-1

我有用於讀取QR碼的while(true)循環。我想在讀取有效QR碼時暫停循環,然後執行某些操作(在數據庫中插入值),然後繼續循環讀取其他QR碼。我用休息和繼續,但打破走出循環,繼續跳到我的代碼,我不想them.I'm與事先C#和SQL server.Thanks工作如何暫停循環一段時間,然後再繼續使用C#?


編輯:我不在閱讀Qr代碼時沒有任何問題我的代碼工作正常,我只需要知道如何暫停循環並執行某些操作,然後繼續循環。下面是代碼。我可以停止,同時用if語句

while ((int)(1) != 0) 
       { 
        ho_Image.Dispose(); 
        HOperatorSet.GrabImage(out ho_Image, hv_AcqHandle); 

         HOperatorSet.DispObj(ho_Image, HWindow); 
         operation(ho_Image, hv_Type1, hv_Type2, hv_Num1ecc, hv_Num2qr, hv_Qr, hv_Ecc, 
         HWindow, hv_Timeoutqr, hv_Timeoutecc, out hv_DecodedDataStrings1, 
         out hv_DecodedDataStrings2, out hv_DataCodeHandle1, out hv_DataCodeHandle2, 
         out hv_foundecc, out hv_foundqr); 

         hv_Qr = 0; 
         hv_Ecc = 0; 
         //MessageBox.Show(" QR NuM : " + this.QRNumber); 
         //MessageBox.Show("ECC Number : " + this.ECCNumber); 

         if (this.QRNumber == QRNum && this.ECCNumber == ECCNum) 
         { 
          foreach (string qr in this.QRValue) 
          { 
           MessageBox.Show(" QR value : " + qr); 
          } 

          foreach (string ecc in this.ECCValue) 
          { 
           MessageBox.Show("ECC value : " + ecc); 
          } 


         } 
       } 
+1

添加異步任務,並等待數據保存到數據庫中。 – raichiks

+2

你看看「yield」用法嗎,我想它可以幫你 –

+2

如果我錯了,請糾正我,但如果你的代碼讀取代碼不是異步的,那麼其他命令將不會被執行,直到完成。 。 – Jelman

回答

0

我想我失去了一些東西,但是這應該工作:

​​
0

是不是真的如暫停while循環的事情。一個循環不能暫停,while循環可以被破壞,你可以退出循環。您可以將整個程序放在睡眠狀態,這會「暫停」循環,實際上是整個程序。當滿足條件時,您也可以將循環陷入到一個塊中,這也會「暫停」循環繼續執行任何操作,並輸入該塊完成它必須完成的操作,然後再次循環。

爲了捕獲它,你可以使用一個標誌,並且只要你的條件滿足,你將該標誌設置爲true,並檢查該標誌是否爲真,然後執行數據庫插入或其他操作。 最後在循環結束時將標誌設置爲false。

這裏是一個非常簡單的示例代碼:

bool read = false; 
while(true) 
{ 
    //Start reading your QR here or do whatever you want to do; However, make sure that the variable 'read' is set to true when you read the code 
    ReadQR(); 

    //Now check if a code is read 
    if(read) 
    { 
      //Do whatever you want to do here if the code was read such as insertion to database, this technically will pause the loop and finish what's in this block then continue on with the loop, even though I highly recommend breaking your code into methods; for instance don't do the insertion logic here instead put the logic in a method and call it here. 

      InsertQRCodeToDb(code); 

      //Set the controller variable back to false 
      read = false; 
    } 
} 

附:出於好奇,你爲什麼要投1到int?另外爲什麼你的條件(int)1!= 0? 難道你不能只是把你的條件,如while(true)嗎?或者(1)不是很簡單,不需要鑄造嗎?

相關問題