Skip to content

Commit a25acef

Browse files
committed
Issue 75: Autostart of Everything when it's not launched
#75 If Everything is not running then run it in background first.
1 parent 5512400 commit a25acef

3 files changed

Lines changed: 123 additions & 5 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Threading;
7+
8+
namespace EverythingToolbar.Helpers
9+
{
10+
public static class EverythingProcessHelper
11+
{
12+
private const string Dll64 = "Everything64.dll";
13+
14+
[DllImport(Dll64, EntryPoint = "Everything_QueryW", CharSet = CharSet.Unicode)]
15+
private static extern bool Everything64_QueryW(bool wait);
16+
17+
[DllImport(Dll64, EntryPoint = "Everything_IsDBLoaded")]
18+
private static extern bool Everything64_IsDBLoaded();
19+
20+
[DllImport(Dll64, EntryPoint = "Everything_GetLastError")]
21+
private static extern uint Everything64_GetLastError();
22+
23+
/// <summary>
24+
/// Checks if Everything is running and DB is loaded.
25+
/// </summary>
26+
public static bool IsRunning()
27+
{
28+
try
29+
{
30+
// Try a dummy query (empty string = no results)
31+
if (!Everything64_QueryW(false))
32+
return false;
33+
34+
return Everything64_IsDBLoaded();
35+
}
36+
catch
37+
{
38+
return false;
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Ensures Everything is running.
44+
/// If not, starts Everything.exe in background with -startup.
45+
/// Waits until DB is loaded or timeout expires.
46+
/// </summary>
47+
public static bool EnsureRunning(int timeoutMs = 5000)
48+
{
49+
if (IsRunning())
50+
{
51+
return true;
52+
}
53+
54+
string[] possiblePaths =
55+
{
56+
ToolbarSettings.User.EverythingPath,
57+
@"C:\Program Files\Everything\Everything.exe",
58+
@"C:\Program Files (x86)\Everything\Everything.exe",
59+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Everything\\Everything.exe"),
60+
};
61+
62+
var everythingPath = possiblePaths.FirstOrDefault(p => File.Exists(p));
63+
if (everythingPath == null)
64+
{
65+
return false;
66+
}
67+
68+
try
69+
{
70+
Process.Start(new ProcessStartInfo
71+
{
72+
FileName = everythingPath,
73+
Arguments = "-startup -first-instance",
74+
UseShellExecute = false,
75+
CreateNoWindow = true
76+
});
77+
78+
var sw = Stopwatch.StartNew();
79+
while (sw.ElapsedMilliseconds < timeoutMs)
80+
{
81+
if (IsRunning())
82+
return true;
83+
84+
Thread.Sleep(millisecondsTimeout: 10);
85+
}
86+
}
87+
catch (Exception)
88+
{
89+
// don't block. User can start Everything on their own as well.
90+
}
91+
92+
return false;
93+
}
94+
}
95+
}

EverythingToolbar/Search/SearchResultProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ private static void LogLastError()
394394
}
395395
}
396396

397-
private static bool Initialize()
397+
private bool Initialize()
398398
{
399399
SetInstanceName(ToolbarSettings.User.InstanceName);
400400

@@ -417,6 +417,7 @@ private static bool Initialize()
417417
{
418418
LogLastError();
419419
Logger.Error("Failed to get Everything version number.");
420+
this.IsBusy = true;
420421
}
421422
else
422423
{

EverythingToolbar/SearchWindow.xaml.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System;
1+
using EverythingToolbar.Helpers;
2+
using EverythingToolbar.Search;
3+
using System;
4+
using System.Threading;
25
using System.Windows;
36
using System.Windows.Input;
47
using System.Windows.Interop;
58
using System.Windows.Media;
69
using System.Windows.Media.Animation;
710
using System.Windows.Threading;
8-
using EverythingToolbar.Helpers;
9-
using EverythingToolbar.Search;
1011

1112
namespace EverythingToolbar
1213
{
@@ -29,6 +30,27 @@ private SearchWindow()
2930

3031
private void OnActivated(object? sender, EventArgs e)
3132
{
33+
// check if Everything.exe process is running or not. If it is not running then start it in background.
34+
Dispatcher.BeginInvoke(new Action(() =>
35+
{
36+
if (!EverythingProcessHelper.IsRunning())
37+
{
38+
EverythingProcessHelper.EnsureRunning();
39+
40+
Dispatcher.Invoke(() =>
41+
{
42+
// Triggers search again after the start of Everything process.
43+
var textBox = (SearchBox.FindName("TextBox") as System.Windows.Controls.TextBox);
44+
if (textBox != null)
45+
{
46+
textBox.Text = " ";
47+
textBox.Text = "";
48+
}
49+
});
50+
}
51+
52+
}), DispatcherPriority.Background);
53+
3254
if (TaskbarStateManager.Instance.IsIcon)
3355
EventDispatcher.Instance.InvokeSearchBoxFocused(this, EventArgs.Empty);
3456

@@ -57,7 +79,7 @@ private void OnLostKeyboardFocus(object? sender, KeyboardFocusChangedEventArgs e
5779
{
5880
if (e.NewFocus == null) // New focus outside application
5981
{
60-
Hide();
82+
Hide();
6183
}
6284
}
6385

0 commit comments

Comments
 (0)