228f3fb69e9eb0ff4ba69d800fc1776409d5acda.svn-base 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Collections;
  7. using System.ComponentModel;
  8. using System.Windows.Forms;
  9. using System.Configuration;
  10. using System.Threading;
  11. using Core.Mes.IBaseInterface;
  12. using Core.Mes.ClientFrameWork;
  13. using System.Runtime.InteropServices;
  14. using System.Diagnostics;
  15. namespace Core.Mes.ClientManager
  16. {
  17. /// <summary>
  18. /// 登录窗口
  19. /// </summary>
  20. public class FrmCover : System.Windows.Forms.Form
  21. {
  22. #region " 定义变量 "
  23. public MainForm _mainForm;
  24. public static int pot;
  25. public static string url;
  26. public static string messageFlag;
  27. public int _loginFlag = 0;
  28. //重新登录标志
  29. public bool ReLoginFlag = false;
  30. public int ReLoginType = 0;
  31. private System.Windows.Forms.Timer timer_checkSem;
  32. private int index = 1;
  33. #endregion //
  34. #region " 窗体自动生成代码 "
  35. public FrmCover()
  36. {
  37. //
  38. // Windows 窗体设计器支持所必需的
  39. //
  40. InitializeComponent();
  41. //
  42. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  43. //
  44. }
  45. public FrmCover(string sUserName)
  46. {
  47. //
  48. // Windows 窗体设计器支持所必需的
  49. //
  50. InitializeComponent();
  51. textBox1.Text = sUserName;
  52. textBox1.Enabled = false;
  53. //
  54. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  55. //
  56. }
  57. /// <summary>
  58. /// 清理所有正在使用的资源。
  59. /// </summary>
  60. protected override void Dispose(bool disposing)
  61. {
  62. if (disposing)
  63. {
  64. if (components != null)
  65. {
  66. components.Dispose();
  67. }
  68. }
  69. base.Dispose(disposing);
  70. }
  71. private System.Windows.Forms.Label label4;
  72. private System.Windows.Forms.Label label5;
  73. private System.Windows.Forms.TextBox textBox1;
  74. private System.Windows.Forms.TextBox textBox2;
  75. private System.Windows.Forms.Button btnLoad;
  76. private System.Windows.Forms.ToolTip toolTip1;
  77. private System.Windows.Forms.Button btnExit;
  78. private System.Windows.Forms.PictureBox pictureBox1;
  79. private System.ComponentModel.IContainer components;
  80. private global::Infragistics.Win.UltraWinProgressBar.UltraProgressBar ultraProgressBar1;
  81. private System.Windows.Forms.Timer timerAutoRun;
  82. #region Windows 窗体设计器生成的代码
  83. /// <summary>
  84. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  85. /// 此方法的内容。
  86. /// </summary>
  87. private void InitializeComponent()
  88. {
  89. this.components = new System.ComponentModel.Container();
  90. Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance();
  91. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmCover));
  92. this.textBox1 = new System.Windows.Forms.TextBox();
  93. this.textBox2 = new System.Windows.Forms.TextBox();
  94. this.btnLoad = new System.Windows.Forms.Button();
  95. this.btnExit = new System.Windows.Forms.Button();
  96. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  97. this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
  98. this.ultraProgressBar1 = new Infragistics.Win.UltraWinProgressBar.UltraProgressBar();
  99. this.timerAutoRun = new System.Windows.Forms.Timer(this.components);
  100. this.label4 = new System.Windows.Forms.Label();
  101. this.label5 = new System.Windows.Forms.Label();
  102. this.timer_checkSem = new System.Windows.Forms.Timer(this.components);
  103. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  104. this.SuspendLayout();
  105. //
  106. // textBox1
  107. //
  108. this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Append;
  109. this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
  110. this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  111. this.textBox1.Location = new System.Drawing.Point(150, 133);
  112. this.textBox1.Name = "textBox1";
  113. this.textBox1.Size = new System.Drawing.Size(120, 21);
  114. this.textBox1.TabIndex = 0;
  115. this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
  116. this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
  117. //
  118. // textBox2
  119. //
  120. this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  121. this.textBox2.Location = new System.Drawing.Point(150, 161);
  122. this.textBox2.Name = "textBox2";
  123. this.textBox2.PasswordChar = '*';
  124. this.textBox2.Size = new System.Drawing.Size(120, 21);
  125. this.textBox2.TabIndex = 1;
  126. this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
  127. //
  128. // btnLoad
  129. //
  130. this.btnLoad.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  131. this.btnLoad.ImageIndex = 0;
  132. this.btnLoad.Location = new System.Drawing.Point(282, 132);
  133. this.btnLoad.Name = "btnLoad";
  134. this.btnLoad.Size = new System.Drawing.Size(56, 23);
  135. this.btnLoad.TabIndex = 2;
  136. this.btnLoad.Text = "登录";
  137. this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
  138. //
  139. // btnExit
  140. //
  141. this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  142. this.btnExit.ImageIndex = 3;
  143. this.btnExit.Location = new System.Drawing.Point(282, 160);
  144. this.btnExit.Name = "btnExit";
  145. this.btnExit.Size = new System.Drawing.Size(55, 23);
  146. this.btnExit.TabIndex = 3;
  147. this.btnExit.Text = "退出";
  148. this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
  149. //
  150. // pictureBox1
  151. //
  152. this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
  153. this.pictureBox1.Location = new System.Drawing.Point(0, 0);
  154. this.pictureBox1.Name = "pictureBox1";
  155. this.pictureBox1.Size = new System.Drawing.Size(450, 104);
  156. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  157. this.pictureBox1.TabIndex = 6;
  158. this.pictureBox1.TabStop = false;
  159. //
  160. // ultraProgressBar1
  161. //
  162. this.ultraProgressBar1.BorderStyle = Infragistics.Win.UIElementBorderStyle.None;
  163. this.ultraProgressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
  164. appearance1.ForeColor = System.Drawing.Color.Yellow;
  165. this.ultraProgressBar1.FillAppearance = appearance1;
  166. this.ultraProgressBar1.Location = new System.Drawing.Point(0, 213);
  167. this.ultraProgressBar1.Maximum = 300;
  168. this.ultraProgressBar1.Name = "ultraProgressBar1";
  169. this.ultraProgressBar1.Size = new System.Drawing.Size(450, 14);
  170. this.ultraProgressBar1.Step = 1;
  171. this.ultraProgressBar1.SupportThemes = false;
  172. this.ultraProgressBar1.TabIndex = 8;
  173. this.ultraProgressBar1.Text = "新余钢铁有限责任公司技术中心";
  174. //
  175. // timerAutoRun
  176. //
  177. this.timerAutoRun.Tick += new System.EventHandler(this.timerAutoRun_Tick);
  178. //
  179. // label4
  180. //
  181. this.label4.AutoSize = true;
  182. this.label4.Location = new System.Drawing.Point(105, 137);
  183. this.label4.Name = "label4";
  184. this.label4.Size = new System.Drawing.Size(41, 12);
  185. this.label4.TabIndex = 12;
  186. this.label4.Text = "用户名";
  187. //
  188. // label5
  189. //
  190. this.label5.AutoSize = true;
  191. this.label5.Location = new System.Drawing.Point(117, 165);
  192. this.label5.Name = "label5";
  193. this.label5.Size = new System.Drawing.Size(29, 12);
  194. this.label5.TabIndex = 13;
  195. this.label5.Text = "密码";
  196. //
  197. // timer_checkSem
  198. //
  199. this.timer_checkSem.Interval = 200;
  200. this.timer_checkSem.Tick += new System.EventHandler(this.timer_checkSem_Tick);
  201. //
  202. // FrmCover
  203. //
  204. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  205. this.ClientSize = new System.Drawing.Size(450, 227);
  206. this.Controls.Add(this.label5);
  207. this.Controls.Add(this.label4);
  208. this.Controls.Add(this.textBox2);
  209. this.Controls.Add(this.textBox1);
  210. this.Controls.Add(this.ultraProgressBar1);
  211. this.Controls.Add(this.btnExit);
  212. this.Controls.Add(this.btnLoad);
  213. this.Controls.Add(this.pictureBox1);
  214. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  215. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  216. this.MaximizeBox = false;
  217. this.Name = "FrmCover";
  218. this.ShowInTaskbar = false;
  219. this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
  220. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  221. this.Text = "系统用户登录";
  222. this.Load += new System.EventHandler(this.FrmCover_Load);
  223. this.SizeChanged += new System.EventHandler(this.FrmCover_SizeChanged);
  224. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  225. this.ResumeLayout(false);
  226. this.PerformLayout();
  227. }
  228. #endregion
  229. #endregion
  230. #region " Init Form "
  231. DataSet dsetLoginUrl = new DataSet();
  232. /// <summary>
  233. /// 窗体初始化
  234. /// </summary>
  235. /// <param name="sender"></param>
  236. /// <param name="e"></param>
  237. private void FrmCover_Load(object sender, System.EventArgs e)
  238. {
  239. try
  240. {
  241. if (ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["Corporation"].Value.ToString() != null)
  242. this.ultraProgressBar1.Text = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["Corporation"].Value.ToString() + @" & 湖南视拓科技发展有限责任公司 联合研发";
  243. this.pictureBox1.Image = System.Drawing.Image.FromFile(@"LoginPic.jpg");
  244. url = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["RemoteServerUrl"].Value.ToString();
  245. if (File.Exists("LoginUrl.txt"))
  246. {
  247. dsetLoginUrl.ReadXml("LoginUrl.txt");
  248. dsetLoginUrl.Tables[0].PrimaryKey = new DataColumn[] { dsetLoginUrl.Tables[0].Columns[0] };
  249. foreach (DataRow arow in dsetLoginUrl.Tables[0].Rows)
  250. {
  251. textBox1.AutoCompleteCustomSource.Add(arow[0].ToString());
  252. }
  253. }
  254. else
  255. {
  256. DataTable dtab = new DataTable();
  257. dtab.Columns.Add("LogID", typeof(string));
  258. dtab.PrimaryKey = new DataColumn[] { dtab.Columns[0] };
  259. dsetLoginUrl.Tables.Add(dtab);
  260. dsetLoginUrl.WriteXml("LoginUrl.txt", XmlWriteMode.IgnoreSchema);
  261. }
  262. }
  263. catch (Exception ex)
  264. {
  265. Console.WriteLine(ex.Message);
  266. }
  267. try
  268. {
  269. //为锁定统一登录设置进程间信号量
  270. if (!string.IsNullOrEmpty(textBox1.Text.Trim()) && ReLoginType == 2)
  271. {
  272. Semaphore sem = OpenSem(false, true);
  273. if (sem != null)
  274. timer_checkSem.Enabled = true;
  275. }
  276. }
  277. catch { }
  278. this.textBox1.Focus();
  279. }
  280. #endregion
  281. #region " Button Event "
  282. private bool IsLoginThreadFlag = false;
  283. private string loginID = "";
  284. private string passwd = "";
  285. private bool IsException = false;
  286. private Thread ThLogin;
  287. /// <summary>
  288. /// 登录按钮事件
  289. /// </summary>
  290. /// <param name="sender"></param>
  291. /// <param name="e"></param>
  292. private void btnLoad_Click(object sender, System.EventArgs e)
  293. {
  294. loginID = this.textBox1.Text.Trim();
  295. passwd = this.textBox2.Text.Trim();
  296. this.timerAutoRun.Interval = 100;
  297. index = 1;
  298. IsException = false;
  299. this.ultraProgressBar1.Value = this.ultraProgressBar1.Minimum;
  300. this.btnLoad.Enabled = false;
  301. if (!ReLoginFlag)
  302. {
  303. //this.ultraProgressBar1.Visible = true;
  304. this.timerAutoRun.Start();
  305. ThreadStart ts = new ThreadStart(LoginHandler);
  306. ThLogin = new Thread(ts);
  307. ThLogin.Name = "LoginThread";
  308. ThLogin.Start();
  309. }
  310. else
  311. {
  312. Relogin();
  313. }
  314. }
  315. private void Relogin()
  316. {
  317. try
  318. {
  319. string ipaddress = ClientCommon.GetIp();
  320. SimpleReturnObject _out = new SimpleReturnObject(0, "");
  321. ClientCommon._RemotingHelp.InitServerUrlList("ServerCommon", url);
  322. object obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetPurviewInfo",
  323. new object[] { this.loginID, this.passwd, Dns.GetHostName(), ipaddress }, out _out);
  324. if (_out.ErrCode != 0) throw new Exception(_out.ErrMessage);
  325. if (!string.IsNullOrEmpty(_out.ErrMessage))
  326. {
  327. this._mainForm.ShowPopUpMsg(_out.ErrMessage, "密码过于简单,请尽快修改密码!");
  328. }
  329. ClientCommon._UserInfo = (UserInfo)obj;
  330. Semaphore sem = OpenSem(true, true);
  331. timer_checkSem.Enabled = false;
  332. AfterLogin();
  333. //调用服务器与客户端时间同步函数
  334. SetTime.SetClientTime();
  335. }
  336. catch (Exception ex)
  337. {
  338. MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  339. }
  340. this.btnLoad.Enabled = true;
  341. }
  342. private void AfterLogin()
  343. {
  344. try
  345. {
  346. _loginFlag = 1;
  347. try
  348. {
  349. if (dsetLoginUrl.Tables[0].Rows.Find(textBox1.Text.Trim()) == null)
  350. {
  351. if (dsetLoginUrl.Tables[0].Rows.Count == 10)
  352. dsetLoginUrl.Tables[0].Rows[0].Delete();
  353. dsetLoginUrl.Tables[0].Rows.Add(new object[] { textBox1.Text.Trim() });
  354. dsetLoginUrl.Tables[0].AcceptChanges();
  355. dsetLoginUrl.WriteXml("LoginUrl.txt", XmlWriteMode.IgnoreSchema);
  356. }
  357. }
  358. catch
  359. { }
  360. this.Close();
  361. }
  362. catch (Exception ex)
  363. {
  364. this.btnLoad.Enabled = true;
  365. _loginFlag = 0;
  366. MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  367. }
  368. }
  369. private void LoginHandler()
  370. {
  371. string ipaddress = ClientCommon.GetIp();
  372. try
  373. {
  374. IsLoginThreadFlag = false;
  375. SimpleReturnObject outInfo = new SimpleReturnObject(0, "");
  376. ClientCommon._RemotingHelp.InitServerUrlList("ServerCommon", url);
  377. object obj = ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetPurviewInfo",
  378. new object[] { this.loginID, this.passwd, Dns.GetHostName(), ipaddress }, out outInfo);
  379. if (outInfo.ErrCode != 0) throw new Exception(outInfo.ErrMessage);
  380. if (!string.IsNullOrEmpty(outInfo.ErrMessage))
  381. {
  382. this._mainForm.ShowPopUpMsg(outInfo.ErrMessage, "密码过于简单,请尽快修改密码!");
  383. }
  384. ClientCommon._UserInfo = (UserInfo)obj;
  385. ClientCommon._RemotingHelp.ServerUrlList = (DataSet)ClientCommon._RemotingHelp.ExecuteMethod("ServerCommon", "Core.Mes.ServerCommon.UserInfoManager", "GetServerList",
  386. null, out outInfo);
  387. _loginFlag = 1;
  388. IsLoginThreadFlag = true;
  389. }
  390. catch (Exception ex)
  391. {
  392. //this.btnLoad.Enabled =true;
  393. IsException = true;
  394. _loginFlag = 0;
  395. IsLoginThreadFlag = true;
  396. if (ex.Message == "线程正被中止。") return;
  397. MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  398. }
  399. }
  400. private void btnExit_Click(object sender, System.EventArgs e)
  401. {
  402. _loginFlag = -1;
  403. try
  404. {
  405. this.timerAutoRun.Stop();
  406. if (ThLogin != null)
  407. {
  408. ThLogin.Abort();
  409. }
  410. }
  411. catch { }
  412. this.Close();
  413. }
  414. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  415. {
  416. if (e.KeyChar == (char)13)
  417. {
  418. }
  419. if (e.KeyChar == (char)27)
  420. {
  421. this.btnExit_Click(sender, new EventArgs());
  422. }
  423. }
  424. private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  425. {
  426. if (e.KeyChar == (char)13)
  427. {
  428. this.btnLoad_Click(sender, new EventArgs());
  429. }
  430. if (e.KeyChar == (char)27)
  431. {
  432. this.btnExit_Click(sender, new EventArgs());
  433. }
  434. }
  435. private void button1_Click(object sender, System.EventArgs e)
  436. {
  437. FrmPasswd frm = new FrmPasswd();
  438. frm.strUserID = this.textBox1.Text.Trim();
  439. frm.ShowDialog();
  440. }
  441. #endregion
  442. #region " Monuse Events "
  443. #endregion
  444. private void timerAutoRun_Tick(object sender, System.EventArgs e)
  445. {
  446. int current = this.ultraProgressBar1.Value;
  447. int max = this.ultraProgressBar1.Maximum;
  448. int min = this.ultraProgressBar1.Minimum;
  449. int step = this.ultraProgressBar1.Step;
  450. if (index++ > 300)
  451. {
  452. string strInfo = "";
  453. try
  454. {
  455. this.timerAutoRun.Stop();
  456. if (ThLogin != null)
  457. {
  458. ThLogin.Abort();
  459. }
  460. }
  461. catch (Exception ex)
  462. {
  463. strInfo = ex.Message;
  464. }
  465. MessageBox.Show("连接超时!\n" + strInfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  466. return;
  467. }
  468. if (current == max && step > 0)
  469. this.ultraProgressBar1.Value = min;
  470. this.ultraProgressBar1.PerformStep();
  471. if (IsLoginThreadFlag)
  472. {
  473. if (IsException)
  474. {
  475. this.timerAutoRun.Stop();
  476. //this.ultraProgressBar1.Visible = false;
  477. }
  478. if (this.ultraProgressBar1.Value != max)
  479. {
  480. this.timerAutoRun.Interval = 10;
  481. this.ultraProgressBar1.Step = 5;
  482. }
  483. else
  484. {
  485. this.timerAutoRun.Stop();
  486. Semaphore sem = OpenSem(true, false);
  487. this.AfterLogin();
  488. }
  489. }
  490. else
  491. {
  492. }
  493. }
  494. private void textBox1_KeyDown(object sender, KeyEventArgs e)
  495. {
  496. if (e.KeyData == System.Windows.Forms.Keys.Return)
  497. {
  498. System.Windows.Forms.SendKeys.Send("{TAB}");
  499. }
  500. }
  501. [DllImport("user32.dll")]
  502. private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  503. [DllImport("User32.dll")]
  504. public static extern int SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
  505. //ShowWindow参数
  506. private const int SW_SHOWMINIMIZED = 2;
  507. private const int SWP_SHOWWINDOW = 0x40;
  508. private void FrmCover_SizeChanged(object sender, EventArgs e)
  509. {
  510. //base.OnSizeChanged(e);
  511. if (this.WindowState == FormWindowState.Minimized)
  512. {
  513. var owner = this.Owner;
  514. if (owner != null)
  515. {
  516. ShowWindow(owner.Handle, SW_SHOWMINIMIZED);
  517. this.Visible = true;
  518. //移到屏幕外面,模拟最小化效果
  519. SetWindowPos(this.Handle, IntPtr.Zero, owner.Left, owner.Top, 0, 0, SWP_SHOWWINDOW);
  520. //Trace.WriteLine(string.Format("x: {0}, y: {1}", Program.MainForm.Left, Program.MainForm.Top));
  521. }
  522. }
  523. }
  524. private void timer_checkSem_Tick(object sender, EventArgs e)
  525. {
  526. try
  527. {
  528. string loginid = textBox1.Text.Trim().ToUpper();
  529. string sem_name = "CORE_MES." + loginid;
  530. if (string.IsNullOrEmpty(loginid)) return;
  531. bool CreateNewSem = false;
  532. Semaphore ProcessSem = new Semaphore(1, 1, sem_name, out CreateNewSem);
  533. if (ProcessSem.WaitOne(0, false))
  534. {
  535. try
  536. {
  537. ProcessSem.Release(1);
  538. }
  539. catch { }
  540. timer_checkSem.Enabled = false;
  541. SomeOneLogined();
  542. }
  543. }
  544. catch { }
  545. }
  546. private Semaphore OpenSem(bool ReleaseIt, bool ForceCreate)
  547. {
  548. bool CreateNewSem = false;
  549. Semaphore sem = null;
  550. string loginid = textBox1.Text.Trim().ToUpper();
  551. string sem_name = "CORE_MES." + loginid;
  552. if (string.IsNullOrEmpty(loginid)) return sem;
  553. if (!ForceCreate)
  554. {
  555. try
  556. {
  557. sem = Semaphore.OpenExisting(sem_name);
  558. }
  559. catch
  560. {
  561. sem = null;
  562. }
  563. }
  564. else
  565. {
  566. sem = new Semaphore(1, 1, sem_name, out CreateNewSem);
  567. }
  568. try
  569. {
  570. if (sem != null)
  571. {
  572. if (ReleaseIt)
  573. {
  574. sem.Release(1);
  575. Debug.Print("释放信号量!");
  576. }
  577. else
  578. {
  579. if (sem.WaitOne(1, false))
  580. Debug.Print("获取信号量!");
  581. }
  582. }
  583. }
  584. catch (Exception ex) { }
  585. return sem;
  586. }
  587. private void SomeOneLogined()
  588. {
  589. timer_checkSem.Enabled = false;
  590. loginID = this.textBox1.Text.Trim();
  591. this.timerAutoRun.Interval = 100;
  592. index = 1;
  593. IsException = false;
  594. this.ultraProgressBar1.Value = this.ultraProgressBar1.Minimum;
  595. this.btnLoad.Enabled = false;
  596. try
  597. {
  598. AfterLogin();
  599. //调用服务器与客户端时间同步函数
  600. SetTime.SetClientTime();
  601. }
  602. catch (Exception ex)
  603. {
  604. MessageBox.Show("连接服务器失败!\n" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  605. }
  606. this.btnLoad.Enabled = true;
  607. }
  608. }
  609. }