Files
cms_thermo_active/Step.Client/View/LoadingForm.cs
T
CMS3762\carminatini 2a28a201be Added C# CMS Client
2017-12-01 16:51:09 +01:00

237 lines
7.0 KiB
C#

using CMS_Client.Config;
using Microsoft.WindowsAPICodePack.Taskbar;
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;
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
VersionLBL.Text = Application.ProductName + " V" + Application.ProductVersion;
}
//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 " + CMSConfiguration.FirstUrl + "...", "");
do { } while (!testConnection(new Uri(CMSConfiguration.FirstUrl))) ;
//Open Nc Window
setStatus("Opening the NC Window... ", "");
if (CMSConfiguration.HMINcPresent)
if (!OpenNcWindow())
return;
//Close the Window
closeWindow();
}
//Sub-Method used to read the configuration
private bool readConfiguration()
{
//Set the Status Variable
setStatus("Reading Configuration...", "");
//Read the Config
String Message = CMSConfiguration.InitializeSettings();
//Check the readed Config
if (String.IsNullOrEmpty(Message))
{
setStatus("Configuration ok!", "");
return true;
}
else
{
setStatus("Close the application!", Message);
return false;
}
}
//Sub-Method used to test the connection
private bool testConnection(Uri url)
{
Boolean Connected = false;
//Try to connect
ConnTestRequest = (HttpWebRequest)WebRequest.Create(url);
ConnTestRequest.Timeout = (int)(TimerTest);
ConnTestRequest.KeepAlive = false;
try
{
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: " + CMSConfiguration.FirstUrl, "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
}
}