這裏是我的問題:如何協調3個線程執行(UI與DevExpress的完成)
我有了兩個方法服務器上的WCF服務:
Synchronize() and GetExecutionState().
我也有一個客戶端,就是調用這些操作。
Synchronize()執行很多事情,並在幾個步驟中,所以在每一步我更新本地(在wcf)變量,我通過GetExecutionState()操作發佈此變量操作可能會失敗,可以採取永久執行等。
所以,當我耗在客戶端上此服務,請說somebutton_click()事件, 我想發生什麼事是這樣的:
- 顯示無限進度條(主線程,UI);
- 啓動一個線程來運行Synchronize();
- 啓動另一個線程,每隔x分鐘讀取一次GetExecutionState(),並在連接失敗(這是我需要這最後一個線程的主要原因)時取消整個事情。
我不知道很多關於線程的知識,但到目前爲止我已經實現了1和2。有人可以幫助我3嗎?
我使用devExpress,這裏是我的相關代碼。
服務器部分:
public class SyncServerService :ISyncServer {
protected CsNo Node;
protected SyncState State;
public SyncServerService() {
State = SyncState.None;
Node = null;
}
public SyncState OperationState() {
return State;
}
public void PutComputerName(string value) {
var man = new CsNoManager();
Node = man.GetByMachineName(value);
}
public bool CanSync() {
var man = new ViewSyncLogManager();
var log = man.GetByMachineName(Node.MachineName);
return !log[0].IsInSync;
}
public CommandExecutionResponse Synchronize() {
CommandExecutionResponse res = null;
var logManager = new CsLogSyncManager();
var log = logManager.GetByNode(Node.IDNo);
State=SyncState.Syncing;
//step 1
State = SyncState.State2;
//...step n
State = SyncState.SomeOtherState;
//somewhere along the path create the res object
return res;
}
}
我讀的地方,與WCF,我可以調用操作都同步和異步,所以我不認爲我必須用我的有關要求,服務器部分一塌糊塗。
客戶端部分:
點擊按鈕用於啓動過程:
私人無效cmdSync_Click(對象發件人,EventArgs的){
pgbSync.Properties.Stopped = false;
backgroundWorker1.RunWorkerAsync();
}
pgbSync是鍵入MarqueeProgressBar,具有無限循環的DevExpress進度條
backgroundWorker1的類型是System.ComponentModel.BackgroundWorker ...據說在後臺運行任務。
它的起點和終點的方法是:
START:
private void StartSync(object sender, DoWorkEventArgs e) {
try {
//setting up wcf link properties
var manager = new CsConfiguracaoManager();
var address = manager.SyncAppServiceAddress();
var binding = new NetTcpBinding {
Security = new NetTcpSecurity() { Mode = SecurityMode.None },
CloseTimeout = new TimeSpan(0, 0, 30, 0, 0),
OpenTimeout = new TimeSpan(0, 0, 30, 0, 0),
ReceiveTimeout = new TimeSpan(0, 0, 30, 0, 0),
SendTimeout = new TimeSpan(0, 0, 30, 0, 0)
};
var factory = new ChannelFactory<ISyncServer>(binding, new EndpointAddress(address));
var proxy = factory.CreateChannel();
proxy.PutComputerName(PcName);
//checking if i can sync first
if (proxy.CanSync() == true) {
ExecutionResponse = proxy.Sync();
}
else {
//set up messages to show errors
}
}
catch (DataException dataErr) {
//set up appropriate messages
}
catch (EndpointNotFoundException err) {
//set up appropriate messages
}
catch (Exception masterErr) {
//set up appropriate messages
}
}
FINISH:
私人無效FinishSync(對象發件人,RunWorkerCompletedEventArgs E){
pgbSync.Properties.Stopped = true;
//process ExecutionResponse object from FinishSync
}
這一切運行,但如果我斷開網絡後,進程已經開始(我們a重新預測大量的通信問題),該過程只會在服務設置的30分鐘過後纔會引發異常。
這就是爲什麼我想介紹第三步,我檢查OperationState的輸出每x分鐘。
如果我不能讀取它,我放棄操作,如果它有一個null或err狀態,我也停止。
任何想法?
我會嘗試Timers.Timer並回到你身邊 – sergio