Title | Use DDE to make a single instance of a program handle all requests |
Description | This example shows how to use DDE to make a single instance of a program handle all requests in Visual Basic 6. It uses App.PrevInstance to see if it is the first instance running. If it is not the first instance, the program uses DDE to send information to the running instance and then exits. |
Keywords | one instance, instance, DDE |
Categories | Software Engineering, Windows |
|
|
When it starts, the program uses App.PrevInstance to see if it is the first instance running. If it is not the first instance, the program uses DDE to send information to the previous instance. It then activates that instance and exits.
The LinkTopic = | where is the name of the project file (.vbp) and is the value set in the form's LinkTopic property.
|
|
' At design time:
' Save project as OneInstDDE.vbp
' Form.LinkMode = Source
' Form.LinkTopic = one_instance_topic
Private Sub Form_Load()
Dim cap As String
If App.PrevInstance Then
' DDE Destination.
' Clear any existing DDE link.
Text1.LinkMode = vbLinkNone
' Define link to source.
Text1.LinkTopic = _
"OneInstDDE|" & _
"one_instance_topic"
' Link to Source.Text1.
Text1.LinkItem = "Text1"
' Establish manual link.
Text1.LinkMode = vbLinkManual
DoEvents
' Push data.
Text1.Text = Now
Text1.LinkPoke
' Activate the previous instance.
cap = Me.Caption
Me.Caption = ""
AppActivate cap
' Exit.
Unload Me
Else
' DDE Source.
' Save the current time.
Text1.Text = Now
End If
End Sub
|
|
When the second instance sends data to the first instance, the first instance's Text1 control's contents change. The Text1_Change event handler adds the new text to the program's list.
|
|
' Add this data to the list.
Private Sub Text1_Change()
List1.AddItem Text1.Text
End Sub
|
|
|
|