close

在測試即將上線的APP後台發現,某個方法會造成SQL插入資料重複,並且影響到登入的環節,所以根據評估後打算採用另外開 Thread,還好目標方法不需要回傳資料給前台操作,比較像是藏在某個環節上的補正資料。 

 

      ThreadProductionDate(cus.SimpleFactoryId);
      return datalist;///在方法回傳前,呼叫撰寫的方法啟動

==========================================================

程式碼片段如下

    void ThreadProductionDate(int SimpleFactoryId) {

            ParameterizedThreadStart threadDelegate = new ParameterizedThreadStart(ProductionDate);
            Thread thread = new Thread(threadDelegate);
            thread.Start(new DAL.ViewModel.ThreadProductionDate
            {
                SimpleFactoryId = SimpleFactoryId,
            });
        }

ProductionDate為主要方法的主體

在Thread NEW 後塞上去
並把方法再啟動的時候 傳入宣告的ViewModel

Start開啟

public partial class ThreadProductionDate
{
        public int SimpleFactoryId { get; set; }
}

ThreadProductionDate透過宣告一個ViewModel準備承接傳入的變數

private void ProductionDate(object obj)
{
            Go2elife.App.DAL.ViewModel.ThreadProductionDate myClass = obj as Go2elife.App.DAL.ViewModel.ThreadProductionDate;
            var SimpleFactoryId = myClass.SimpleFactoryId;

====================================以下為方法的執行 

}

===========================================================
切忌留意不同線程有同步與非同步問題

過程中有遇到  _entitiesWrite.SaveChanges(); 無法變更的問題,原因就是資料上變更有所衝突

 

 

 

 

 

 

 

 

 

 

 

 

 

arrow
arrow
    文章標籤
    C# 程式設計
    全站熱搜

    萌新-工程師 發表在 痞客邦 留言(0) 人氣()