From 22d5da7790086dafa174346adb1476db4d62b2d3 Mon Sep 17 00:00:00 2001 From: Nicola Carminati Date: Wed, 17 Apr 2019 11:17:20 +0200 Subject: [PATCH] Lauch client on CNC Connected --- Client/Browser_Tools/BrowserJSObject.cs | 4 +-- Step.Core/ThreadsFunctions.cs | 48 +++++++++++++++++++++++-- Step/program.cs | 2 -- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/Client/Browser_Tools/BrowserJSObject.cs b/Client/Browser_Tools/BrowserJSObject.cs index e5e4292f..ca800fc3 100644 --- a/Client/Browser_Tools/BrowserJSObject.cs +++ b/Client/Browser_Tools/BrowserJSObject.cs @@ -122,9 +122,7 @@ namespace Active_Client.Browser_Tools //If the mainform is disposed do nothing if (mainForm.IsDisposed) return; - - //Invoke method if is needed or call the method in STD mode - + //Invoke method if is needed or call the method in STD mode if (mainForm.InvokeRequired) mainForm.Invoke((MethodInvoker)delegate () diff --git a/Step.Core/ThreadsFunctions.cs b/Step.Core/ThreadsFunctions.cs index e6d0adf0..061f300a 100644 --- a/Step.Core/ThreadsFunctions.cs +++ b/Step.Core/ThreadsFunctions.cs @@ -7,10 +7,12 @@ using Step.Model.DTOModels.AlarmModels; using Step.Model.DTOModels.Scada; using Step.Model.DTOModels.ToolModels; using Step.NC; +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Threading; using TeamDev.SDK.MVVM; using static CMS_CORE_Library.Models.DataStructures; @@ -967,11 +969,14 @@ public static class ThreadsFunctions else if (cmsError.errorCode != CMS_ERROR_CODES.OK) ncHandler.Dispose(); - Thread.Sleep(1000); - // Send status to UI MessageServices.Current.Publish(SEND_NC_STATUS, null, ncHandler.numericalControl.NC_IsConnected()); - } + + Thread.Sleep(1000); + } + if (ServerConfig.ServerStartupConfig.AutoOpenCmsClient) + StartCMSClient(null); + NcToolTableHandler toolTable = new NcToolTableHandler(); // After reconecction write option @@ -1141,6 +1146,43 @@ public static class ThreadsFunctions return sleep; } + public static void StartCMSClient(string url) + { + //Setup the Path Variable + string CMSClientPath = ""; + if (!ClientIsRunning()) + { + // Check if the system is 64/32 bit + if (Environment.Is64BitOperatingSystem && File.Exists(CLIENT_PATH_64)) + CMSClientPath = CLIENT_PATH_64; + else if (File.Exists(CLIENT_PATH_86)) + CMSClientPath = CLIENT_PATH_86; + + if (!String.IsNullOrEmpty(CMSClientPath)) + Process.Start(CMSClientPath, url); + } + } + private static bool ClientIsRunning() + { + Process[] p = Process.GetProcessesByName(CLIENT_EXE_NAME_NOEXT); + foreach (Process pr in p) + { + if (pr.MainWindowHandle != IntPtr.Zero) + { + ShowWindow(pr.MainWindowHandle, 9); + SetForegroundWindow(pr.MainWindowHandle); + return true; + } + } + return false; + } + + [DllImport("user32.dll")] + static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); #endregion SupportFunctions } \ No newline at end of file diff --git a/Step/program.cs b/Step/program.cs index c23fde69..923471aa 100644 --- a/Step/program.cs +++ b/Step/program.cs @@ -83,8 +83,6 @@ namespace Step // Start listeners ListenersHandler.Start(); - if (ServerStartupConfig.AutoOpenCmsClient) - ServerControlWindow.StartCMSClient(null); // Wait interrupt from client StopRequest.WaitOne();