Как использовать NDDE Client с несколькими темами
Я занимаюсь разработкой приложения.NET, и я впервые работаю с DDE.
Таким образом, в этом приложении пользователь может добавлять / редактировать конфигурацию DDE. Они могут поставить тему т.е. [Book1.xls]Sheet1
а также пункт т.е. R1C1
, После сохранения это приложение должно иметь возможность инициализировать клиент DDE, чтобы начать посоветовать Excel в соответствии с конфигурацией.
Я пытаюсь использовать нить, как это
public Thread StartNewDDEThread(string topic, string cell)
{
var t = new Thread(() => InitializeDDEClient(topic, cell));
t.Start();
return t;
}
private void InitializeDDEClient(string topic, string cell)
{
try
{
DdeClient client = new DdeClient("Excel", topic);
// Subscribe to the Disconnected event. This event will notify the application when a conversation has been terminated.
client.Disconnected += (sender, args) =>
{
Console.WriteLine(
"OnDisconnected: " +
"IsServerInitiated=" + args.IsServerInitiated.ToString() + " " +
"IsDisposed=" + args.IsDisposed.ToString());
};
// Connect to the server. It must be running or an exception will be thrown.
client.Connect();
// Advise Loop
client.StartAdvise(cell, 1, true, 60000);
client.Advise += (sender, args) =>
{
try
{
DdeClient senderClient = (DdeClient)sender;
DataRow[] row = source.Select("Cell= '" + args.Item + "' AND Topic= '" + senderClient.Topic + "'");
if (row.Length > 0)
{
row[0]["DDE Price"] = args.Text.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\0", string.Empty);
string side = row[0]["Side"] as string;
string formula = row[0]["Formula"] as string;
decimal Price = decimal.Parse(row[0]["DDE Price"] as string, new NumberFormatInfo() { NumberDecimalSeparator = "," });
decimal Spread = decimal.Parse(row[0]["Spread"] as string, new NumberFormatInfo() { NumberDecimalSeparator = "," });
Expression e = new Expression(formula);
e.Parameters["Price"] = Price;
decimal result = (decimal)e.Evaluate();
row[0]["Bid"] = side == "Buy" ? result : result - Spread;
row[0]["Ask"] = side == "Buy" ? result + Spread : result;
}
} catch (Exception e)
{
MessageBox.Show(e.Message);
}
};
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Но он перестал слушать изменения Excel в какой-то момент. Я поместил точку останова в событие Advise, но оно не было получено, что означает, что оно больше не выполняется. Это правильный способ сделать это? И как редактировать разговор, если пользователь изменяет конфигурацию DDE. Спасибо