Microsoft KB Archive/201014

= PRB: Blocking Occurs When You Call a Visual Basic ActiveX EXE from Active Server Pages =

Article ID: 201014

Article Last Modified on 11/26/2003

-

APPLIES TO


 * Microsoft Internet Information Server 4.0
 * Microsoft Active Server Pages 4.0
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q201014



SYMPTOMS
When you instantiate an ActiveX EXE component from Active Server Pages (ASP) in page scope only, by default, all requests to the component are serialized to a single thread. This may cause contention and blocking.



CAUSE
Visual Basic 6.0 only supports Single-threaded or Apartment-threaded objects. By default, ActiveX EXE projects are set to the Apartment threading model. Thus, a Visual Basic ActiveX EXE is subject to the rules that COM imposes on single-threaded apartment (STA) objects.

If an object that runs in an STA is called concurrently by multiple clients (regardless of their threading model), COM synchronizes access to the object by posting window messages to the component's message queue. As a result, the object only receives one call each time it retrieves and dispatches a COM-related message.

Although this interference implies some performance penalty, it allows applications that support different threading models to work together. Thus, all possible combinations of client and out-of-process component interoperability are supported.



RESOLUTION
By default, the Instancing property of a Visual Basic ActiveX EXE is set to MultiUse. Therefore, only one server process is created using the STA model. Because Visual Basic only supports Single-threaded and STA objects, you cannot use either Free-threading or Both-threading to create multi-threaded objects. However, you can achieve concurrent execution as follows:


 * Increase the number of threads in the thread pool.
 * In the Visual Basic project properties, set the "Thread by object" option.



Steps to Reproduce Behavior
 Use Performance Monitor (PerfMon) to monitor the following counters:  Active Server Pages object  Requests Queued Requests Executing Sessions Total</ul> </li> Process object (for the ActiveX EXE process) <ul> Thread Count</li></ul> </li></ul> </li> If you are using a single computer as your client, make sure that you have two instances of Internet Explorer (Iexplore.exe) running. This ensures that you have two separate client processes.</li>  Create a Visual Basic ActiveX EXE named ThreadWaitProject.exe with one class named ThreadTest. Accept the default values of the threading model in the project properties, and implement the following function: Private Declare Function GetCurrentThreadId Lib "kernel32" As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Function ThreadWait(nSeconds As Long) As Long Sleep nSeconds * 1000 ThreadWait = GetCurrentThreadId End Function </li>  Create an ASP page named Threadwait_vb_exe.asp, and paste the following script: <% Option Explicit DIM NSec

Sub TestEXE response.write "<H1>OOP VB EXE Threadwait</H1>" Dim objTest Set objTest = Server.CreateObject("ThreadWaitProjectEXE.ThreadSleep") Response.Write "StartTime: " & Now & "<BR>" Response.Write "ThreadID: " & objTest.ThreadWait(10) & "<BR>" Response.Write "EndTime: " & Now & "<BR>" Response.write "Session ID: " & Session.SessionId & "<BR>" Set objTest = Nothing End Sub

TestEXE %>                   </li> From two distinct clients (see step 2), request the ASP page Threadwait_vb_exe at the same time. Both requests share the same ThreadID, and the start and end times of both requests are serialized (that is, the second request starts only after the first request finishes). You can use PerfMon (see step 1) to see that one request is queued while the other is being executed. When you apply the resolution, the ThreadIDs differ for the two requests.</li></ol>

Keywords: kbprb KB201014

-

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

© Microsoft Corporation. All rights reserved.