BaoKai_202508-Wcs-Jingwang..../WCSIce/Program.cs
2025-08-24 12:51:29 +08:00

166 lines
6.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.UserSkins;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using DevExpress.XtraSplashScreen;
namespace WCS
{
static class Program
{
/// <summary>
/// 该函数设置由不同线程产生的窗口的显示状态。
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="cmdShow">指定窗口如何显示。查看允许值列表请查阅ShowWlndow函数的说明部分。</param>
/// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。</returns>
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
/// <summary>
/// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
/// </summary>
/// <param name="hWnd">将被激活并被调入前台的窗口句柄。</param>
/// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。</returns>
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//f8331421-62a4-4264-b327-ab63c272f7e9
if (!HslCommunication.Authorization.SetAuthorizationCode("f8331421-62a4-4264-b327-ab63c272f7e9"))
{
// active failed
MessageBox.Show("授权失败当前程序只能使用24小时");
return;
}
Process instance = RunningInstance();
if (instance == null)
{
#region PLC地址
///SplashScreenManager.ShowDefaultWaitForm("请稍后...","应用程序加载中...");
Dictionary<string, List<string>> typeClass = new Dictionary<string, List<string>>();
DataTable table = RfConfig.Create().plcds.Tables[0];
foreach (DataRow row in table.Rows)
{
string[] plcstr = row["plcvalaue"].ToString().Split('%');
List<string> listPlc = new List<string>();
for (int i = 0; i < plcstr.Length; i++)
{
if (plcstr[i].ToString().Trim().Length > 0)
listPlc.Add(plcstr[i].ToString());
}
typeClass.Add(row["vlaue"].ToString(), listPlc);
}
PLCCommon.PlcFactory.Instance().typeClass = typeClass;
#endregion
//获取异常信息并记录
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
RfConfig.Create().GetConfigInfo();
BonusSkins.Register();
SkinManager.EnableFormSkins();
UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");
//SplashScreenManager.CloseForm();
Application.Run(new MainForm());
}
else
{
HandleRunningInstance(instance);
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
WriteSysLog.SysLog("程序发送异常" + str);
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
WriteSysLog.SysLog("程序发生异常" + str);
}
/// <summary>
/// 获取正在运行的实例没有运行的实例返回null;
/// </summary>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
return process;
}
}
}
return null;
}
/// <summary>
/// 显示已运行的程序。
/// </summary>
public static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //显示
SetForegroundWindow(instance.MainWindowHandle); //放到前端
}
static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");
return sb.ToString();
}
}
}