Microsoft Knowledge Base Email Alertz

KBAlertz.com: Performance Degradation in Visual Basic 6.0 Components Running in High Throughput Multi-threaded Applications

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]






Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Performance Degradation in Visual Basic 6.0 Components Running in High Throughput Multi-threaded Applications

Article ID:949142
Last Review:February 13, 2008
Revision:1.2
Source: Microsoft Support

Back to the top

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Back to the top

Action

You are running a Visual Basic 6.0 component in a high throughput, multi-threaded application; such as COM+/MTS, Active Server Pages (ASP), or a Visual Basic 6.0 ActiveX EXE.

Back to the top

Result

Performance degrades and/or the application encounters high CPU as the component services a large number of client requests.

Back to the top

Cause

The Visual Basic component may be performing an operation that results in a critical section convoy. Internally Visual Basic 6.0 uses critical sections to protect key data structures, which the Visual Basic run-time uses to maintain a wide range of per-thread project information. If the component is executing code that requires access to the critical section, multiple threads may be waiting to take ownership of the critical section. This causes a delay in each thread servicing its request. Performance may decline over time as more and more threads are created, with each one waiting to access the critical section.

Common scenarios for this to occur would be:

1. Calling procedures or variables stored in standard modules (.bas).
2. Making late bound calls on COM components.
3. Using Visual Basic intrinsic file I/O statements, such as Open, Close, Get, Put, Print, or Write.

Back to the top

Resolution

Consider making the following changes to the Visual Basic component to help avoid critical section convoys in the Visual Basic run-time.

1. Where possible, move procedures and variables stored in standard standard (.bas) modules into class modules.

2. Modify the component to use early bound calls to COM components, rather than late bound calls.

3. If the component is using the Visual Basic intrinsic file I/O statements, use one of the following alternatives instead.
    a. Use the FileSystemObject from the Scripting run-time.
    b. Use the Win32 API ReadFile and WriteFile functions.

    165942 How To Write Data to a File Using WriteFile API
    http://kbalertz.com/165942 (http://support.microsoft.com/default.aspx?scid=kb;en-us;165942)

   c. Reduce the number of times calling the file I/O get and put to read or write data. For example, read and/or write larger chunks of data at a time.


Related Knowledgebase Articles
========================

815053 INFO: Avoid Global Variables in Visual Basic COM+ Components
http://kbalertz.com/815053 (http://support.microsoft.com/default.aspx?scid=kb;en-us;815053)

241896 Threading issues with Visual Basic 6.0 ActiveX components
http://kbalertz.com/241896 (http://support.microsoft.com/default.aspx?scid=kb;en-us;241896)

Back to the top

More Information



Visual Basic uses the critical section MSVBVM60!gCSHinst to protect key data structures in per-thread project information. Here are sample call stacks showing threads waiting on this critical section in various situations.

Late bound COM component
=====================

0:000> ~7kbL 123
ChildEBP RetAddr Args to Child
00eef3e4 77f8822a 0000073c 00000000 00000000 NTDLL!NtWaitForSingleObject+0xb
00eef458 77f8819b 6aae0700 6a9e526a 6aae0770 NTDLL!RtlpWaitForCriticalSection+0x9e
00eef460 6a9e526a 6aae0770 00eef788 6a9e4979 NTDLL!RtlEnterCriticalSection+0x46
00eef46c 6a9e4979 110020d6 00eef4a4 69b38658 MSVBVM60!RTIsNativeHandler+0x16
00eef488 6a9e491f 00000000 00eef4a4 02142e40 MSVBVM60!SehTopmostBasicHandler+0x40
00eef49c 6a9f67a4 00000000 6aad1262 00eef4c4 MSVBVM60!GetCurrentEpiProject+0x18
00eef4a4 6aad1262 00eef4c4 02142e40 6aadb361 MSVBVM60!GetProjLcid+0x12
00eef4b8 6aada936 11006394 02142e40 6aabc2a4 MSVBVM60!ExDispidOfHlnam+0x1e
00eef4e4 6aadb37e 11006394 00000000 00eef51c MSVBVM60!ExVarLateMem+0x49  
00eef504 110120d5 00eef698 00eef6a8 11006394 MSVBVM60!__vbaVarLateMemCallLdRf+0x1d  <-- Shows a late bound COM call from the component
WARNING: Stack unwind information not available. Following frames may be wrong.
00eef79c 1100ef77 00eef884 00000000 00eef8a8 WICData!DllCanUnloadNow+0x8d7f


Intrinsic File I/O With Get/Put
========================

0:074> kL
ChildEBP RetAddr
0b5df5ac 77f8f295 NTDLL!NtWaitForSingleObject+0xb
0b5df620 77f87f26 NTDLL!RtlpWaitForCriticalSection+0x9e
0b5df628 6a9e5d83 NTDLL!RtlEnterCriticalSection+0x46
0b5df638 6a9e5d5d MSVBVM60!FindPECFromRTContext+0x23
0b5df644 6aaaeed8 MSVBVM60!FindProjExecContext+0x18
0b5df64c 6aa1619e MSVBVM60!epiProject::PeprojinfoMT+0xb
0b5df650 6aab9f86 MSVBVM60!GetProjectHandle+0x2b
0b5df66c 6aaba116 MSVBVM60!PrepGetPut+0x10
0b5df68c 6aab9e5a MSVBVM60!FileGetPut+0x19
0b5df6ac 0043e80f MSVBVM60!__vbaPut3+0x18 <-- Notice that the intrinsic file put is being called.
WARNING: Stack unwind information not available. Following frames may be wrong.
0b5df840 77d79290 DMSWrapper+0x3e80f
0b5df874 77d96aee rpcrt4!Invoke+0x30
0b5dfaf8 77d90719 rpcrt4!NdrStubCall2+0x664
0b5dfb5c 779e92e4 rpcrt4!CStdStubBuffer_Invoke+0x6b
0b5dfb80 7cef55fd OLEAUT32!CUnivStubWrapper::Invoke+0xe4
0b5dfbc4 7cef58d8 OLE32!SyncStubInvoke+0x61
0b5dfc0c 7ce8833d OLE32!StubInvoke+0xa8
0b5dfc70 7ce7a711 OLE32!CCtxComChnl::ContextInvoke+0xbb
0b5dfc8c 7ce7a57b OLE32!MTAInvoke+0x18
0b5dfcbc 7cef54b5 OLE32!STAInvoke+0x56

Critical section details:
0:000> !critlist
CritSec at 6aae0770. Owned by thread 0. Transitioning.
Waiting Threads: 6 7 8 9 10 12 13 14 15 16 20 21 24 25 30 35 38 46 49 52 59 68 72 74

0:000> !locks 6aae0770
CritSec MSVBVM60!gCSHinst+0 at 6AAE0770
LockCount 23
RecursionCount 0
OwningThread 0
EntryCount 6657633
ContentionCount 664323b
*** Locked



Back to the top

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

Back to the top


APPLIES TO
•Microsoft Visual Basic 6.0 Professional Edition
•Microsoft Visual Basic 6.0 Enterprise Edition

Back to the top

Keywords: 
kbnomt kbrapidpub KB949142

Back to the top

   

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please