Register a DDE Server in a Windows Forms Application versus a Console Application

1.2k views Asked by At

I need to register a DDE Server in a Windows Form versus a Console Application. I've tried the registration code in various points in the Windows Form Application but it doesnt appear to be registering. I've tried it in frmMain and Program.cs Main().

When I attempt to the DDE Server I get the standard can't connect message: "MainForm_Load: The client failed to connect to "CRMIntegrator|myservice". Make sure the server application is running and that it supports the specified service name and topic name pair."

Here is my registration code:

public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            try
            {
                // Create a server that will register the service name 'myapp'.
                using (DdeServer server = new MyServer("CRMIntegrator"))
                {
                    // Register the service name.
                    server.Register();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
1

There are 1 answers

1
Hans Passant On BEST ANSWER

You are not using the using keyword appropriately here. After the Register() call, the server will be immediately disposed. Which does indeed make it pretty unlikely that it will still be alive by the time the Load event runs.

Make the server variable a field in your form class. Don't dispose it until the form is closed, do so in the OnFormClosed() method override or FormClosed event handler.