2011-08-18 96 views
1

我想要這個方法被編出來,所以我可以設置一個計時器而不是等待它完成。這是對服務的呼叫。c中的線程問題#

private static void callValueEng(ValueEngineService.Contracts.ValueEngServiceParams param) 
{ 
    using (WCFServiceChannelFactory<IValueEngineService> client = 
       new WCFServiceChannelFactory<IValueEngineService>(
        Repository.Instance.GetWCFServiceUri(typeof(IValueEngineService)))) 
    { 
     client.Call(x => x.ValueManyTransactionsWithOldEngines(translatedParams)); 
    } 
} 

我試着穿出來像這樣:

System.Threading.Thread newThread; 
//RestartValueEngineService(); 

List<TransactionInfo> currentIdsForValuation = ((counter + 7000) <= allIds.Count) 
           ? allIds.GetRange(counter, 7000) 
           : allIds.GetRange(counter, allIds.Count - counter); 
translatedParams.tranquoteIds = currentIdsForValuation; 

// thread this out 
newThread = new System.Threading.Thread(callValueEng(translatedParams)); 

但它說「的最佳重載的比賽有一些無效的參數。」我究竟做錯了什麼?

回答

3

嘗試:

var invoker = new Action<ValueEngineService.Contracts.ValueEngServiceParams>(callValueEng); 
invoker.BeginInvoke(translatedParams, null, null); 

這將異步調用你的方法。

+0

如何傳遞它的參數? – slandau

+0

修正了它,犯了一個錯誤(忽略了參數) – Bas

+0

BeginIvoke不會創建一個新的線程 – sll

0

System.Threading.Thread構造函數接受一個委託作爲它的參數。 嘗試

newThread = new System.Threading.Thread(new ParameterizedThreadStart(callValueEng)); 
newThread.start(translatedParams); 
+1

這不會工作,因爲我猜我的方法是不正確的。 – slandau

+0

問題是你的方法是靜態的。線程構造函數接受一個委託映射到帶有簽名void方法(obj params)的方法。由於它是靜態的,請使用發佈的調用者答案。 – tafoo85

+0

@ tafoo85:您可以將具有正確簽名的靜態方法傳遞給Thread.Start(...),問題是OP的方法有錯誤的簽名 – sll

0

有一個在System.Threading.Thread類沒有構造函數帶你已通過類型的代表。您只能傳遞threadstart或paramererizedthreadstart類型的委託。

0

回答你的問題「我做錯了什麼?」 - 你試圖通過與簽名方法,該方法不受ParameterizedThreadStart代表支持(see here

簽名應該是

void ParameterizedThreadStart(Object obj) 

,而不是

void ParameterizedThreadStart(
        ValueEngineService.Contracts.ValueEngServiceParams param) 
0

您在使用.NET 4?如果是這樣,你可以這樣做:

Task.Factory.StartNew(() => callValueEng(translatedParams)); 

它會在新線程上運行你的代碼。如果你需要做的事情,當它完成時,那麼你也可以很容易地做到這一點:

Task.Factory.StartNew(() => callValueEng(translatedParams)) 
    .ContinueWith(() => /* Some Other Code */);