Created
July 15, 2017 00:07
-
-
Save tmori3y2/6cbdfbbc38fb4e085b650c0f57cb2044 to your computer and use it in GitHub Desktop.
LinqPad Script to avoid the sleep mode on running DISM command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\WPF\PresentationCore.dll</Reference> | |
<Reference><RuntimeDirectory>\WPF\PresentationFramework.dll</Reference> | |
<Reference><RuntimeDirectory>\WPF\PresentationUI.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Threading.Tasks.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Xaml.dll</Reference> | |
<Reference><RuntimeDirectory>\WPF\WindowsBase.dll</Reference> | |
<Namespace>System.ComponentModel</Namespace> | |
<Namespace>System.Diagnostics</Namespace> | |
<Namespace>System.Runtime.InteropServices</Namespace> | |
<Namespace>System.Threading.Tasks</Namespace> | |
<Namespace>System.Windows</Namespace> | |
<Namespace>System.Windows.Controls</Namespace> | |
</Query> | |
[Flags] | |
enum EXECUTION_STATE : uint | |
{ | |
ES_SYSTEM_REQUIRED = 0x00000001, | |
ES_DISPLAY_REQUIRED = 0x00000002, | |
ES_AWAYMODE_REQUIRED = 0x00000040, | |
ES_CONTINUOUS = 0x80000000, | |
ES_DONE = ES_CONTINUOUS, | |
ES_KEEP_ALIVE = ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_AWAYMODE_REQUIRED | ES_CONTINUOUS, | |
} | |
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] | |
static extern uint SetThreadExecutionState(EXECUTION_STATE esFlags); | |
public void Main() | |
{ | |
new MonitoringWindow().Show(); | |
} | |
public class MonitoringWindow : Window | |
{ | |
Button startButton = new Button { Content = "Start", Margin = new Thickness(5) }; | |
Button cancelButton = new Button { Content = "Cancel", Margin = new Thickness(5), IsEnabled = false }; | |
TextBlock resultText = new TextBlock { Margin = new Thickness(5) }; | |
StackPanel stackPanel = new StackPanel(); | |
CancellationTokenSource cancelSource = null; | |
public MonitoringWindow() | |
{ | |
InitializeComponent(); | |
} | |
async void startButton_Click(object sender, EventArgs args) | |
{ | |
startButton.IsEnabled = false; | |
cancelButton.IsEnabled = true; | |
SetThreadExecutionState(EXECUTION_STATE.ES_DONE); | |
resultText.Text = "Waiting..."; | |
cancelSource = new CancellationTokenSource(); | |
var token = cancelSource.Token; | |
try | |
{ | |
while (true) | |
{ | |
bool foundDism = false; | |
var processes = Process.GetProcesses(); | |
foreach (var process in processes) | |
{ | |
if (process.ProcessName.ToLower() == "dism") | |
{ | |
foundDism = true; | |
SetThreadExecutionState(EXECUTION_STATE.ES_KEEP_ALIVE); | |
resultText.Text = "Dism found...\n"; | |
resultText.Text += $"Id\t\t: {process.Id}\n"; | |
resultText.Text += $"StartTime\t: {process.StartTime}\n"; | |
resultText.Text += $"Now\t\t: {DateTime.Now}\n"; | |
break; | |
} | |
token.ThrowIfCancellationRequested(); | |
} | |
if (!foundDism) | |
{ | |
resultText.Text = "Finished."; | |
break; | |
} | |
await Task.Delay(5000, token); | |
} | |
} | |
catch (OperationCanceledException) | |
{ | |
resultText.Text = "Monintoring Canceled"; | |
} | |
catch (Exception ex) | |
{ | |
resultText.Text = "Error: " + ex.Message; | |
} | |
finally | |
{ | |
startButton.IsEnabled = true; | |
cancelButton.IsEnabled = false; | |
SetThreadExecutionState(EXECUTION_STATE.ES_DONE); | |
cancelSource = null; | |
} | |
} | |
void InitializeComponent() | |
{ | |
Title = "Dism keeper"; | |
startButton.Click += startButton_Click; | |
cancelButton.Click += (sender, e) => cancelSource.Cancel(); | |
Width = Height = 400; | |
Padding = new Thickness(15); | |
stackPanel.Children.Add(startButton); | |
stackPanel.Children.Add(cancelButton); | |
stackPanel.Children.Add(resultText); | |
Content = stackPanel; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment