Bonjour à tous,
J'ai fais une application avec une windows form qui détecte l'activité de l'utilisateur sur l'écran;
Je voudrais transformer cette application en service.
J'ai pensée à mettre ma clase MainForm dans mon service et à créer une nouvelle instance de la classe mais cela ne fonctionne pas.
Comment puis je faire pour migrer mon appli?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.Button CreateCtrlMonButton; private System.ComponentModel.Container components = null; private System.Windows.Forms.TextBox OutputTextBox; private System.Windows.Forms.Button DisposeMonButton; private System.Windows.Forms.Button NullMonVarButton; private System.Windows.Forms.Button EnableMonButton; private System.Windows.Forms.Button DisableMonButton; private System.Windows.Forms.Button TriggerCGButton; private System.Windows.Forms.GroupBox CreateOptsGroup; private System.Windows.Forms.ComboBox MonTypeComboBox; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox IntervalTextBox; private System.Windows.Forms.CheckBox SyncCheckBox; private System.Windows.Forms.CheckBox MonKeyboardCheckBox; private System.Windows.Forms.CheckBox MonMouseCheckBox; private System.Windows.Forms.Button ShowMsgBoxButton; private System.Windows.Forms.Button ShowOtherWindowButton; private System.Windows.Forms.CheckBox ModalWndCheckBox; private IInactivityMonitor inactivityMonitor = null; public MainForm() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } #region Vom Windows Form-Designer generierter Code private void InitializeComponent() { this.SuspendLayout(); // // MainForm // this.ClientSize = new System.Drawing.Size(284, 262); this.Name = "MainForm"; this.ShowIcon = true; this.ShowInTaskbar = true; this.TransparencyKey = System.Drawing.Color.Transparent; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; this.ResumeLayout(false); inactivityMonitor = MonitorCreator.CreateInstance(MonitorType.GlobalHookMonitor); inactivityMonitor.SynchronizingObject = null; inactivityMonitor.MonitorKeyboardEvents = true; inactivityMonitor.MonitorMouseEvents = true; inactivityMonitor.Interval = 1000; inactivityMonitor.Elapsed += new ElapsedEventHandler(TimeElapsed); inactivityMonitor.Reactivated += new EventHandler(Reactivated); inactivityMonitor.Enabled = true; } #endregion [STAThread] static void Main() { try { Application.Run(new MainForm()); } catch (Exception e) { MessageBox.Show(e.Message); } } private void TimeElapsed(object sender, ElapsedEventArgs e) { try { if (this.InvokeRequired) { Process[] myProcesses; // Returns array containing all instances of Solitaire myProcesses = Process.GetProcessesByName("Calc"); if (myProcesses.Length > 0) { foreach (Process myProcess in myProcesses) { myProcess.Kill(); } // RestartService(this.ServiceName, 5000); } } // WriteOutput("Event 'Elapsed' occured (Invoke() would have been required)"); else { { Process[] myProcesses; // Returns array containing all instances of Solitaire myProcesses = Process.GetProcessesByName("Calc"); if (myProcesses.Length > 0) { foreach (Process myProcess in myProcesses) { myProcess.Kill(); } // RestartService(this.ServiceName, 5000); } } // WriteOutput("Event 'Elapsed' occured (synchronized call)"); } } catch (Exception exception) { // WriteOutput("Exception occured: " + exception.Message); } } private void Reactivated(object sender, EventArgs e) { try { /* if (this.InvokeRequired) {} // WriteOutput("Event 'Reactivated' occured (Invoke() would have been required)"); else { } // WriteOutput("Event 'Reactivated' occured (synchronized call)");*/ } catch (Exception exception) { // WriteOutput("Exception occured: " + exception.Message); } } }
Merci
Partager