using Microsoft.WindowsAPICodePack.Taskbar; using Client.Config; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Client.Utils; using System.IO; using CefSharp; namespace CMS_Client.View { public partial class LoadingForm : Form { public const int TimerTest = 2000; private HttpWebRequest ConnTestRequest; private HttpWebResponse ConnTestResponse; private String ConnTestError; private Task ConnTask; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region WINDOW_START_&_BEHAVIOUR_METHOD //Constructor public LoadingForm() { InitializeComponent(); } //OnLoad Method private void LoadingForm_Load(object sender, EventArgs e) { //Force on Desktop 1 this.DesktopLocation = new Point((Screen.PrimaryScreen.Bounds.Width / 2) - (this.Width / 2), (Screen.PrimaryScreen.Bounds.Height / 2) - (this.Height / 2)); //Start Backgroud Task ConnTask = Task.Run(() => BagroundWorker()); //Setup product label if(Environment.Is64BitProcess) VersionLBL.Text = Application.ProductName + " V" + Application.ProductVersion + " - 64 Bit"; else VersionLBL.Text = Application.ProductName + " V" + Application.ProductVersion + " - 32 Bit"; } //Set the status & Messages Labels private void setStatus(String status, String Error) { //Invoke method if is needed or call the method in STD mode if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate () { StatusLBL.Text = status; }); this.Invoke((MethodInvoker)delegate () { ErrorLBL.Text = Error; }); } else { StatusLBL.Text = status; ErrorLBL.Text = Error; } } //Close the Window private void closeWindow() { //Invoke method if is needed or call the method in STD mode if (this.InvokeRequired) this.Invoke((MethodInvoker)delegate () { this.Close(); }); else this.Close(); } //Method used to add the shadow to the Window protected override CreateParams CreateParams { get { const int CS_DROPSHADOW = 0x20000; CreateParams cp = base.CreateParams; cp.ClassStyle |= CS_DROPSHADOW; return cp; } } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region CONNECTING_TASK_METHOD //Main Void of the background Task private void BagroundWorker() { //Show the loading state on the app ICON this.Invoke((MethodInvoker)delegate () {TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Indeterminate, this.Handle); }); //Read the Configuration if (!readConfiguration()) return; //try to Request setStatus("Connecting to " + Config.ConnectionConfig.Url + "...", ""); do { } while (!testConnection(new Uri(Config.ConnectionConfig.Url))) ; //Open Nc Window setStatus("Opening the NC Window... ", ""); if (Config.VendorHmiConfig.Enabled) if (!OpenNcWindow()) return; //Setup The Browser if (!SetupBrowser()) return; //Close the Window closeWindow(); } //Sub-Method used to SetupThe Browser Environment private bool SetupBrowser() { //Delete Browser Cache try { if (Config.ConnectionConfig.DeleteCahceFolderOnStartup && Directory.Exists(Constants.BROWSER_CACHE_FOLDER)) { setStatus("Deleting Browser Chache Folder...", ""); Directory.Delete(Constants.BROWSER_CACHE_FOLDER, true); } } catch (Exception E) { setStatus("Close the application!", E.Message); return false; } return true; } //Sub-Method used to read the configuration private bool readConfiguration() { //Set the Status Variable setStatus("Reading Configuration...", ""); //Read the Config try { ConfigController.ReadStartupConfig(); setStatus("Configuration ok!", ""); return true; } catch (Exception E) { setStatus("Close the application!", E.Message); return false; } } //Sub-Method used to test the connection private bool testConnection(Uri url) { Boolean Connected = false; //Try to connect try { ConnTestRequest = (HttpWebRequest)WebRequest.Create(url); ConnTestRequest.Timeout = (int)(TimerTest); ConnTestRequest.KeepAlive = false; ConnTestResponse = (HttpWebResponse)ConnTestRequest.GetResponse(); ConnTestError = ConnTestResponse.StatusCode.ToString(); Connected = (ConnTestResponse.StatusCode == HttpStatusCode.OK) ? true : false; } catch (WebException ex) { Connected = false; ConnTestError = ex.Status.ToString(); } //Check if it's connected if (Connected) { setStatus("Connected!", ""); return true; } else { setStatus("Retry connection to: " + Config.ConnectionConfig.Url, "Server not found (Error:" + ConnTestError + ")"); return false; } } //Sub-Method used to open the NC Window private bool OpenNcWindow() { switch(NcWindow.StartNcWindow()) { case 0: return true; case 1: { setStatus("Close the application!", "NC Path not found: " + NcWindow.ProcessPath); return false; }; case 2: { setStatus("Close the application!", "Unable to start the NC application: "); return false; }; } return false; } #endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #region CLOSE_BUTTON_BEHAVIUR //On click Method private void CloseLabel_Click(object sender, EventArgs e) { Environment.Exit(0); } //On Mouse Enter private void CloseLabel_MouseEnter(object sender, EventArgs e) { CloseLabel.ForeColor = Color.DarkRed; } //On Mouse Leave private void CloseLabel_MouseLeave(object sender, EventArgs e) { CloseLabel.ForeColor = Color.White; } #endregion } }