Results 1 to 10 of 10

Thread: Force new instance to wait untill previous one finish working

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Force new instance to wait untill previous one finish working

    How can i tell each new instance, once you are launched, check if a previous instance is sending the message(using api sendmessage), if yes, wait, before you send your message untill the previous instance send all it's messages (the file name), and once done and previous instance close, now you can start sending your message .... Something like making instances into a queue

    Let me now explain the why

    The following Code (found in the net), let us send a file name after we right click on it in window explorer, to our application.

    In this code, each new instance is getting the file name from the command$, sending letter by letter the file name to the WndProc() , using the SendMessage api.

    Then in WndProc we get the letters and we compose again the file name ! Wich allow us later to open it or work on it (add it to listbox), this job is made into the sub called OpenFile.

    This works great when i have only 1 command sent (right click on 1 file and open it with the application) , but if i have many files selected here comes the problem :


    All instances are almost using the sendmessage api on the same time, so the file that we receivne in WndPRoc() won't be correct.

    For example : if we have selected 3 files C:\file1.txt C:\file2.txt C:\file3.txt
    we right click and open with our application
    3 instances are opened on the same time, they will all call the sendmessage api to send their file name (letter by letter) to the WndPRoc() .
    WndPRoc() will receive letter from each on same time, so the result will be that when we will try to recompose the file name in WndPRoc(), we won't get C:\file1.txt then C:\file2.txt then C:\file3.txt because all were sent on the same time, so we will get something like :

    CCC:::\\\fffiiillleee123...tttxxxttt (maybe not exactly in this order, but something like this)

    From1 :

    vb Code:
    1. Option Explicit
    2. Private Sub Form_Load()
    3.     MyMessageId = RegisterWindowMessage(PrivateMessage)
    4.     Dim FileName As String
    5.     FileName = Command$
    6.     If App.PrevInstance Then
    7.         If Len(FileName) Then
    8.             Dim N As Long
    9.             For N = 1 To Len(FileName)
    10.                 SendMessage HWND_BROADCAST, MyMessageId, Asc(Mid$(FileName, N)), ByVal 0&
    11.                 DoEvents
    12.             Next
    13.             SendMessage HWND_BROADCAST, MyMessageId, 0, ByVal 0& 'terminate the string
    14.         End If
    15.         Unload Me
    16.     Else
    17.         Show
    18.         If Len(FileName) Then OpenFile FileName
    19.         lpWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
    20.     End If
    21. End Sub


    Module1 :
    vb Code:
    1. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
    3. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    4. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    5. Public Const PrivateMessage As String = "This is my application's private message" '<- Change this to a suitable message string
    6. Public MyMessageId As Long
    7. Public lpWndProc As Long
    8. Public Const HWND_BROADCAST = &HFFFFFFFF
    9. Public Const GWL_WNDPROC = (-4)
    10.  
    11. Public Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    12.     If uMsg = MyMessageId Then
    13.         Static FileName As String
    14.         If wParam Then
    15.             FileName = FileName & Chr$(wParam)
    16.         Else
    17.             OpenFile FileName
    18.             FileName = vbNullString
    19.         End If
    20.     End If
    21.     WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
    22. End Function
    23.  
    24. Public Sub OpenFile(FileName As String)
    25.     '
    26.     List1.AddItem FileName
    27.  
    28.     '
    29. End Sub



    here is the link of the code am talking about

  2. #2
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: Force new instance to wait untill previous one finish working

    Ah, I see what you're saying. You need to use WM_COPYDATA, with a Byte Array that represents the file name. Gimme a few minutes......

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Re: Force new instance to wait untill previous one finish working

    take your time i will be waiting and thanks for caring to help

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Force new instance to wait untill previous one finish working

    What the ...???? Woah... hold on a sec there... what in the world are you doing? You're running an app that's a getting a list of files from the command line and then doing WHAT? Sending the file names one character at a time, to where? Itself? What? That's crazy. Once you hace the file name (form1, line 5) .... just call OpenFile and pass it the filename. I don't get what it is you are doing by sending the file names using a win message like that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Re: Force new instance to wait untill previous one finish working

    Quote Originally Posted by techgnome View Post
    What the ...???? Woah... hold on a sec there... what in the world are you doing? You're running an app that's a getting a list of files from the command line and then doing WHAT? Sending the file names one character at a time, to where? Itself? What? That's crazy. Once you hace the file name (form1, line 5) .... just call OpenFile and pass it the filename. I don't get what it is you are doing by sending the file names using a win message like that.

    -tg
    the command doesn't send a list of files - it sends only 1 file at time !!! For each file selected the command open a new instance of your application ! The new instance send the file name to the first instance and close itself ...

  6. #6
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: Force new instance to wait untill previous one finish working

    Sorry for the delay. I had dishes and stuff. Anyway, here's a project that sends data across processes. Currently only works for 1 file. I'm looking into it. Could you upload your project so I can look at a few things?
    Attached Files Attached Files

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Re: Force new instance to wait untill previous one finish working

    Thanks for your reply, i will check your code, but anyway i need it for many files not only one...
    I can't upload My code because i made it using DDE, you can check This Thread so you understand more what is going on and there is the part of my code related to DDE...
    When i got the problem with DEE i said maybe trying api is better and faster, that's why i am trying to see how can i make it with sendmessage, to replace the actual code by sendmessage api maybe i won't face the same problem am facing with DEE
    Last edited by justgreat; Dec 11th, 2009 at 09:51 AM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Re: Force new instance to wait untill previous one finish working

    Quote Originally Posted by Xiphias3 View Post
    Sorry for the delay. I had dishes and stuff. Anyway, here's a project that sends data across processes. Currently only works for 1 file. I'm looking into it. Could you upload your project so I can look at a few things?

    Thanks , my first impression is that what you posted is what i need but i can't confirm before i read the code and see and if it's ,ok, i will try to adapt it to what i want to fit my code.
    By the way, did you check my code in the other thread ? if not and you want me to send it to you in private, please let me know and send me your email in private so that i mail it to you
    thanks a lot again, i will keep you updated if it worked or not , but i think that it will

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Re: Force new instance to wait untill previous one finish working

    Quote Originally Posted by Xiphias3 View Post
    Sorry for the delay. I had dishes and stuff. Anyway, here's a project that sends data across processes. Currently only works for 1 file. I'm looking into it. Could you upload your project so I can look at a few things?

    This code is nice, but their is a problem i am facing which is that during the file sending, if the user closed the explorer windows from which he right click on the files, even if the program.exe is still showing (not closed), the transfer of files name stop, do you have an idea how to solve this ?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    441

    Re: Force new instance to wait untill previous one finish working

    Also i would like to know if there is a way to detect that all files names are transfered - to know when i can start working on them

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