[問題] NDDE server/client寫在同一支程式

作者: zaqimon (dream)   2020-04-07 20:29:44
我是C#初學還沒什麼完整的概念
以前有寫過C所以對程式還算有概念
我想用NDDE寫一支類似DDE proxy的中介程式
所以我的程式同時需要當DdeServer跟DdeClient
把DdeServer.OnRequest直接丟給DdeClient.Request
但事情沒那麼簡單
DdeServer.OnRequest只要超過一個item就很容易exception
因為OnRequest會有reentrance的問題
我的DdeServer跟DdeClient已經使用不同DdeContext去new
這樣DdeServer跟DdeClient才能跑在不同ManagedThreadId以避免衝突
但依然還是無法解決DdeServer.OnRequest reentrance的問題
只要去call了ddeClient.Request就會發生reentrance
換成Sleep就不會發生reentrance
以下是我的DdeServer.OnRequest程式碼
請問有辦法解決這個reentrace的問題嗎?
有測試過lock鎖不住
測試過semaphore直接deadlock
謝謝
protected override RequestResult OnRequest(DdeConversation conversation,
string item, int format)
{
if (format == 1)
{
byte[] re;
Console.WriteLine(">>>");
//Thread.Sleep(1000); // no reentrance
//re = ddeClient.Request(item, 1, 1000); // reentrance
re = (byte[])ddeClient.Context.Invoke(new Func<byte[]>(() =>
ddeClient.Request(item, 1, 1000)), null); // still reentrance
Console.WriteLine("<<<");
return new RequestResult(re);
}
return RequestResult.NotProcessed;
}
作者: Litfal (Litfal)   2020-04-07 22:31:00
你是要做到reentrace還是thread safe? 這裡應該很少在討論reentrant的,你也應該不是遇到這個問題吧
作者: ssccg (23)   2020-04-08 09:29:00
不懂為什麼這種程式會想要做到一進一出? 除非預期只有一個client,不然這樣效能不會很差嗎?通常只會想做thread safe,結果對就好執行順序不重要吧至於會有exception那應該是要去研究到底為什麼有exception
作者: Litfal (Litfal)   2020-04-08 19:39:00
高階語言不在意reentrance,你講這個字還沒什麼人懂。很多人連interrupt都不知道也能寫一手好程式。你的問題應該是thread safe你用lock擋不住也不正常,先確定寫法吧

Links booklink

Contact Us: admin [ a t ] ucptt.com