FrmPvsServer.cs
8.29 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace SunVoteARSPPT
{
public delegate void OnPvsBaseConnectEvent(int ID, int iStatus);
public delegate void OnPvsBaseEvent(int ID, int iMode, string sInfo);
public delegate void OnPvsKeyEvent(int ID, int iMode, string sInfo);
public delegate void OnPvsKeyVote(int ID, int iMode, string sInfo);
public delegate void OnPvsCommErrSys(string ID, int ErrNo, string ErrInfo);
public delegate void OnPvsCommErrEx(string ID, int ErrNo, string ErrInfo);
public partial class FrmPvsServer : Form
{
public FrmPvsServer()
{
InitializeComponent();
}
private static FrmPvsServer frmPvs = null;
public static PVSServer.MyPVS GetMyPvs()
{
return GetFrmPvs().InitPvs();
}
public static FrmPvsServer GetFrmPvs()
{
if (frmPvs == null)
{
frmPvs = new FrmPvsServer();
frmPvs.Show();
}
return frmPvs;
}
/// <summary>
/// 释放PVS,杨斌 2015-04-14
/// </summary>
public static void FreeMyPvs()
{
if (frmPvs != null)
{
frmPvs.PvsStop();
frmPvs.PVS.BaseConnectClose();
frmPvs.PVS = null;
frmPvs.Close();
frmPvs = null;
KillProcess("PVSServer");
GC.Collect();
}
}
public static void KillProcess(string processName)
{
try
{
foreach (Process thisproc in Process.GetProcessesByName(processName))//循环查找
{
//if (!thisproc.CloseMainWindow())
{
thisproc.Kill();
}
}
}
catch { }
}
private PVSServer.MyPVS PVS = null;
public OnPvsBaseConnectEvent OnBaseConnectEvent = null;
public OnPvsBaseEvent OnBaseEvent = null;
public OnPvsKeyEvent OnKeyEvent = null;
public OnPvsKeyVote OnKeyVote = null;
public OnPvsCommErrSys OnCommErrSys = null;
public OnPvsCommErrEx OnCommErrEx = null;
private void FrmPvsServer_Load(object sender, EventArgs e)
{
this.Hide();
this.ShowInTaskbar = false;
}
private PVSServer.MyPVS InitPvs()
{
if ((PVS == null) && (IntPtr.Size == 4))
{
PVS = new PVSServer.MyPVS();
PVS.BaseEvent += PVS_BaseEvent;
PVS.BaseConnectEvent += PVS_BaseConnectEvent;
PVS.CommErrEx += PVS_CommErrEx;
PVS.CommErrSys += PVS_CommErrSys;
PVS.KeyEvent += PVS_KeyEvent;
PVS.KeyVote += PVS_KeyVote;
tmrGet.Elapsed += tmrGet_Elapsed;
//tmrGet.Tick += tmrGet_Tick;
}
return PVS;
}
private void FrmPvsServer_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
PvsStop();//杨斌 2015-04-14
PVS = null;
}
catch { }
}
void PVS_BaseConnectEvent(int ID, int iStatus)
{
if (OnBaseConnectEvent != null)
this.BeginInvoke(OnBaseConnectEvent, ID, iStatus);
//this.BeginInvoke(new OnPvsBaseConnectEvent(PVS_PvsOnBaseConnectEvent), ID, iStatus);
}
void PVS_BaseEvent(int ID, int iMode, string sInfo)
{
if (OnBaseEvent != null)
this.BeginInvoke(OnBaseEvent, ID, iMode, sInfo);
}
void PVS_KeyEvent(int ID, int iMode, string sInfo)
{
if (OnKeyEvent != null)
this.BeginInvoke(OnKeyEvent, ID, iMode, sInfo);
}
/// <summary>
/// 收到按键值缓冲。避免卡死。杨斌 2015-03-27
/// </summary>
private List<KeyVoteRes> LstGetKeyVote = new List<KeyVoteRes>();
void PVS_KeyVote(int ID, int iMode, string sInfo)
{
//杨斌 2015-03-27
//if (OnKeyVote != null)
// this.BeginInvoke(OnKeyVote, ID, iMode, sInfo);
if (VoteModifyMode == 0)//不允许修改,PVS键盘不支持,软件屏蔽。杨斌 2015-11-10
{
string sID = ID.ToString();
if (GlobalInfo.response.ResponseDataList.Contains(sID))
{
if (!string.IsNullOrEmpty(GlobalInfo.response.ResponseDataList[sID].KeyValue))
return;
}
if (LstGetKeyVote.Exists(o => o.ID == ID))
{
return;
}
}
LstGetKeyVote.Add(new KeyVoteRes() { ID = ID, iMode = iMode, sInfo = sInfo });
if (!tmrGet.Enabled)
{
tmrGet.Interval = 10;
tmrGet.Enabled = true;
}
}
void PVS_CommErrSys(string ID, int ErrNo, string ErrInfo)
{
if (OnCommErrSys != null)
this.BeginInvoke(OnCommErrSys, ID, ErrNo, ErrInfo);
}
void PVS_CommErrEx(string ID, int ErrNo, string ErrInfo)
{
if (OnCommErrEx != null)
this.BeginInvoke(OnCommErrEx, ID, ErrNo, ErrInfo);
}
private int VoteModifyMode = 1;
public void VoteStart(int voteMode, string voteSetting, int iModifyMode = 1)
{
VoteModifyMode = iModifyMode;
Action<object> action = (obj) => PvsStart(voteMode, voteSetting);
action.BeginInvoke(this, ar => action.EndInvoke(ar), null);
}
public void VoteStop()
{
Action<object> action = (obj) => PvsStop();
action.BeginInvoke(this, ar => action.EndInvoke(ar), null);
}
private void PvsStart(int voteMode, string voteSetting)
{
if (PVS != null)
{
PVS.VoteMode(voteMode, voteSetting);
PVS.VoteNew();
PVS.VoteStart();
}
}
private void PvsStop()
{
if (PVS != null)
{
PVS.VoteStop();
tmrGet.Stop();
//IsRunGet = false;
//GetData(9999);
}
}
System.Timers.Timer tmrGet = new System.Timers.Timer();
//System.Windows.Forms.Timer tmrGet = new System.Windows.Forms.Timer();
private bool IsRunGet = false;
//private void tmrGet_Tick(object sender, EventArgs e)
void tmrGet_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (IsRunGet)
return;
GetData();
//Control.CheckForIllegalCrossThreadCalls = false;
//Thread th = new Thread(GetData);
//th.IsBackground = true;
//th.Start();
}
private void GetData(int count = 10)
{
IsRunGet = true;
while ((LstGetKeyVote.Count > 0) && (count > 0))
{
count--;
int ID = LstGetKeyVote[0].ID;
int iMode = LstGetKeyVote[0].iMode;
string sInfo = LstGetKeyVote[0].sInfo;
LstGetKeyVote.RemoveAt(0);
//异步调用,卡UI
//if (OnKeyVote != null)
// this.BeginInvoke(OnKeyVote, ID, iMode, sInfo);
//直接调用,但是线程访问问题,卡UI。
//if (OnKeyVote != null)
// OnKeyVote(ID, iMode, sInfo);
//方法A:同步调用,但是卡UI
if (OnKeyVote != null)
this.Invoke(OnKeyVote, ID, iMode, sInfo);
}
if (LstGetKeyVote.Count < 1)
tmrGet.Enabled = false;
IsRunGet = false;
}
private class KeyVoteRes
{
public int ID = 0;
public int iMode = 0;
public string sInfo = "";
}
}
}