-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
1104 lines (1009 loc) · 41.2 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32.TaskScheduler;
using System.Security.Permissions;
using System.Security.Principal;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId);
[DllImport("psapi.dll")]
static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);
public int path_list_int;
List<string> path_list = new List<string>();
public int name_list_int;
List<string> name_list = new List<string>();
public static string Selected_row_proc;
public int Selected_row_proc_int;
public string slider_cache;
public bool rewrited;
public bool rewrited_list_async = true;
public string text = "";
public string cache;
public string old_rewrited_string;
public string return_value;
public int track_value;
public string test;
public string path2;
public string list_value_check;
public static string thread_name;
public string current_checked_path;
public static bool file_changes = false;
public string text_list_cache;
public static string path;
public static int value;
public static bool restart_bool;
public bool start_up_window_change = false;
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
TaskService ts;
//LANG MANAGER
private string lang_basic_01;
private string lang_basic_02;
private string lang_basic_03;
private string lang_basic_04;
private string lang_basic_05;
private string lang_basic_06;
private string lang_basic_07;
private string lang_basic_08;
private string lang_basic_09;
private string lang_basic_10;
private string lang_error_01;
private string lang_error_02;
private string lang_error_03;
private string lang_error_04;
private string lang_error_05;
private string lang_error_06;
private string lang_error_07;
private string lang_error_08;
private string lang_error_09;
private string lang_error_10;
public Form1()
{
InitializeComponent();
notifyIcon2.Text = "";
System.Threading.Tasks.Task.Run(Check_USAGE_CPU);
try
{
lang_manager(System.Globalization.CultureInfo.InstalledUICulture.ThreeLetterISOLanguageName.ToString());
}catch(Exception)
{
}
comboBox1.Text = "🌐";
comboBox1.Items.Add("DEU| Deutsch 🌐");
comboBox1.Items.Add("ENG| English 🌐");
button1.Text = lang_basic_09;
label1.Hide();
label2.Text = "";
checkBox1.Hide();
checkBox3.Text = lang_basic_01;
trackBar1.Hide();
trackBar1.ResetText();
checkBox2.Checked = false;
List_Box();
if (CheckAdmin())
{
button2.Hide();
}
else
{
button2.Show();
}
try {
using (ts = new TaskService())
{
try
{
Microsoft.Win32.TaskScheduler.Task get_task = ts.GetTask("Stop_High_CPU_usage");
if (get_task.Enabled)
{
checkBox3.Checked = true;
checkBox2.Checked = true;
}
}
catch (Exception)
{
checkBox3.Checked = false;
}
}
checkBox2.Text = lang_basic_02;
if (rkApp.GetValue("Stop_High_CPU_usage") == null)
{
if (checkBox2.Checked == true)
{
}
else
{
checkBox2.Checked = false;
}
}
else
{
if (checkBox3.Checked == true)
{
rkApp.DeleteValue("Stop_High_CPU_usage");
}
else
{
if (rkApp.GetValue("Stop_High_CPU_usage").ToString() != Application.ExecutablePath.ToString()) ;
{
rkApp.SetValue("Stop_High_CPU_usage", Application.ExecutablePath);
}
}
checkBox2.Checked = true;
}
}catch(Exception)
{
}
}
public static string Get_Process(int pid)
{
var processHandle = OpenProcess(0x0400 | 0x0010, false, pid);
if (processHandle == IntPtr.Zero)
{
return null;
}
const int lengthSb = 4000;
var sb = new StringBuilder(lengthSb);
string result = null;
if (GetModuleFileNameEx(processHandle, IntPtr.Zero, sb, lengthSb) > 0)
{
result = sb.ToString();
}
CloseHandle(processHandle);
return result;
}
public void List_Box()
{
if (rewrited_list_async == true)
{
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Process_list_config.cfg");
if (File.Exists(path))
{
text = File.ReadAllText(path);
}
else
{
FileStream fileStream = File.Create(path);
fileStream.Close();
}
text_list_cache = text;
rewrited_list_async = false;
}
bool add_to_list_allowed = false;
listView1.Items.Clear();
path_list.Clear();
name_list.Clear();
int x = 0;
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
try
{
x++;
add_to_list_allowed = false;
string path = "";
ListViewItem list = new ListViewItem(theprocess.ProcessName);
list.SubItems.Add(theprocess.Id.ToString());
list.Name = theprocess.ProcessName;
list.SubItems.Add(theprocess.MainWindowTitle);
Process process = Process.GetProcessById(theprocess.Id);
try
{
path = Get_Process(theprocess.Id);
}
catch
{
}
if (path == null)
{
name_list_int = 0;
if (name_list != null)
{
string[] name_list_array = name_list.ToArray();
foreach (string name_list_string in name_list_array)
{
if (name_list_string == theprocess.ProcessName)
{
name_list_int = 1;
}
}
}
else
{
name_list_int = 0;
}
if (name_list_int == 0)
{
list.ForeColor = Color.FromArgb(100, 200, 85, 0);
path = lang_error_01;
list.SubItems.Add(path);
try
{
name_list.Add(theprocess.ProcessName);
}
catch
{
}
add_to_list_allowed = true;
}
else
{
add_to_list_allowed = false;
}
}
else
{
if (path_list != null)
{
path_list_int = 0;
string[] path_list_array = path_list.ToArray();
foreach (string path_list_string in path_list_array)
{
if (path_list_string == path)
{
path_list_int = 1;
}
}
}
else
{
path_list_int = 0;
}
if (path_list_int == 0)
{
try
{
path_list.Add(path);
}
catch
{
}
list.SubItems.Add(path);
add_to_list_allowed = true;
list.SubItems.Add(path);
string[] tokens;
tokens = text_list_cache.Split('\n');
foreach (string token in tokens)
{
int x_int = 0;
string[] list_token = token.Split(',');
try
{
foreach (string list_p in list_token)
{
if (x_int == 0)
{
thread_name = list_p;
}
if (x_int == 1)
{
path2 = list_p;
}
if (x_int == 2)
{
Int32.TryParse(list_p, out value);
}
if (x_int == 3)
{
if (path == path2)
{
if (value != 0)
{
list.BackColor = Color.FromArgb(100, 0, 189, 0);
}
}
}
x_int++;
}
}
catch (Exception)
{
}
}
}
else
{
add_to_list_allowed = false;
}
}
if (add_to_list_allowed == true)
{
listView1.Items.Add(list);
}
}
catch (Exception)
{
}
}
}
public static void ExecuteAsAdmin(string fileName, bool admin)
{
Process proc = new Process();
proc.StartInfo.FileName = fileName;
if (admin == true)
{
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.Verb = "runas";
}
proc.Start();
proc.Close();
}
public void Check(string pfad, string thread_name)
{
while (true)
{
using (PerformanceCounter process_cpu = new PerformanceCounter("Process", "% Processor Time", thread_name))
{
var process_cpu_usage_start = process_cpu.NextValue();
System.Threading.Thread.Sleep(1000);
var process_cpu_usage = process_cpu.NextValue();
var process_cpu_usage_new = process_cpu.NextValue();
if (process_cpu_usage > 1000)
{
try
{
foreach (Process proc in Process.GetProcessesByName(thread_name))
{
proc.Kill();
}
}
catch (Exception)
{
}
bool admin = true;
ExecuteAsAdmin(pfad, admin);
}
}
}
}
private void notifyIcon2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
private void ImportStatusForm_Resize(object sender, EventArgs e)
{
//Debug.WriteLine("mini" + this.WindowState);
if (this.WindowState == FormWindowState.Minimized)
{
if (ShowInTaskbar == true)
{
ShowInTaskbar = false;
}
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
trackBar1.Value = 0;
foreach (ListViewItem test_debug in listView1.SelectedItems)
{
// Debug.WriteLine(test_debug.Name);
string index = test_debug.Name;
try
{
bool error = true;
// istViewItem list = new ListViewItem("Debug");
//list.SubItems.Add(index);
//listView2.Items.Add(list);
foreach (Process proc in Process.GetProcessesByName(index))
{
label2.Show();
error = false;
checkBox1.Enabled = true;
checkBox1.Show();
checkBox1.Text = lang_basic_04;
label1.Show();
label1.ResetText();
label2.ResetText();
label2.Text = lang_basic_05;
if (proc.MainWindowTitle != "")
{
label1.Text = lang_basic_03 + proc.ProcessName + " (" + proc.MainWindowTitle + ")";
}
else
{
label1.Text = lang_basic_03 + proc.ProcessName;
}
Selected_row_proc_int = proc.Id;
checkBox1.Checked = false;
return_value = null;
current_checked_path = Get_Process(proc.Id);
if (current_checked_path == null)
{
checkBox1.Enabled = false;
}
else
{
//Set string return_value
get_file(current_checked_path);
}
trackBar1.Show();
trackBar1.Enabled = true;
trackBar1.Value = 0;
if (return_value != null)
{
string[] return_array = return_value.Split(',');
int x = 0;
foreach (string return_string in return_array)
{
if (x == 3)
{
checkBox1.Checked = Convert.ToBoolean(return_string);
test = return_string;
}
if (x == 2)
{
int track_value = 0;
Int32.TryParse(return_string, out track_value);
trackBar1.Value = track_value;
if (track_value != 0)
{
label2.Text = Process.GetProcessById(Selected_row_proc_int).ProcessName + lang_basic_06 + track_value + lang_basic_07;
}
}
x++;
}
}
}
if(error == true)
{
label1.Text = lang_basic_03 + index;
checkBox1.Show();
checkBox1.Text = lang_basic_04;
checkBox1.Enabled = false;
label1.Show();
label1.ResetText();
label2.ResetText();
label2.Text = lang_error_02;
}
}
catch (Exception)
{
label1.Text = lang_basic_03 + index;
checkBox1.Show();
checkBox1.Text = lang_basic_04;
checkBox1.Enabled = false;
label1.Show();
label2.Text = lang_error_02;
}
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
int a = trackBar1.Value;
if (a == 0)
{
label2.Text = lang_basic_05;
}
else
{
try
{
if (checkBox1.Checked)
{
label2.Text = Process.GetProcessById(Selected_row_proc_int).ProcessName + lang_basic_06 + a.ToString() + lang_basic_07;
}
else
{
label2.Text = Process.GetProcessById(Selected_row_proc_int).ProcessName + lang_basic_06 + a.ToString() + lang_basic_08;
}
}
catch (Exception)
{
label2.Text = lang_error_02;
}
}
if (Get_Process(Selected_row_proc_int) != null)
{
save(Selected_row_proc_int, a, checkBox1.Checked);
}
else
{
trackBar1.Enabled = false;
checkBox1.Enabled = false;
label2.Text = lang_error_03;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
public void save(int proc_id, int value_slider, bool restart)
{
if (Get_Process(proc_id) != Application.ExecutablePath)
{
try
{
if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Process_list_config.cfg")))
{
}
else
{
}
bool rewrite = false;
rewrited = false;
text = File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Process_list_config.cfg"));
string[] tokens = text.Split('\n');
foreach (string token in tokens)
{
rewrite = false;
int x_int = 0;
string[] list_token = token.Split(',');
foreach (string list_p in list_token)
{
if (x_int == 0)
{
cache = list_p;
}
if (x_int == 1)
{
if (list_p == Get_Process(proc_id))
{
cache = cache + "," + Get_Process(proc_id);
rewrite = true;
}
}
if (x_int == 2)
{
slider_cache = list_p;
}
if (x_int == 3)
{
if (rewrite == true)
{
old_rewrited_string = cache + "," + slider_cache + "," + list_p;
text = text.Replace(old_rewrited_string, Process.GetProcessById(proc_id).ProcessName + "," + Get_Process(proc_id) + "," + value_slider.ToString() + "," + restart.ToString());
rewrited = true;
}
}
x_int++;
}
}
//DEBUG
//ListViewItem list1 = new ListViewItem("Rewrite_old");
//list1.SubItems.Add(old_rewrited_string);
//listView2.Items.Add(list1);
//ListViewItem list = new ListViewItem("Rewrite");
//list.SubItems.Add(Process.GetProcessById(proc_id).ProcessName + "," + Process.GetProcessById(proc_id).MainModule.FileName + "," + value_slider.ToString() + "," + restart.ToString());
//listView2.Items.Add(list);
if (rewrited == false)
{
text = text + "\n" + Process.GetProcessById(proc_id).ProcessName + "," + Get_Process(proc_id) + "," + value_slider.ToString() + "," + restart;
}
File.WriteAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Process_list_config.cfg"), text);
file_changes = true;
rewrited_list_async = true;
}
catch (Exception)
{
}
}
}
public void get_file(string path)
{
if (path != Application.ExecutablePath)
{
text = File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Process_list_config.cfg"));
string[] tokens = text.Split('\n');
bool rewrite;
foreach (string token in tokens)
{
rewrite = false;
int x_int = 0;
string[] list_token = token.Split(',');
foreach (string list_p in list_token)
{
if (x_int == 0)
{
cache = list_p;
}
if (x_int == 1)
{
if (list_p == path)
{
cache = cache + "," + path;
rewrite = true;
}
}
if (x_int == 2)
{
slider_cache = list_p;
}
if (x_int == 3)
{
if (rewrite == true)
{
return_value = cache + "," + slider_cache + "," + list_p;
return;
}
}
x_int++;
}
}
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (sender != null)
{
return_value = null;
get_file(current_checked_path);
if (return_value != null)
{
string[] return_array = return_value.Split(',');
int x = 0;
foreach (string return_string in return_array)
{
if (x == 2)
{
int track_value = 0;
Int32.TryParse(return_string, out track_value);
try
{
if (checkBox1.Checked)
{
label2.Text = Process.GetProcessById(Selected_row_proc_int).ProcessName + lang_basic_06 + track_value + lang_basic_07;
}
else
{
label2.Text = Process.GetProcessById(Selected_row_proc_int).ProcessName + lang_basic_06 + track_value + lang_basic_08;
}
}
catch
{
trackBar1.Enabled = false;
checkBox1.Enabled = false;
label2.Text = lang_error_02;
}
if (Get_Process(Selected_row_proc_int) != null)
{
save(Selected_row_proc_int, track_value, checkBox1.Checked);
}
else
{
trackBar1.Enabled = false;
checkBox1.Enabled = false;
label2.Text = lang_error_03;
}
}
x++;
}
}
else
{
if (Get_Process(Selected_row_proc_int) != null)
{
save(Selected_row_proc_int, track_value, checkBox1.Checked);
}
else
{
trackBar1.Enabled = false;
checkBox1.Enabled = false;
label2.Text = lang_error_03;
}
}
}
}
static async System.Threading.Tasks.Task Check_USAGE_CPU()
{
string text = File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Process_list_config.cfg"));
file_changes = false;
string[] tokens;
while (true)
{
try
{
if (Form1.file_changes == true)
{
text = File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Process_list_config.cfg"));
}
else
{
System.Threading.Thread.Sleep(1000);
}
tokens = text.Split('\n');
foreach (string token in tokens)
{
int x_int = 0;
string[] list_token = token.Split(',');
foreach (string list_p in list_token)
{
if (x_int == 0)
{
thread_name = list_p;
}
if (x_int == 1)
{
path = list_p;
}
if (x_int == 2)
{
Int32.TryParse(list_p, out value);
}
if (x_int == 3)
{
if (value != 0)
{
restart_bool = Convert.ToBoolean(list_p);
using (PerformanceCounter process_cpu = new PerformanceCounter("Process", "% Processor Time", thread_name))
{
//Debug.WriteLine(thread_name + " wird geprüft");
var process_cpu_usage_start = process_cpu.NextValue();
System.Threading.Thread.Sleep(1000);
var process_cpu_usage = process_cpu.NextValue() / Environment.ProcessorCount;
//Debug.WriteLine(thread_name + " hat eine Auslastung von " + process_cpu_usage + "% Core " + Environment.ProcessorCount);
var process_cpu_usage_new = process_cpu.NextValue();
if (process_cpu_usage > value)
{
try
{
foreach (Process proc in Process.GetProcessesByName(thread_name))
{
proc.Kill();
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception)
{
}
if (restart_bool)
{
ExecuteAsAdmin(Form1.path, true);
}
}
}
}
}
x_int++;
}
}
}
catch (Exception)
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Lädt...";
List_Box();
button1.Text = lang_basic_09;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
if (checkBox3.Checked == false)
{
rkApp.SetValue("Stop_High_CPU_usage", Application.ExecutablePath);
}
else
{
}
}
else
{
if (checkBox3.Checked)
{
using (ts = new TaskService())
{
try
{
ts.RootFolder.DeleteTask("Stop_High_CPU_usage");
try
{
Microsoft.Win32.TaskScheduler.Task get_task = ts.GetTask("Stop_High_CPU_usage");
if (get_task.Enabled)
{
checkBox3.Checked = true;
checkBox2.Checked = true;
}
}
catch (Exception)
{
checkBox3.Checked = false;
}
}
catch (Exception)
{
checkBox3.Checked = true;
checkBox2.Checked = true;
checkBox3.Enabled = false;
checkBox2.Enabled = false;
checkBox3.Text = lang_error_04;
checkBox3.BackColor = Color.FromArgb(10, 218, 142, 61);
checkBox2.BackColor = Color.FromArgb(10, 218, 142, 61);
}
}
}
rkApp.DeleteValue("Stop_High_CPU_usage", false);
}
}
private void checkBox3_CheckedChanged_1(object sender, EventArgs e)
{
if (checkBox3.Checked == false)
{
rkApp.SetValue("Stop_High_CPU_usage", Application.ExecutablePath);
try
{
ts.RootFolder.DeleteTask("Stop_High_CPU_usage");
try
{
Microsoft.Win32.TaskScheduler.Task get_task = ts.GetTask("Stop_High_CPU_usage");
if (get_task.Enabled)
{
checkBox3.Checked = true;
checkBox2.Checked = true;
}
}
catch (Exception)
{
checkBox3.Checked = false;
}
}
catch
{
checkBox3.Checked = true;
checkBox2.Checked = true;
checkBox3.Enabled = false;
checkBox2.Enabled = false;
checkBox3.Text = lang_error_04;
checkBox3.BackColor = Color.FromArgb(10, 218, 142, 61);
checkBox2.BackColor = Color.FromArgb(10, 218, 142, 61);
}
}
else
{
try
{
Microsoft.Win32.TaskScheduler.Task get_task = ts.GetTask("Stop_High_CPU_usage");
if (get_task.Enabled)
{
checkBox3.Checked = true;
checkBox2.Checked = true;
}
}
catch (Exception)
{
using (ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Autostart for Stop_High_CPU_usage";
// Create a trigger that will fire the task at this time every other day
td.Triggers.Add(new LogonTrigger { });
td.Principal.RunLevel = TaskRunLevel.Highest;
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction(Application.ExecutablePath, "", null));
// Register the task in the root folder
try
{
ts.RootFolder.RegisterTaskDefinition("Stop_High_CPU_usage", td);
rkApp.DeleteValue("Stop_High_CPU_usage", false);
checkBox3.Checked = true;
checkBox2.Checked = true;
}
catch (System.UnauthorizedAccessException)
{
checkBox3.Enabled = false;
checkBox3.Checked = false;
checkBox3.Text = lang_error_04;
checkBox3.BackColor = Color.FromArgb(10, 150, 62, 0);
}
// Remove the task we just created
//ts.RootFolder.("Test");