我用计时器制作了一个DDE服务器示例。
using System;
using System.Windows.Forms;
using NDde.Server;
using System.Timers;
namespace DDEServer_Test
{
public partial class DDEServer_MainForm : Form
{
public DDEServer_MainForm()
{
InitializeComponent();
}
public void runDDEServer()
{
try
{
using (DdeServer server = new theDDEServer("dde_server"))
{
server.Register();
}
}
catch (Exception ex)
{
}
}
public void dispServerName(string serverName = "")
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = serverName; });
}
else
{
this.textBox1.Text = serverName;
}
//this.textBox1.Text = serverName;
}
public void dispTopicName(string topicName = "")
{
if (textBox2.InvokeRequired)
{
textBox2.Invoke((MethodInvoker)delegate { textBox2.Text = topicName; });
}
else
{
this.textBox2.Text = topicName;
}
//this.textBox2.Text = topicName;
}
public void dispItemName(string itemName = "")
{
if (textBox3.InvokeRequired)
{
textBox3.Invoke((MethodInvoker)delegate { textBox3.Text = itemName; });
}
else
{
this.textBox3.Text = itemName;
}
//this.textBox3.Text = itemName;
}
public void dispHandleString(string handleString = "")
{
if (textBox4.InvokeRequired)
{
textBox4.Invoke((MethodInvoker)delegate { textBox4.Text = handleString; });
}
else
{
this.textBox4.Text = handleString;
}
//this.textBox4.Text = handleString;
}
public void dispFormatString(string formatString = "")
{
if (textBox5.InvokeRequired)
{
textBox5.Invoke((MethodInvoker)delegate { textBox5.Text = formatString; });
}
else
{
this.textBox5.Text = formatString;
}
//this.textBox5.Text = formatString;
}
public void dispCommandString(string commandString="")
{
if (textBox6.InvokeRequired)
{
textBox6.Invoke((MethodInvoker)delegate { textBox6.Text = commandString; });
}
else
{
this.textBox6.Text = commandString;
}
//this.textBox6.Text = commandString;
}
public void dispDataString(string dataString = "")
{
if (textBox7.InvokeRequired)
{
textBox7.Invoke((MethodInvoker)delegate { textBox7.Text = dataString; });
}
else
{
this.textBox7.Text = dataString;
}
//this.textBox7.Text = dataString;
}
private void button1_Click(object sender, EventArgs e)
{
runDDEServer();
}
//class
public sealed class theDDEServer : DdeServer
{
DDEServer_MainForm mainForm = new DDEServer_MainForm();
private System.Timers.Timer _Timer = new System.Timers.Timer();
public theDDEServer(string service) : base(service)
{
_Timer.Elapsed += this.OnTimerElapsed;
_Timer.Interval = 1000;
_Timer.SynchronizingObject = this.Context;
}
private void OnTimerElapsed(object sender, ElapsedEventArgs args)
{
Advise("*", "*");
}
public override void Register()
{
Console.WriteLine("R");
base.Register();
_Timer.Start();
}
public override void Unregister()
{
_Timer.Stop();
base.Unregister();
}
protected override bool OnBeforeConnect(string topic)
{
Console.WriteLine("Before Connect");
mainForm.dispServerName(base.Service);
mainForm.dispTopicName(topic);
mainForm.dispHandleString();
mainForm.dispHandleString();
mainForm.dispItemName();
mainForm.dispFormatString();
mainForm.dispCommandString();
mainForm.dispDataString();
return true;
}
protected override void OnAfterConnect(DdeConversation conversation)
{
Console.WriteLine("After Connect");
mainForm.dispServerName(conversation.Service);
mainForm.dispTopicName(conversation.Topic);
mainForm.dispHandleString(conversation.Handle.ToString());
mainForm.dispHandleString();
mainForm.dispItemName();
mainForm.dispFormatString();
mainForm.dispCommandString();
mainForm.dispDataString();
}
protected override void OnDisconnect(DdeConversation conversation)
{
Console.WriteLine("Disconnect");
mainForm.dispServerName(conversation.Service);
mainForm.dispTopicName(conversation.Topic);
mainForm.dispHandleString(conversation.Handle.ToString());
mainForm.dispItemName();
mainForm.dispFormatString();
mainForm.dispCommandString();
mainForm.dispDataString();
}
protected override bool OnStartAdvise(DdeConversation conversation, string item, int format)
{
Console.WriteLine("Start Advise");
mainForm.dispServerName(conversation.Service);
mainForm.dispTopicName(conversation.Service);
mainForm.dispHandleString(conversation.Handle.ToString());
mainForm.dispItemName(item);
mainForm.dispFormatString(format.ToString());
mainForm.dispCommandString();
mainForm.dispDataString();
return format == 1;
}
protected override void OnStopAdvise(DdeConversation conversation, string item)
{
Console.WriteLine("Stop Advise");
mainForm.dispServerName(conversation.Service);
mainForm.dispTopicName(conversation.Service);
mainForm.dispHandleString(conversation.Handle.ToString());
mainForm.dispItemName(item);
mainForm.dispFormatString();
mainForm.dispCommandString();
mainForm.dispDataString();
}
protected override ExecuteResult OnExecute(DdeConversation conversation, string command)
{
Console.WriteLine("Execute");
mainForm.dispServerName(conversation.Service);
mainForm.dispTopicName(conversation.Service);
mainForm.dispHandleString(conversation.Handle.ToString());
mainForm.dispItemName();
mainForm.dispFormatString();
mainForm.dispCommandString(command);
mainForm.dispDataString();
return ExecuteResult.Processed;
}
protected override PokeResult OnPoke(DdeConversation conversation, string item, byte[] data, int format)
{
Console.WriteLine("Poke");
mainForm.dispServerName(conversation.Service);
mainForm.dispTopicName(conversation.Service);
mainForm.dispHandleString(conversation.Handle.ToString());
mainForm.dispItemName(item);
mainForm.dispFormatString(format.ToString());
mainForm.dispCommandString();
mainForm.dispDataString(data.Length.ToString());
return PokeResult.Processed;
}
protected override RequestResult OnRequest(DdeConversation conversation, string item, int format)
{
Console.WriteLine("Request");
mainForm.dispServerName(conversation.Service);
mainForm.dispTopicName(conversation.Service);
mainForm.dispHandleString(conversation.Handle.ToString());
mainForm.dispItemName(item);
mainForm.dispFormatString(format.ToString());
mainForm.dispCommandString();
mainForm.dispDataString();
if (format == 1)
{
return new RequestResult(System.Text.Encoding.ASCII.GetBytes("Now = " + DateTime.Now.ToString() + "\0"));
}
return RequestResult.NotProcessed;
}
protected override byte[] OnAdvise(string topic, string item, int format)
{
Console.WriteLine("Advise");
mainForm.dispServerName(this.Service);
mainForm.dispTopicName(topic);
mainForm.dispHandleString(item);
mainForm.dispItemName(item);
mainForm.dispFormatString(format.ToString());
mainForm.dispCommandString();
mainForm.dispDataString();
if (format == 1)
{
return System.Text.Encoding.ASCII.GetBytes("Now = " + DateTime.Now.ToString() + "\0");
}
return null;
}
public void TargetInvocationException(Exception inner)
{
MessageBox.Show(inner.Message);
}
}
}
}
单击按钮后,开始运行DDE服务器。控制台显示“ R”,表示它运行Register()并且_Timer已启动。 mscorlib.dll中出现错误消息“ System.Reflection.TargetInvocationException”,并且该进程在Advise(“ ”,“ ”);
处停止在Advise()和_Timer中都没有Invoke()方法。 请帮我解决问题。谢谢。
答案 0 :(得分:1)
您似乎遇到了一些收容/终身问题。该窗体创建一个theDdeServer,然后theDdeServer创建一个主窗体的不同实例(但从不显示)。看起来不对。您可能希望将表单传递给施工中的服务器
但是最可能的原因是该表单创建了theDdeServer,然后立即将其处置:
public void runDDEServer()
{
try
{
using (DdeServer server = new theDDEServer("dde_server"))
{
server.Register();
}
}
catch (Exception ex)
{
}
}
在using
块的结尾处,.Net将破坏您的服务器。您不想在这里使用using
。相反,您希望表单保留在服务器上...然后在处置表单后将其删除。像这样:
public partial class DDEServer_MainForm : Form
{
DdeServer server;
/* snipped out code */
public void runDDEServer()
{
try
{
server = new theDDEServer("dde_server", this);
server.Register();
}
catch (Exception ex)
{
//--> you should definitely do something here. Print a message at least!
}
}
/* snipped out code */
//--> In the form designer, hook this method to the OnClosing event!
private void closeForm(object sender, System.ComponentModel.CancelEventArgs e)
{
server.Unregister();
server.Dispose();
}
}
然后,在DdeServer中,将表单作为构造函数参数:
public sealed class theDDEServer : DdeServer
{
DDEServer_MainForm mainForm;
System.Timers.Timer _Timer = new System.Timers.Timer();
public theDDEServer(string service, Form mainForm) : base(service)
{
DDEServer_MainForm = mainForm;
_Timer.Elapsed += this.OnTimerElapsed;
_Timer.Interval = 1000;
_Timer.SynchronizingObject = this.Context;
}
/* snipped out code */
public override void Unregister()
{
DDEServer_MainForm = null;
_Timer.Stop();
base.Unregister();
}
/* snipped out code */
}
答案 1 :(得分:0)
按照Clay的说明进行操作并修改代码。这是代码,它可以正常运行。
using System;
using System.Windows.Forms;
using NDde.Server;
using System.Timers;
namespace DDEServer_Test
{
public partial class DDEServer_MainForm : Form
{
theDDEServer server;
public DDEServer_MainForm()
{
InitializeComponent();
}
public void runDDEServer()
{
try
{
server = new theDDEServer("DDE_Server", this);
server.Register();
}
catch (Exception ex)
{
MessageBox.Show("DDE Server registered failed. " + ex.Message);
}
}
private void DDEServer_MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (server.IsRegistered)
server.Unregister();
server.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
runDDEServer();
}
}
public sealed class theDDEServer : DdeServer
{
DDEServer_MainForm server_mainForm;
private System.Timers.Timer _Timer = new System.Timers.Timer();
public theDDEServer(string service, DDEServer_MainForm mainform) : base(service)
{
server_mainForm = mainform;
_Timer.Elapsed += this.OnTimerElapsed;
_Timer.Interval = 1000;
_Timer.SynchronizingObject = this.Context;
}
private void OnTimerElapsed(object sender, ElapsedEventArgs args)
{
Console.WriteLine("Advise all");
this.Advise("*", "*");
}
public override void Register()
{
Console.WriteLine("Register");
base.Register();
_Timer.Start();
}
public override void Unregister()
{
Console.WriteLine("Unregister");
server_mainForm = null;
_Timer.Stop();
base.Unregister();
}
protected override bool OnBeforeConnect(string topic)
{
Console.WriteLine("OnBeforeConnect");
return true;
}
protected override void OnAfterConnect(DdeConversation conversation)
{
Console.WriteLine("OnAfterConnect");
}
protected override void OnDisconnect(DdeConversation conversation)
{
Console.WriteLine("OnDisconnect");
}
protected override bool OnStartAdvise(DdeConversation conversation, string item, int format)
{
Console.WriteLine("OnStartAdvise");
return format == 1;
}
protected override void OnStopAdvise(DdeConversation conversation, string item)
{
Console.WriteLine("OnStopAdvise");
}
protected override ExecuteResult OnExecute(DdeConversation conversation, string command)
{
Console.WriteLine("OnExecute");
return ExecuteResult.Processed;
}
protected override PokeResult OnPoke(DdeConversation conversation, string item, byte[] data, int format)
{
Console.WriteLine("OnPoke");
return PokeResult.Processed;
}
protected override RequestResult OnRequest(DdeConversation conversation, string item, int format)
{
Console.WriteLine("OnRequest");
if (format == 1)
{
Console.WriteLine("Request item = " + item);
return new RequestResult(System.Text.Encoding.ASCII.GetBytes("Now = " + DateTime.Now.ToString() + "\0"));
}
return RequestResult.NotProcessed;
}
protected override byte[] OnAdvise(string topic, string item, int format)
{
Console.WriteLine("OnAdvise");
if (format == 1)
{
Console.WriteLine("Advise item = " + item);
return System.Text.Encoding.ASCII.GetBytes("Now = " + DateTime.Now.ToString() + "\0");
}
return null;
}
}
}