You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
568 lines
18 KiB
568 lines
18 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Threading;
|
|
using System.ServiceModel;
|
|
using System.Net;
|
|
|
|
namespace SiaSun.LMS.WCFHost
|
|
{
|
|
public partial class MAIN : Form
|
|
{
|
|
bool boolIsServer = true;
|
|
public static MAIN mainWin;
|
|
IList<ServiceModel.WCFService> listWCFService = new List<ServiceModel.WCFService>();
|
|
DateTime dtStartClearHistory = DateTime.Now;
|
|
DateTime dtStartGC = DateTime.Now;
|
|
|
|
bool boolTaskManageAlive = true;
|
|
bool boolControlApplyAlive = true;
|
|
bool boolERP_PlanAlive = true;
|
|
BackgroundWorker bkwTaskManage = new BackgroundWorker();
|
|
BackgroundWorker bkwControlApply = new BackgroundWorker();
|
|
BackgroundWorker bkwERP_Plan = new BackgroundWorker();
|
|
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
public MAIN()
|
|
{
|
|
InitializeComponent();
|
|
mainWin = this;
|
|
|
|
this.bkwTaskManage.WorkerSupportsCancellation = true;
|
|
this.bkwTaskManage.DoWork += new DoWorkEventHandler(bkwTaskManage_DoWork);
|
|
this.bkwControlApply.WorkerSupportsCancellation = true;
|
|
this.bkwControlApply.DoWork += new DoWorkEventHandler(bkwControlApply_DoWork);
|
|
//this.bkwERP_Plan.WorkerSupportsCancellation = true;
|
|
//this.bkwERP_Plan.DoWork +=new DoWorkEventHandler(bkwERP_Plan_DoWork);
|
|
this.FormClosing += new FormClosingEventHandler(MAIN_FormClosing);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查校验是否WCF服务器
|
|
/// </summary>
|
|
private void CheckWCFServer()
|
|
{
|
|
try
|
|
{
|
|
//获得设置的服务器地址
|
|
string strWCFServer = SiaSun.LMS.Common.AppSettings.GetValue("CheckWCFServer");
|
|
if (Convert.ToBoolean(strWCFServer))
|
|
{
|
|
if (MainApp._IPAddressList.Length == 0)
|
|
{
|
|
boolIsServer = false;
|
|
}
|
|
else
|
|
{
|
|
string strServerIP = SiaSun.LMS.Common.AppSettings.GetValue("WCFServer");
|
|
boolIsServer = (MainApp._IPAddressList.Count(r => r.ToString() == strServerIP) > 0);
|
|
}
|
|
|
|
//如果非服务器则设置所有的服务端功能设置不可用
|
|
if (!boolIsServer)
|
|
{
|
|
this.menuFile.Enabled = this.menuHelp.Enabled = this.menuTask.Enabled = this.menuManage.Enabled = false;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
boolIsServer = false;
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
|
|
//加载窗体
|
|
private void MAIN_Load(object sender, EventArgs e)
|
|
{
|
|
//忽略线程间调用错误
|
|
Control.CheckForIllegalCrossThreadCalls = false;
|
|
|
|
//设置显示标题
|
|
this.Text = SiaSun.LMS.Common.AppSettings.GetValue("Title");
|
|
|
|
//检查是否WCF服务器
|
|
this.CheckWCFServer();
|
|
|
|
string mesout = string.Empty;
|
|
|
|
//设置数据绑定
|
|
if (this.boolIsServer)
|
|
{
|
|
if (this.StartService())
|
|
{
|
|
//打开业务处理开关,允许处理业务
|
|
this.BackWorkerEnable(true);
|
|
|
|
//设置标识
|
|
this.StartBackWorker();
|
|
|
|
//显示托盘图标
|
|
this.notifyIconServer.Visible = true;
|
|
|
|
//隐藏
|
|
this.menuItemHide_Click(null, EventArgs.Empty);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("该计算机不能启用服务端程序!");
|
|
}
|
|
//限制内存
|
|
System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = new IntPtr(10);
|
|
}
|
|
|
|
#region ------线程监视,线程管理--管理任务处理、控制指令申请处理
|
|
|
|
/// <summary>
|
|
/// 加载线程
|
|
/// </summary>
|
|
private void StartBackWorker()
|
|
{
|
|
this.bkwTaskManage.RunWorkerAsync();
|
|
this.bkwControlApply.RunWorkerAsync();
|
|
this.bkwERP_Plan.RunWorkerAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 任务处理
|
|
/// </summary>
|
|
void bkwTaskManage_DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
string sResult = string.Empty;
|
|
//判断是否启用开关
|
|
while (this.boolTaskManageAlive)
|
|
{
|
|
//是否处理业务
|
|
if (this.menuItemManageTask.Checked)
|
|
{
|
|
try
|
|
{
|
|
MainApp._I_BaseService.Invoke("ControlBase", "CONTROL_WATCHING", new object[] { }, out sResult);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//this.menuItemManageTask.Checked = false;
|
|
|
|
this.AddMessage("CONTROL_Watching." + ex.ToString());
|
|
}
|
|
|
|
try
|
|
{
|
|
//监视管理任务,处理完成的管理任务
|
|
//Program._I_ManageService.MANAGE_Watching();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//this.menuItemManageTask.Checked = false;
|
|
this.AddMessage("MANAGE_Watching." + ex.ToString());
|
|
}
|
|
|
|
//清理日志信息
|
|
this.ClearLog();
|
|
|
|
//判断是否清理内存--30分钟定时清理一次
|
|
if (dtStartGC.AddMinutes(30) < DateTime.Now)
|
|
{
|
|
dtStartGC = DateTime.Now;
|
|
System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = new IntPtr(20);
|
|
}
|
|
}
|
|
|
|
//挂起2秒
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 控制指令申请
|
|
/// </summary>
|
|
void bkwControlApply_DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
string sResult = string.Empty;
|
|
//判断是否启用开关
|
|
while (this.boolControlApplyAlive)
|
|
{
|
|
//是否处理业务
|
|
if (menuItemControlApply.Checked)
|
|
{
|
|
try
|
|
{
|
|
//是否过期
|
|
bool boolAllowClearHistory = (dtStartClearHistory.AddDays(2) < DateTime.Now);
|
|
|
|
//监视申请任务
|
|
MainApp._I_BaseService.Invoke("ControlBase", "ControlApplyTask", new object[] { }, out sResult);
|
|
|
|
//MainApp._I_BaseService.ExecuteNonQuery_ReturnVoid(" delete from io_control where manage_id = 0 and control_status = 999 or control_status=900");
|
|
|
|
//更改日期记录
|
|
if (boolAllowClearHistory)
|
|
{
|
|
dtStartClearHistory = DateTime.Now;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//this.menuItemControlApply.Checked = false;
|
|
this.AddMessage("CONTROL_APPLY," + ex.ToString());
|
|
}
|
|
}
|
|
//挂起2秒
|
|
Thread.Sleep(500);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ERP控制指令申请
|
|
/// </summary>
|
|
//void bkwERP_Plan_DoWork(object sender, DoWorkEventArgs e)
|
|
//{
|
|
// string sResult = string.Empty;
|
|
// //判断是否启用开关
|
|
// while (this.boolERP_PlanAlive)
|
|
// {
|
|
// //是否处理业务
|
|
// if (menuItemControlApply.Checked)
|
|
// {
|
|
// try
|
|
// {
|
|
// //监视计划申请任务
|
|
// MainApp._I_BaseService.Invoke("ControlBase", "ERP_Plan_Task", new object[] { }, out sResult);
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// this.menuItemControlApply.Checked = false;
|
|
// this.AddMessage("ERP_Plan_Task," + ex.ToString());
|
|
// }
|
|
// }
|
|
// //挂起2秒
|
|
// Thread.Sleep(5000);
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 停止线程
|
|
/// </summary>
|
|
private void StopBackWorker()
|
|
{
|
|
try
|
|
{
|
|
//终止线程标识
|
|
this.boolTaskManageAlive = false;
|
|
this.boolControlApplyAlive = false;
|
|
this.boolERP_PlanAlive = false;
|
|
|
|
//停止处理标识
|
|
this.menuItemManageTask.Checked = false;
|
|
this.menuItemControlApply.Checked = false;
|
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
//释放资源
|
|
this.bkwTaskManage.CancelAsync();
|
|
this.bkwControlApply.CancelAsync();
|
|
this.bkwERP_Plan.CancelAsync();
|
|
|
|
//Message
|
|
this.AddMessage("Stop Thread Finished!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止业务处理
|
|
/// </summary>
|
|
private void BackWorkerEnable(bool IsEnable)
|
|
{
|
|
this.menuItemManageTask.Checked = IsEnable;
|
|
this.menuItemControlApply.Checked = IsEnable;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除日志文件
|
|
/// </summary>
|
|
private void ClearLog()
|
|
{
|
|
try
|
|
{
|
|
if (DateTime.Now.Hour == 12 && DateTime.Now.Minute == 30 && DateTime.Now.Second < 30)
|
|
{
|
|
DateTime dt = DateTime.Now.AddDays(-60);
|
|
MainApp._I_BaseService.ClearLogFile(dt);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.AddMessage(ex.Message);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//显示消息
|
|
public void AddMessage(string Message)
|
|
{
|
|
try
|
|
{
|
|
//清除数据
|
|
if (this.richtxtMessage.Lines.Length >= 500)
|
|
{
|
|
this.richtxtMessage.Clear();
|
|
}
|
|
|
|
//追加显示数据
|
|
this.richtxtMessage.AppendText(Message);
|
|
this.richtxtMessage.AppendText(Environment.NewLine);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
#region ------启动/停止服务
|
|
|
|
/// <summary>
|
|
/// 按钮事件
|
|
/// </summary>
|
|
private void ButtonClick(object sender,EventArgs e)
|
|
{
|
|
ToolStripButton btn = sender as ToolStripButton;
|
|
if (btn != null)
|
|
{
|
|
switch (btn.Name)
|
|
{
|
|
case "tlbtnStart":
|
|
if (this.StartService())
|
|
{
|
|
this.BackWorkerEnable(true);
|
|
}
|
|
break;
|
|
case "tlbtnStop":
|
|
if (this.StopService())
|
|
{
|
|
this.BackWorkerEnable(false);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置服务显示
|
|
/// </summary>
|
|
private bool StartService()
|
|
{
|
|
bool boolResult = true;
|
|
try
|
|
{
|
|
this.listWCFService.Clear();
|
|
this.gridService.DataSource = null;
|
|
|
|
ServiceHostGroup.StartAllConfiguredServices(ref listWCFService);
|
|
this.gridService.DataSource = listWCFService;
|
|
boolResult = listWCFService.Count(r => r.WCFServiceStatus == "启动") > 0;
|
|
if (boolResult)
|
|
{
|
|
tlbtnStart.Enabled = false;
|
|
tlbtnStop.Enabled = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.AddMessage(string.Format("{0},{1}","启动服务",ex.Message+"\n"+ex.InnerException.Message));
|
|
}
|
|
return boolResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止服务
|
|
/// </summary>
|
|
private bool StopService()
|
|
{
|
|
bool boolResult = true;
|
|
try
|
|
{
|
|
this.gridService.DataSource = null;
|
|
//关闭服务
|
|
ServiceHostGroup.CloseAllServices(ref listWCFService);
|
|
this.gridService.DataSource = listWCFService;
|
|
this.tlbtnStart.Enabled = true;
|
|
this.tlbtnStop.Enabled = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.AddMessage(string.Format("{0},{1}","停止服务",ex.Message));
|
|
}
|
|
return boolResult;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ------菜单栏
|
|
|
|
/// <summary>
|
|
/// 菜单点击
|
|
/// </summary>
|
|
private void MenuItemClick(object sender, EventArgs e)
|
|
{
|
|
ToolStripMenuItem item = sender as ToolStripMenuItem;
|
|
if (item != null)
|
|
{
|
|
switch (item.Name)
|
|
{
|
|
//文件
|
|
case "menuItemSaveMessage": //保存消息
|
|
this.SaveMessage();
|
|
break;
|
|
case "menuItemClearMessage": //清除消息
|
|
this.richtxtMessage.Clear();
|
|
break;
|
|
|
|
//任务处理
|
|
case "menuItemManageTask": //管理任务处理
|
|
case "menuItemControlApply": //控制指令申请
|
|
item.Checked = !item.Checked;
|
|
break;
|
|
|
|
case "menuItemConfig":
|
|
this.Config();
|
|
break;
|
|
|
|
//帮助
|
|
case "menuItemHelp":
|
|
this.Help();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存消息
|
|
/// </summary>
|
|
private void SaveMessage()
|
|
{
|
|
try
|
|
{
|
|
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
|
|
{
|
|
string strFileName = this.saveFileDialog1.FileName;
|
|
using (System.IO.FileStream flstr = new System.IO.FileStream(strFileName, System.IO.FileMode.OpenOrCreate))
|
|
{
|
|
System.IO.StreamWriter strWriter = new System.IO.StreamWriter(flstr);
|
|
strWriter.Write(this.richtxtMessage.Text);
|
|
|
|
strWriter.Close();
|
|
flstr.Close();
|
|
|
|
strWriter.Dispose();
|
|
flstr.Dispose();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message,"系统提示");
|
|
}
|
|
}
|
|
|
|
//配置信息
|
|
private void Config()
|
|
{
|
|
CONFIG form = new CONFIG();
|
|
form.Show();
|
|
}
|
|
|
|
//显示帮助文档
|
|
private void Help()
|
|
{
|
|
try
|
|
{
|
|
System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory + @"Tools\SSLMS_Server_Help.chm");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region -------窗体
|
|
|
|
//关闭窗体
|
|
void MAIN_FormClosing(object sender, FormClosingEventArgs e)
|
|
{
|
|
e.Cancel = true;
|
|
menuItemHide_Click(null, EventArgs.Empty);
|
|
}
|
|
|
|
//双击图标显示界面
|
|
private void notifyIconServer_DoubleClick(object sender, EventArgs e)
|
|
{
|
|
this.Visible = !this.Visible;
|
|
if (this.Visible)
|
|
{
|
|
menuItemShow_Click(null, EventArgs.Empty);
|
|
}
|
|
else
|
|
{
|
|
menuItemHide_Click(null, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
//显示窗体界面
|
|
private void menuItemShow_Click(object sender, EventArgs e)
|
|
{
|
|
if (this.Visible)
|
|
return;
|
|
this.WindowState = FormWindowState.Maximized;
|
|
this.Visible = true;
|
|
}
|
|
|
|
//隐藏窗体界面
|
|
private void menuItemHide_Click(object sender, EventArgs e)
|
|
{
|
|
if (!this.Visible)
|
|
return;
|
|
this.WindowState = FormWindowState.Minimized;
|
|
this.Hide();
|
|
this.notifyIconServer.ShowBalloonTip(3, "提示", "程序隐藏在后台运行", ToolTipIcon.Info);
|
|
}
|
|
|
|
//退出程序
|
|
private void menuItemExit_Click(object sender, EventArgs e)
|
|
{
|
|
if (MessageBox.Show("终止程序将会影响其他客户端的访问,确定终止服务端程序!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
|
|
{
|
|
//终止线程
|
|
this.StopBackWorker();
|
|
|
|
//延迟关闭
|
|
Thread.Sleep(500);
|
|
|
|
//终止服务
|
|
this.StopService();
|
|
|
|
//释放资源
|
|
this.notifyIconServer.Dispose();
|
|
|
|
//终止程序
|
|
Application.ExitThread();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
}
|