Consulting

Results 1 to 7 of 7

Thread: Sleeper: Threading in VBA

  1. #1
    VBAX Newbie
    Joined
    Aug 2005
    Posts
    5
    Location

    Sleeper: Threading in VBA

    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

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    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
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    VBA doesn't support multi-threading.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Newbie
    Joined
    Aug 2005
    Posts
    5
    Location
    Quote Originally Posted by johnske
    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

  5. #5
    VBAX Newbie
    Joined
    Aug 2005
    Posts
    5
    Location
    Quote Originally Posted by xld
    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

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by xld
    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?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    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)
    Kind Regards,
    Ivan F Moala From the City of Sails

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •