Browse Source

Fixes issue 2456: Restart self on code 42 exit status

tags/0.6.3m1rc1
Michael 15 years ago
parent
commit
8cdf5f5c06
1 changed files with 46 additions and 2 deletions
  1. 46
    2
      launcher/windows/DMDirc.dpr

+ 46
- 2
launcher/windows/DMDirc.dpr View File

@@ -49,9 +49,27 @@ var
49 49
   i: integer;
50 50
   jarName: String;
51 51
   launcherUpdate: boolean = false;
52
+  result: integer;
53
+  s: string;
54
+  handle: thandle;
52 55
 begin
53 56
   InitCommonControls;
54 57
 
58
+  { Check for a mutex created by the DMDirc update process. Wait until it no
59
+    longer exists before we continue. This prevents us from proceeding if any
60
+    previous instance of the launcher is shutting down. }
61
+  handle := OpenMutex(SYNCHRONIZE, False, 'DMDirc_Launcher_Restart_Wait');
62
+  if handle <> 0 then begin
63
+    { The mutex exists. Wait for it for up to 5 seconds. }
64
+    if WaitForSingleObject(handle, 5000) = WAIT_TIMEOUT then begin
65
+      { Timed out - we cannot continue, there is some kind of serious issue? }
66
+      showError('Internal error: Timed out waiting for previous instance of launcher to stop during restart upgrade', 'DMDirc', true, true);
67
+      exit;
68
+    end;
69
+    { If we get to here, the mutex has been released and we can continue }
70
+    CloseHandle(handle);
71
+  end;
72
+
55 73
   jarName := ExtractFileDir(paramstr(0))+'\DMDirc.jar';
56 74
 
57 75
   directory := GetEnvironmentVariable('APPDATA')+'\DMDirc';
@@ -92,7 +110,7 @@ begin
92 110
     end
93 111
     else RunProgram('"'+ExtractFileDir(paramstr(0))+'\DMDircUpdater.exe" --UpdateSourceDir "'+directory+'"', not launcherUpdate);
94 112
   end;
95
-  
113
+
96 114
   if not launcherUpdate then begin
97 115
     // Check JVM
98 116
     if (ExecAndWait(javaCommand+' -version') <> 0) then begin
@@ -114,7 +132,33 @@ begin
114 132
         showError(errorMessage, 'DMDirc', True);
115 133
       end
116 134
       else begin
117
-        Launch(javaCommand+' -ea -jar "'+jarName+'"'+' -l windows-'+launcherVersion+' '+cliParams)
135
+        //Launch(javaCommand+' -ea -jar "'+jarName+'"'+' -l windows-'+launcherVersion+' '+cliParams)
136
+        { Need to wait so we can deal with exit code 42 to restart }
137
+        result := ExecAndWait(javaCommand+' -ea -jar "'+jarName+'"'+' -l windows-'+launcherVersion+' '+cliParams);
138
+        if result = 42 then begin
139
+          { Need to restart DMDirc launcher
140
+            We can't just rerun the EXE because it is possible for the new
141
+            process to launch the updater and try to replace this EXE before
142
+            the current thread ends - remote but possible. We deal with this
143
+            case by creating a mutex that this program will spin on when it
144
+            restarts until this instance ends. }
145
+          handle := CreateMutex(nil, True, 'DMDirc_Launcher_Restart_Wait');
146
+          if handle = 0 then begin
147
+            showError('Internal error: Failed to create restart mutex', 'DMDirc', true, true);
148
+            exit;
149
+          end;
150
+
151
+          { Build new command line }
152
+          s := '';
153
+          for i := 1 to paramcount do begin
154
+            if pos(' ', paramstr(i)) <> 0 then s := s + '"' + paramstr(i) + '"' else
155
+              s := s + paramstr(i);
156
+            if i < paramcount then s := s + ' ';
157
+          end;
158
+
159
+          { Launch self }
160
+          Launch('"' + paramstr(0)+ '" ' + s);
161
+        end;
118 162
       end;
119 163
     end
120 164
     else begin

Loading…
Cancel
Save