172 lines
5.9 KiB
C#
172 lines
5.9 KiB
C#
using Termo.Active.Core.Properties;
|
|
using Termo.Active.Model;
|
|
using Termo.Active.Utils;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using TeamDev.SDK.MVVM;
|
|
|
|
namespace Termo.Active.Core
|
|
{
|
|
public static class ThreadSiemensHmi
|
|
{
|
|
private static Thread SiemensHmi;
|
|
private static string ProcessPath;
|
|
private static string ProcessName = "slsmhmihost";
|
|
private static string ProcessName2 = "RUN_HMI";
|
|
private static string ProcessName3 = "SLSMSYSTEMMANAGER";
|
|
private static string SiemensPath = @"\Siemens\MotionControl\siemens\sinumerik\hmi\autostart\run_hmi.exe";
|
|
private static Process NcProcess;
|
|
private static int TriedTimes;
|
|
private static bool ProcKilled;
|
|
private static int WaitingMs = 500;
|
|
private static int TimesToTryKill = 10;
|
|
private const int WS_HIDE = 0;
|
|
private const int WS_SHOW = 5;
|
|
private const int WM_SETICON = 0x80;
|
|
private const int ICON_BIG = 1;
|
|
|
|
public static int InitWindow()
|
|
{
|
|
SiemensHmi = new Thread(new ThreadStart(CheckRunningHmi));
|
|
|
|
ProcessPath = GetProgramFilesx86Path() + SiemensPath;
|
|
|
|
// Read if exists a Process with correct name
|
|
Process[] processes = Process.GetProcessesByName(ProcessName);
|
|
|
|
// Check if exists the Nc Path
|
|
if (!File.Exists(ProcessPath))
|
|
ExceptionManager.ManageError(Constants.ERROR_LEVEL.FATAL, "Siemens HMI not found at: " + ProcessPath);
|
|
|
|
// If exists the Process
|
|
if (processes.Length > 0)
|
|
{
|
|
// Set the first founded process
|
|
NcProcess = processes[0];
|
|
NcProcess.WaitForInputIdle();
|
|
|
|
// Wait until the process is started
|
|
TriedTimes = 1;
|
|
while (processes.Length == 0 && TriedTimes < TimesToTryKill)
|
|
{
|
|
processes = Process.GetProcessesByName(ProcessName);
|
|
if (processes.Length == 0)
|
|
Thread.Sleep(WaitingMs);
|
|
|
|
TriedTimes++;
|
|
}
|
|
|
|
// Kill the process if needed
|
|
if (TriedTimes == TimesToTryKill)
|
|
{
|
|
//Kill the Process
|
|
if (NcProcess != null && !NcProcess.HasExited)
|
|
NcProcess.Kill();
|
|
|
|
//Setup the Variable
|
|
ProcKilled = true;
|
|
}
|
|
}
|
|
|
|
if (processes.Length <= 0 || ProcKilled)
|
|
{
|
|
NcProcess = new Process();
|
|
|
|
// Setup the Process path
|
|
NcProcess.StartInfo.FileName = ProcessPath;
|
|
|
|
// Start the Process
|
|
NcProcess.Start();
|
|
NcProcess.WaitForInputIdle();
|
|
|
|
// Wait until the process is started
|
|
while ((processes.Length <= 0 || (processes[0].MainWindowHandle == IntPtr.Zero)))
|
|
{
|
|
processes = Process.GetProcessesByName(ProcessName);
|
|
if (processes.Length <= 0 || (processes[0].MainWindowHandle == IntPtr.Zero))
|
|
{
|
|
Thread.Sleep(WaitingMs);
|
|
}
|
|
}
|
|
|
|
// Set the first founded process
|
|
NcProcess = processes[0];
|
|
|
|
// Set the kill method
|
|
NcProcess.Exited += OnProcessExit;
|
|
}
|
|
|
|
// Set icon
|
|
SendMessage(NcProcess.MainWindowHandle, WM_SETICON, ICON_BIG, Resources.SinumerikHmi.Handle);
|
|
|
|
// Hide the second window
|
|
processes = Process.GetProcessesByName(ProcessName2);
|
|
foreach (Process proc in processes)
|
|
{
|
|
if (proc.MainWindowHandle != IntPtr.Zero)
|
|
ShowWindow(proc.MainWindowHandle, WS_HIDE);
|
|
}
|
|
|
|
// Hide The third window
|
|
processes = Process.GetProcessesByName(ProcessName3);
|
|
foreach (Process proc in processes)
|
|
{
|
|
if (proc.MainWindowHandle != IntPtr.Zero)
|
|
ShowWindow(proc.MainWindowHandle, WS_HIDE);
|
|
}
|
|
|
|
// Start the following thread
|
|
SiemensHmi.Start();
|
|
return 0;
|
|
}
|
|
|
|
public static void StopThread()
|
|
{
|
|
if (SiemensHmi!= null && SiemensHmi.IsAlive)
|
|
SiemensHmi.Abort();
|
|
}
|
|
|
|
private static void CheckRunningHmi()
|
|
{
|
|
Process[] processes;
|
|
bool isRunning = true;
|
|
while (isRunning)
|
|
{
|
|
processes = Process.GetProcessesByName(ProcessName);
|
|
if (processes.Length == 0)
|
|
{
|
|
// Stop server
|
|
MessageServices.Current.Publish(Constants.SEND_STOP_SERVER);
|
|
isRunning = false;
|
|
}
|
|
else
|
|
Thread.Sleep(1000);
|
|
}
|
|
}
|
|
|
|
private static void OnProcessExit(object sender, EventArgs e)
|
|
{
|
|
// Close server when the hmism process it's not running
|
|
MessageServices.Current.Publish(Constants.SEND_STOP_SERVER);
|
|
}
|
|
|
|
private static string GetProgramFilesx86Path()
|
|
{
|
|
if (8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
|
|
{
|
|
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
|
|
}
|
|
return Environment.GetEnvironmentVariable("ProgramFiles");
|
|
}
|
|
|
|
[DllImport("user32.dll")]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
|
|
[DllImport("user32.dll")]
|
|
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);
|
|
}
|
|
} |