PDA

View Full Version : Sleeper: Threading in VBA



venkyhyd
08-21-2005, 11:10 PM
I am a NewBie to VBA with Excel, can guide me in implementing Multithreading in VBA, I guess VBA with Excel will support Multithreading any samples wld be of great help.

thanks in advance
venky

johnske
08-23-2005, 02:20 AM
Hi venky,

I've taken the liberty of moving your post to the excel Help forum, where you may possibly have more chance of getting a response to your question

Regards,
John :)

Bob Phillips
08-23-2005, 03:00 AM
VBA doesn't support multi-threading.

venkyhyd
08-23-2005, 03:07 AM
Hi venky,

I've taken the liberty of moving your post to the excel Help forum, where you may possibly have more chance of getting a response to your question

Regards,
John :)

hi Jhon,

Thanx, hope to get some help thr...anyhow i did solved it using the timer method, hope to get more other than Timers...

venky

venkyhyd
08-23-2005, 03:09 AM
VBA doesn't support multi-threading.

Thnx i did tried solving the requirement with Timers but hope to get some other techniques than timers

venky

TonyJollans
08-23-2005, 03:42 AM
VBA doesn't support multi-threading.

Can you point me to somewhere that documents this please? I have never done it but I don't see why the CreateThread API call cannot be used.

The Timer, in effect, creates a separate thread. So what is it about multithreading that VBA doesn't support?

Ivan F Moala
08-27-2005, 01:23 AM
Tony, VBA/VB supports objects in both single threaded and apartment models.

In my endeavours to try new or perhaps impossible things within VBA I some times neglect
the fact that"just because you can, then you should" .... sometimes you need to take stock
of what it is you are doing it for.
The API call CreateThread is one such API call that you really need to be aware of what you are doing!
Just because you can do it, doesn't in this case mean you should. ( I have been guilty of this:) )
Don't forget that @ the CPU register level (low level machine language) it is the CPU that handles
(by quickly swapping register value, instruction pointers and stack pointers) to give you the
appearance of mutlitasking without interfering with each other. Each process consists of a virtual
address space that includes executable instructions, a set of resources such as file handles,
and one or more threads that execute within its address space. Typical applications consist
of only one process, so program and process are often used synonymously. Why do I mention this?
because OS handle this differently eg NT schedules this .. the Win32 API defines four priority classes:
realtime, high, normal, and idle. These names correspond to priority levels 24, 13, 8, and 4.
Setting a process priority class causes all the threads of that process to begin executing at
priorities within ?2 of the class priority. One of the scheduler's jobs is to assign units of
CPU time (quantums) to threads. There really is a lot happening @ this level, better to let the
OS handle it, BUT if you do then you need to know about COMs (COMs is complex enought)
and it's underlying rules. Most people think first of the interface part of the COMS rules ie the methods
and properties that an object exposes. But it also defines THREADING as part of its rules, and like
any part of the COMs rules if you break it, you can get into very deep trouble.
The programer will need to know of the 3 threading models AND code appropriately so as to
adhere STRICTLY to the threading requirements of each object that it creates.
If you break the COM rules, you are no longer protected by those features in COM
that allow objects to successfully communicate (Interface) with each other and with clients.

If you must use that API then you MUST adhere to the COMs rules.

There are a number of OLE.dll calls to do to get this right, most of these are interface calls
CoMarshalInterThreadInterfaceInStream
CoInitialize
CoUninitialize, you will need an interface identifier
IDispatch interface (interface for dispatching, you will see this a lot for COM and C++ as it
is a std call)
CoGetInterfaceAndReleaseStream (you need to create a proxy object to represent your
new object)