|
-
Dec 11th, 2009, 05:11 AM
#1
Thread Starter
Hyperactive Member
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:
Option Explicit
Private Sub Form_Load()
MyMessageId = RegisterWindowMessage(PrivateMessage)
Dim FileName As String
FileName = Command$
If App.PrevInstance Then
If Len(FileName) Then
Dim N As Long
For N = 1 To Len(FileName)
SendMessage HWND_BROADCAST, MyMessageId, Asc(Mid$(FileName, N)), ByVal 0&
DoEvents
Next
SendMessage HWND_BROADCAST, MyMessageId, 0, ByVal 0& 'terminate the string
End If
Unload Me
Else
Show
If Len(FileName) Then OpenFile FileName
lpWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End If
End Sub
Module1 :
vb Code:
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
If uMsg = MyMessageId Then
Static FileName As String
If wParam Then
FileName = FileName & Chr$(wParam)
Else
OpenFile FileName
FileName = vbNullString
End If
End If
WndProc = CallWindowProc(lpWndProc, hWnd, uMsg, wParam, lParam)
End Function
Public Sub OpenFile(FileName As String)
'
List1.AddItem FileName
'
End Sub
here is the link of the code am talking about
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|