CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2001
    Location
    Israel
    Posts
    94

    Multi Threading Problem

    I understand that there is a problem to call for a Visual basic function from a thread that no it created. How can I overcome it though, Heard that Messaging is optional but I do not know how to implement it.

    Any help would be appriciated,
    FatMan


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Multi Threading Problem

    it you are talking about normal function in standard module, it should be ok to be called by other thread, just to beware on global variable as VB is apartment-threaded.

    if you are talking about Form function, then sending message is one way. but before you send the message, you need to subclass the form to capture the message that u r going to use.

    refer http://vblib.virtualave.net there is 2 class, vbSubclassing & vbMultiThreading that make u life easy, HTH

    maybe ur situation is a bit difficult, but if I didn't answer ur question, let make me understand and maybe I can be more helpful.




  3. #3
    Join Date
    Feb 2001
    Location
    Israel
    Posts
    94

    Re: Multi Threading Problem

    I am trying the Messaging way now for some time, I sent message from a Dll function I called numbered 555 and could not catch it in VB, I will try again, but if you have a sample code for messaging with DLL I would be very greatfull.

    FatMan


  4. #4
    Join Date
    Apr 2000
    Posts
    737

    Re: Multi Threading Problem

    you should got an example. Please note, user define message must be declare as follow

    WM_LEV_MSG = WM_USER + 555&

    instead of

    WM_LEV_MSG = 555&

    cause may be used by the system, by adding WM_USER, you are safe.


  5. #5
    Join Date
    Dec 1999
    Location
    Dublin, Ireland
    Posts
    1,173

    Re: Multi Threading Problem

    For completeness you should not create your own constant for your message like this because you have no guarantee that some other program might not use that same value for one of it's messages and broadcast it thus confusing your application.

    To prevent this you should register your windows message using the RegisterWindowsMessage API call:

    'Declaration
    private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (byval lpString as string) as Long




    What I tend to do is have this wrapped up in a function shared by both applications thus:

    public Function LevWindowMessage() as Long

    If mMessage = 0 then
    mMessage = RegisterWindowMessage("WM_LEV_MSG")
    End If
    LevWindowMessage = mMessage

    End Function




    Which you can then send and recieve safely.

    For a complete example of how this is used, see the example on http://www.planetsource.com entitled "A better systray".

    -------------------------------------------------
    Ex. Datis: Duncan Jones
    Merrion Computing Ltd
    http://www.merrioncomputing.com
    '--8<-----------------------------------------
    NEW -The printer usage monitoring application
    '--8<------------------------------------------

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