Hi, I am currently in the process of writing a database application that will start at Windows start up and reside in the system tray with all forms hidden until the user selects from the system tray icons popup menu or double clicks the desktop icon, at which time a form (currently the main form is shown).
The application will be used on a large network of computers with various operating systems ranging from Windows XP to Windows 10 and thus needs to be adaptable.
When the application is run a mutex is created using the applications GUID.
When the user double clicks on the desktop icon a second instance of the application is created which again tries to create a mutex using the same GUID, this being unsuccessful, the second instance of the application broadcasts a message to all top level windows and then terminates. On receiving the message the first instance of the application then shows it main form.
The code located in the project file for creating the mutex and determining if another instance of the program running is as follows.
var
vmutex : THandle;
begin
// Register GUID as windows message.
AppMessage := RegisterWindowMessage(AppGUID);
// Create application mutex with GUID
vmutex := CreateMutex(nil, True, AppGUID);
// If mutex already exists then broardcast windows message.
if GetLastError = ERROR_ALREADY_EXISTS then
begin
Windows.SendMessage(HWND_BROADCAST, AppMessage, 0, 0);
Exit;
end;
end;
And the handler within the main form is a override of the 'WndProc' routine.
procedure TMainForm.WndProc(var Msg: TMessage);
begin
if Msg.msg = AppMessage then
begin
MainForm.Show;
MainForm.ShowInTaskBar:= stDefault;
end;
inherited WndProc(Msg);
end;
I have tested the application on all major versions of Windows and it works fine, up to and including Windows 8.1, however on Windows 10, running the second instance of the application triggers the first's to show it's form as expected but does not terminate, it instead continues to run as a background task.
I have tried the alternatives, PostMessage and PostThreadMessage but these are ignored by the message handler, presumable because of ownership issues.
Please could anyone help with this problem as I cannot see what could be wrong and why it would only affect running on one of the 5 operating systems tested on.
Many thanks
scamb09