How To Use WaitForEvents and EndWaitForEvents in eVB (281113)



The information in this article applies to:

  • Microsoft eMbedded Visual Basic 3.0

This article was previously published under Q281113

SUMMARY

Formless projects support only the App object and the Err object. The App object provides two methods, WaitForEvents and EndWaitForEvents, to enable formless applications to process events. When you write event-driven code for a formless application, the procedure that calls WaitForEvents remains suspended until you call App.End or App.EndWaitForEvents. The App.EndWaitForEvents method immediately returns control to the next statement after the App.WaitForEvents statement. However, if your application is not waiting for events, you should use App.End instead of the App.EndWaitForEvents statement, which has no effect.

You can use the CreateObjectWithEvents function to create ActiveX controls that respond to events. CreateObjectWithEvents can only be used with non-graphical controls such as the Comm and WinSock controls.

MORE INFORMATION

The sample below uses WinSock controls to send a string message from a Visual Basic 6.0 desktop server application to an eMbedded Visual Basic (eVB) 3.0 client project. The eVB client is run and its execution is suspended by using AppWaitForEvents. A string message is then sent from the server and arrives at the eVB client. At this time, the DataArrival event is fired and makes a call to EndWaitForEvents, which cancels the suspension of code in Sub Main and allows the code execution to continue. Please note that you can only use AppWaitForEvents and EndWaitForEvents in formless eVB projects.

To create the following sample, you must create a desktop Visual Basic 6.0 application and an eVB application that will run on a Pocket PC device as follows:
  1. In Visual Basic 6.0, create a server project as follows:
    1. Create a Standard EXE project in Visual Basic 6.0. Form1 is created by default.
    2. Add a textbox (Text1), a command button (Command1), and a Winsock control (Winsock1) to Form1.
    3. Set the Multiline properties for Text1 to true.
    4. Paste the following code into General Declarations section:
      Private Sub Form_Load()
          Winsock1.Protocol = 0
          Winsock1.LocalPort = 0
          Winsock1.Listen
          Text1.Text = "RemotePort: " & Winsock1.LocalPort & vbCrLf & "RemoteHost: " & Winsock1.LocalIP
      End Sub
      Private Sub Command1_Click()
          Winsock1.SendData "This is a test string" & vbCrLf
      End Sub
      Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
          If Winsock1.State <> 0 Then Winsock1.Close
          Winsock1.Accept requestID
      End Sub
      						
  2. In eMbedded Visual Basic 3.0, create the Formless application as follows:
    1. Create a New Windows CE Formless project, and select Pocket PC as the intended platform.
    2. From the Project menu, click Components, and then select the Microsoft CE WinSock Control 3.0 check box to add a WinSock control to the project.
    3. Add the following code to General Declarations section:
      Option Explicit
      Dim W1
      Dim data 
      Sub Main()
          Dim mydata
          Set W1 = CreateObjectWithEvents("WinSock.WinSock", "W1_")
          W1.LocalPort = 0
          W1.Protocol = 0
          W1.RemotePort = 1234               'Get RemotePort from desktop VB application.
          W1.RemoteHost = "111.111.111.111"  'Get RemoteHost from desktop VB application.
          W1.Connect
      
          App.WaitForEvents
          mydata = data
      End Sub
      Public Sub W1_DataArrival(ByVal bytesTotal As Long)
      'This event is fired when data is received from server.
          Dim sockdata
          W1.GetData sockdata, vbString, bytesTotal 'Get the data
          data = data & sockdata
          If Right(data, 2) = vbCrLf Then
              App.EndWaitForEvents 'End wait when data is received
          End If
      End Sub
      Public Sub W1_ConnectionRequest(ByVal requestID As Long)
          W1.Accept requestID
      End Sub
      						
  3. Run the Visual Basic 6.0 desktop application in the Visual Basic 6.0 Integrated Development Environment (IDE) to start the server and to get the RemotePort and RemoteHost IP addresses.

    Copy the RemotePort value from Text1, and paste it in the following line of the eVB Sub Main procedure:
    W1.RemotePort = 1234
    						
    Likewise, copy the RemoteHost string from Text1 in the desktop Visual Basic application, and paste it in the following line of the eVB Sub Main procedure:
    W1.RemoteHost="111.111.111.111"
    						
    NOTE: Make sure that the preceding string is surrounded by quotation marks.
  4. Set a breakpoint on the following line of code in the eVB Sub Main:
    mydata = data
    						
    This breakpoint will only be hit if EndWaitForEvents cancels AppWaitForEvents, which only occurs if the DataArrival event occurs. The DataArrival event is triggered once the client's WinSock control receives a string from the server WinSock on the desktop. When you run the eVB project on the Pocket PC device, no string is sent from the server; thus, execution should not stop at the breakpoint.
  5. To send a string from the desktop to the device, click Send on the form in the desktop Visual Basic 6.0 application. After a small time delay, the string should arrive at the WinSock control on the device, and the breakpoint should be hit.
If you use the serial port, refer to the following article in the Microsoft Knowledge Base:

189626 How To Use the Winsock Control Through a Serial Connection

To use the Comm control in a similar fashion, see the "Creating Events for a Formless Application" topic in the online help documentation. In this sample, execution is suspended by calling WaitForEvents. When the OnComm event is triggered, EndWaitForEvents is called, and the code execution resumes.

Additional Notes

  • In the WaitForEvents method, .pvbload does not exit when it is used with the App.End method.
  • The application does not exit if you call the WaitForEvents method twice.
  • An error does not occur if you call the WaitForEvents method from Form code.

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbhowto KB281113