瀏覽代碼

1. sellerInvoiceFailureWatcher 增加 LastAccess filter
2. 所有發票處理完成並引發InvoiceCompleted之後,將所有FileSystemWatcher 關閉

Snoopy 2 年之前
父節點
當前提交
71e303eb27
共有 2 個文件被更改,包括 29 次插入4 次删除
  1. 27 2
      TCCInvoice/InvoiceGenerator.cs
  2. 2 2
      TCCInvoice/Program.cs

+ 27 - 2
TCCInvoice/InvoiceGenerator.cs

@@ -229,7 +229,7 @@ namespace TCCInvoice
             {
                 Path = SELLER_INVOICE_FAILURE_PATH,
                 Filter = "*.xml",
-                NotifyFilter = NotifyFilters.LastWrite,
+                NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess,
                 IncludeSubdirectories = false,
                 EnableRaisingEvents = true,
             };
@@ -285,6 +285,7 @@ namespace TCCInvoice
                 if (count == totalQuantity)
                 {
                     InvoiceCompleted?.Invoke(invoiceResponseItemList);
+                    TurnOffFileSystemWatcher();
                 }
             }
         }
@@ -317,6 +318,7 @@ namespace TCCInvoice
                 if (count == totalQuantity)
                 {
                     InvoiceCompleted?.Invoke(invoiceResponseItemList);
+                    TurnOffFileSystemWatcher();
                 }
             }
             else // 上傳失敗
@@ -330,6 +332,7 @@ namespace TCCInvoice
                 if (count == totalQuantity)
                 {
                     InvoiceCompleted?.Invoke(invoiceResponseItemList);
+                    TurnOffFileSystemWatcher();
                 }
             }
         }
@@ -349,7 +352,7 @@ namespace TCCInvoice
                 await Task.Delay(500);
                 if (!File.Exists(responsePath)) // 可能是網優SQL連線失敗,需要嘗試重新上傳
                 {
-                    await Task.Delay(180000);
+                    await Task.Delay(30000);
                     File.Copy(failurePath, sellerinvoicePath);
                 }
             });
@@ -521,6 +524,28 @@ namespace TCCInvoice
             Random rdm = new Random(Guid.NewGuid().GetHashCode());
             return rdm.Next(min, max).ToString().PadLeft(4, '0');
         }
+
+        private void TurnOffFileSystemWatcher()
+        {
+            if (preInvoiceResponseWatcher != null)
+            {
+                preInvoiceResponseWatcher.EnableRaisingEvents = false;
+                preInvoiceResponseWatcher.Dispose();
+                preInvoiceResponseWatcher = null;
+            }
+            if (sellerInvoiceResponseWatcher != null)
+            {
+                sellerInvoiceResponseWatcher.EnableRaisingEvents = false;
+                sellerInvoiceResponseWatcher.Dispose();
+                sellerInvoiceResponseWatcher = null;
+            }
+            if (sellerInvoiceFailureWatcher != null)
+            {
+                sellerInvoiceFailureWatcher.EnableRaisingEvents = false;
+                sellerInvoiceFailureWatcher.Dispose();
+                sellerInvoiceFailureWatcher = null;
+            }
+        }
         #endregion
 
         #region Nested Classes

+ 2 - 2
TCCInvoice/Program.cs

@@ -17,11 +17,11 @@ namespace TCCInvoice
             myInvoice.InvoiceCompleted += MyInvoice_InvoiceCompleted;
             
             // 填具待開立發票的相關資料(Preinvoice)
-            myInvoice.DataNumber = "202204183000";
+            myInvoice.DataNumber = "202206133009";
             myInvoice.DataDate = DateTime.Today;
             myInvoice.BuyerId = null; // 一般消費者
             myInvoice.TotalAmount = 524;
-            myInvoice.CarrierId1 = null;
+            myInvoice.CarrierId1 = "XXXXXXXX";
             myInvoice.MainRemark = "交易訂單號:1234567890";
             myInvoice.DonateMark = 0;
             myInvoice.ContactEmail = null;