CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2004
    Posts
    2

    Can VB execute code concurrently?

    Hi all,

    I realize this may be a fundamentally obvious answer, but I have the following scenario:

    1. A stream of string data arrives via the Winsock control (the data stream is comprised of individual CrLf delimited messages).

    2. I Split the data stream into individual messages into a listbox.

    3. I send listbox item 1 on to the rest of the program (initially sent to a subroutine I'll call 'sub1') where it's execution may take from < 1 sec to > 10 seconds.

    4. My question is this; can I (how can I) send item 2 from the listbox while item 1 is still executing code? The rest of the program does not include class modules.

    5. One oddity is that this works, i.e. multiple executions of code when the data arrives on the COM port. The messages from the COM port are also initially sent to 'sub1' from OnComm - comEvReceive.

    Thank you for any help or suggestions...

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Can VB execute code concurrently?

    Basically, vb can't do two things at once. Only one piece of code can be executing at any given time, though data can still be arriving to the port you have open.

    However, it is unclear to me what you are really getting at. Rather than asking if a particular program flow is possible, can you explain what the end results are that you want to achieve? For example, if you need the subroutine to work with more than one item in the list, you could pass an array, or a single delimited string that the sub would then parse.

    The better you can explain what you want to do, the more suggestions you will likely receive.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Sep 2004
    Location
    Sandhem, Sweden
    Posts
    20

    Re: Can VB execute code concurrently?

    Hi.

    Maybe I'm on the wrong path here, but, isn't it possible to create a new thread for the subroutine. That way you could do whatever you want while the subroutine runs in the background.

    Regards,
    Michael

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Can VB execute code concurrently?

    I see. There are two ways you may solve this problem. One is if the code can be sped up enough to eliminate inconvenient delays. The other (since vb does not support multi-threading), is to make a seporate exe to do the work, and use ShellExecute or similar to start it up.

    Can you describe what this function needs to do? Perhaps it can be made more efficiant.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Can VB execute code concurrently?

    Quote Originally Posted by WizBang
    The other (since vb does not support multi-threading), is to make a seporate exe to do the work, and use ShellExecute or similar to start it up.
    There is no reason to start another application calling ShellExecute. VB 6.0 does not directly support multithreading, and it's pretty bad in debugging multi-threaded applications, but that doesn't mean it's impossible or illegal. Same as subclassing, creating new threads using APIs may be dangerous and needs special care, but do not be scared to try it. Many have succeeded before you and might be worth considering the possible results.
    Try searching info and samples using:
    Code:
    Private Declare Function CreateThread Lib "kernel32" ( _
        lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal dwStackSize As Long, _
        ByVal lpStartAddress As Long, lpParameter As Any, _
        ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
    Regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  6. #6
    Join Date
    Aug 2004
    Posts
    2

    Re: Can VB execute code concurrently?

    Thanks you all for the responses and sorry I took so long to answer.

    Some additional information about what I'm trying to do with this program;

    The program simulates a wireless handset that allows the user to send and receive regular PBX phone calls (like any regular phone), and it allows a server to interact with the handset to send wireless handset to wireless handset calls, text messages, paging, priority rings, multiple ring tones, etc... etc... Additionally, the wireless handset sends all key presses back to the server for interpretation, the wireless handset is dumb, all logic is done on the server side.

    That being said, my program interacts with the server via keyboard (in which case the user is the server), COM port, and TCP from a real server program. The particular case I am having problems with is when I send the following message sequence to the wireless handset;

    Login
    Ring the phone
    Some other stuff

    While the phone is ringing (this is the part that takes up to 10 seconds), I would like the other stuff to happen as well.

    There are multiple subroutines and functions that run in the course of any one message (e.g. login, ring the phone).

    Again, thanks for any help.

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Can VB execute code concurrently?

    Seems to me that this should be possible without multi-threading. Why not send the request via winsock, and use the DataArrival event to respond, rather than waiting? When the data arrives, the program would know that the phone was answered, or it timed out, or whatever.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #8
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Can VB execute code concurrently?

    That might work without multi-threading. Using winsock's events, you can establish a 2-way communication pretty easily... but multi-threading is recommended when having more than one client and some server processing can take awhile, because a responsive server should be listening in a separate thread. Well, that would be my choice, but one could disagree.
    Best regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured