This represents the client side of a DDE conversation.
For a list of all members of this type, see DdeClient Members.
System.Object DdeClient
Thread Safety
This type is safe for multithreaded operations.
Remarks
DDE conversations are established by specifying a service name and topic name pair. The service name is usually the name of the application acting as a DDE server. A DDE server can respond to multiple service names, but most servers usually only respond to one. The topic name is a logical context for data and is defined by the server application. A server can and usually does support many topic names.
After a conversation has been established by calling Connect
an application can read and write data using the Request
and Poke
methods respectively by specifying an item name supported by the active conversation. An item name identifies a unit of data. An application can also be notified of changes by initiating an advise loop on an item name using the StartAdvise
method. Advise loops can either be warm or hot. A hot advise loop returns the data associated with an item name when it changes whereas a warm advise loop only notifies the application without sending any data. Commands can be sent to the server using the Execute
method.
Callbacks and events are invoked on the thread hosting the DdeContext
. All operations must be marshaled onto the thread hosting the DdeContext
associated with this object. Method calls will block until that thread becomes available. An exception will be generated if the thread does not become available in a timely manner.
Example
The following example demonstrates how to use a DdeClient.
[C#]
using System;
using System.Text;
using NDde.Client;
public sealed class Client
{
public static void Main(string[] args)
{
// Wait for the user to press ENTER before proceding.
Console.WriteLine("The Server sample must be running before the client can connect.");
Console.WriteLine("Press ENTER to continue...");
Console.ReadLine();
try
{
// Create a client that connects to 'myapp|mytopic'.
using (DdeClient client = new DdeClient("myapp", "mytopic"))
{
// Subscribe to the Disconnected event. This event will notify the application when a conversation has been terminated.
client.Disconnected += OnDisconnected;
// Connect to the server. It must be running or an exception will be thrown.
client.Connect();
// Synchronous Execute Operation
client.Execute("mycommand", 60000);
// Synchronous Poke Operation
client.Poke("myitem", DateTime.Now.ToString(), 60000);
// Syncronous Request Operation
Console.WriteLine("Request: " + client.Request("myitem", 60000));
// Asynchronous Execute Operation
client.BeginExecute("mycommand", OnExecuteComplete, client);
// Asynchronous Poke Operation
client.BeginPoke("myitem", Encoding.ASCII.GetBytes(DateTime.Now.ToString() + "\0"), 1, OnPokeComplete, client);
// Asynchronous Request Operation
client.BeginRequest("myitem", 1, OnRequestComplete, client);
// Advise Loop
client.StartAdvise("myitem", 1, true, 60000);
client.Advise += OnAdvise;
// Wait for the user to press ENTER before proceding.
Console.WriteLine("Press ENTER to quit...");
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.WriteLine("Press ENTER to quit...");
Console.ReadLine();
}
}
private static void OnExecuteComplete(IAsyncResult ar)
{
try
{
DdeClient client = (DdeClient)ar.AsyncState;
client.EndExecute(ar);
Console.WriteLine("OnExecuteComplete");
}
catch (Exception e)
{
Console.WriteLine("OnExecuteComplete: " + e.Message);
}
}
private static void OnPokeComplete(IAsyncResult ar)
{
try
{
DdeClient client = (DdeClient)ar.AsyncState;
client.EndPoke(ar);
Console.WriteLine("OnPokeComplete");
}
catch (Exception e)
{
Console.WriteLine("OnPokeComplete: " + e.Message);
}
}
private static void OnRequestComplete(IAsyncResult ar)
{
try
{
DdeClient client = (DdeClient)ar.AsyncState;
byte[] data = client.EndRequest(ar);
Console.WriteLine("OnRequestComplete: " + Encoding.ASCII.GetString(data));
}
catch (Exception e)
{
Console.WriteLine("OnRequestComplete: " + e.Message);
}
}
private static void OnStartAdviseComplete(IAsyncResult ar)
{
try
{
DdeClient client = (DdeClient)ar.AsyncState;
client.EndStartAdvise(ar);
Console.WriteLine("OnStartAdviseComplete");
}
catch (Exception e)
{
Console.WriteLine("OnStartAdviseComplete: " + e.Message);
}
}
private static void OnStopAdviseComplete(IAsyncResult ar)
{
try
{
DdeClient client = (DdeClient)ar.AsyncState;
client.EndStopAdvise(ar);
Console.WriteLine("OnStopAdviseComplete");
}
catch (Exception e)
{
Console.WriteLine("OnStopAdviseComplete: " + e.Message);
}
}
private static void OnAdvise(object sender, DdeAdviseEventArgs args)
{
Console.WriteLine("OnAdvise: " + args.Text);
}
private static void OnDisconnected(object sender, DdeDisconnectedEventArgs args)
{
Console.WriteLine(
"OnDisconnected: " +
"IsServerInitiated=" + args.IsServerInitiated.ToString() + " " +
"IsDisposed=" + args.IsDisposed.ToString());
}
} // class
[Visual Basic]
Imports System.Text
Imports NDde.Client
Module Program
Sub Main()
' Wait for the user to press ENTER before proceding.
Console.WriteLine("The Server sample must be running before the client can connect.")
Console.WriteLine("Press ENTER to continue...")
Console.ReadLine()
Try
' Create a client that connects to 'myapp|mytopic'.
Using client As DdeClient = New DdeClient("myapp", "mytopic")
' Subscribe to the Disconnected event. This event will notify the application when a conversation has been terminated.
AddHandler client.Disconnected, AddressOf OnDisconnected
' Connect to the server. It must be running or an exception will be thrown.
client.Connect()
' Synchronous Execute Operation
client.Execute("mycommand", 60000)
' Synchronous Poke Operation
client.Poke("myitem", DateTime.Now.ToString(), 60000)
' Syncronous Request Operation
Console.WriteLine("Request: " + client.Request("myitem", 60000))
' Asynchronous Execute Operation
client.BeginExecute("mycommand", AddressOf OnExecuteComplete, client)
' Asynchronous Poke Operation
client.BeginPoke("myitem", Encoding.ASCII.GetBytes(DateTime.Now.ToString() + Convert.ToChar(0)), 1, AddressOf OnPokeComplete, client)
' Asynchronous Request Operation
client.BeginRequest("myitem", 1, AddressOf OnRequestComplete, client)
' Advise Loop
client.StartAdvise("myitem", 1, True, 60000)
AddHandler client.Advise, AddressOf OnAdvise
' Wait for the user to press ENTER before proceding.
Console.WriteLine("Press ENTER to quit...")
Console.ReadLine()
End Using
Catch e As Exception
Console.WriteLine(e.ToString())
Console.WriteLine("Press ENTER to quit...")
Console.ReadLine()
End Try
End Sub
Private Sub OnExecuteComplete(ByVal ar As IAsyncResult)
Try
Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
client.EndExecute(ar)
Console.WriteLine("OnExecuteComplete")
Catch e As Exception
Console.WriteLine("OnExecuteComplete: " + e.Message)
End Try
End Sub
Private Sub OnPokeComplete(ByVal ar As IAsyncResult)
Try
Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
client.EndPoke(ar)
Console.WriteLine("OnPokeComplete")
Catch e As Exception
Console.WriteLine("OnPokeComplete: " + e.Message)
End Try
End Sub
Private Sub OnRequestComplete(ByVal ar As IAsyncResult)
Try
Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
client.EndRequest(ar)
Console.WriteLine("OnRequestComplete")
Catch e As Exception
Console.WriteLine("OnRequestComplete: " + e.Message)
End Try
End Sub
Private Sub OnStartAdviseComplete(ByVal ar As IAsyncResult)
Try
Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
client.EndStartAdvise(ar)
Console.WriteLine("OnStartAdviseComplete")
Catch e As Exception
Console.WriteLine("OnStartAdviseComplete: " + e.Message)
End Try
End Sub
Private Sub OnStopAdviseComplete(ByVal ar As IAsyncResult)
Try
Dim client As DdeClient = DirectCast(ar.AsyncState, DdeClient)
client.EndStopAdvise(ar)
Console.WriteLine("OnStopAdviseComplete")
Catch e As Exception
Console.WriteLine("OnStopAdviseComplete: " + e.Message)
End Try
End Sub
Private Sub OnAdvise(ByVal sender As Object, ByVal args As DdeAdviseEventArgs)
Console.WriteLine("OnAdvise: " + args.Text)
End Sub
Private Sub OnDisconnected(ByVal sender As Object, ByVal args As DdeDisconnectedEventArgs)
Console.WriteLine( _
"OnDisconnected: " + _
"IsServerInitiated=" + args.IsServerInitiated.ToString() + " " + _
"IsDisposed=" + args.IsDisposed.ToString())
End Sub
End Module
Requirements
Namespace: NDde.Client
Assembly: NDde (in NDde.dll)
See Also
DdeClient Members | NDde.Client Namespace