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)
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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
Public Const PrivateMessage As String = "This is my application's private message" '<- Change this to a suitable message string
Public MyMessageId As Long
Public lpWndProc As Long
Public Const HWND_BROADCAST = &HFFFFFFFF
Public Const GWL_WNDPROC = (-4)
Public Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
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.
Re: Force new instance to wait untill previous one finish working
Originally Posted by techgnome
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 ...
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?
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.
Re: Force new instance to wait untill previous one finish working
Originally Posted by Xiphias3
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
Re: Force new instance to wait untill previous one finish working
Originally Posted by Xiphias3
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 ?