Microsoft KB Archive/281113

= How To Use WaitForEvents and EndWaitForEvents in eVB =

Article ID: 281113

Article Last Modified on 7/15/2004

-

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:  In Visual Basic 6.0, create a server project as follows:  Create a Standard EXE project in Visual Basic 6.0. Form1 is created by default. Add a textbox (Text1), a command button (Command1), and a Winsock control (Winsock1) to Form1. Set the Multiline properties for Text1 to true.  Paste the following code into General Declarations section: Private Sub Form_Load Winsock1.Protocol = 0 Winsock1.LocalPort = 0 Winsock1.Listen Text1.Text = &quot;RemotePort: &quot; & Winsock1.LocalPort & vbCrLf & &quot;RemoteHost: &quot; & Winsock1.LocalIP End Sub Private Sub Command1_Click Winsock1.SendData &quot;This is a test string&quot; & vbCrLf End Sub Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) If Winsock1.State <> 0 Then Winsock1.Close Winsock1.Accept requestID End Sub   In eMbedded Visual Basic 3.0, create the Formless application as follows:  Create a New Windows CE Formless project, and select Pocket PC as the intended platform.</li> 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.</li>  Add the following code to General Declarations section: Option Explicit Dim W1 Dim data Sub Main Dim mydata Set W1 = CreateObjectWithEvents(&quot;WinSock.WinSock&quot;, &quot;W1_&quot;) W1.LocalPort = 0 W1.Protocol = 0 W1.RemotePort = 1234              'Get RemotePort from desktop VB application. W1.RemoteHost = &quot;111.111.111.111&quot; '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 </li></ol> </li>  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=&quot;111.111.111.111&quot; NOTE: Make sure that the preceding string is surrounded by quotation marks. </li>  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. </li> 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.</li></ol>

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 &quot;Creating Events for a Formless Application&quot; 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.

Keywords: kbhowto KB281113

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.