Click here to Skip to main content
15,896,726 members
Articles / Web Development / ASP.NET

Control your Distributed Application using Windows and Web Service

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
15 Aug 2010CPOL4 min read 23K   606   19  
Control your distributed application using Windows and web service
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data;
//using System.ServiceProcess;
namespace winServiceAndWindowsInterface 
{
	class serviceInterface:  System.Windows.Forms.Form
	{
		#region MyDeclarations 
		static	private NotifyIcon serviceIcon;
		static private ContextMenu serviceContext ;
		static private System.IO.FileSystemWatcher notifyFileWatcher = new System.IO.FileSystemWatcher();
		static public NotifyFrm frm= new NotifyFrm();
		/// <summary>
		/// Service Controller
		/// </summary>
		static	private System.ServiceProcess.ServiceController windowsServiceController;
		static private string ServiceName = "Server1WindowsService";
		static private string strTip ="Monitor/Control Windows Service";

		#region checker Timer
		static Timer CheckerTimer = new Timer();
		#endregion
		#endregion
		private static void MyInit()
		{
			structConfig.LoadiniVaues();
			NotifyFrm frm = new NotifyFrm();
			#region Checker Timer 
			CheckerTimer.Interval = structConfig.RegisteredInterval;
			CheckerTimer.Tick+=new EventHandler(CheckerTimerTic);
			CheckerTimer.Enabled = true;
			#endregion
			// 
			// notifyFileWatcher
			// 
			notifyFileWatcher.Path=structConfig.prifePath;
			notifyFileWatcher.EnableRaisingEvents = true;

			notifyFileWatcher.NotifyFilter = System.IO.NotifyFilters.Size;
			//notifyFileWatcher.SynchronizingObject = this;
			notifyFileWatcher.Changed+=new System.IO.FileSystemEventHandler(notifyFileWatcher_Changed);

			///Structure 
			StructDisplayInfo mynotifyInfo = new StructDisplayInfo();
			mynotifyInfo.initilize();
			//myNotifies = new notifies();
			//
		#region context And Menue
			try
			{
				serviceIcon = new NotifyIcon();
				//tray Tip
				serviceIcon.Text = strTip;
				//
				serviceIcon.Visible = false;
				serviceContext = new ContextMenu();
				CreateMenu();
				serviceIcon.ContextMenu = serviceContext;
				serviceIcon.Visible = true;
			}
			catch (Exception ex)
			{
			MessageBox.Show(ex.Message);
			}
			#endregion
		}
		# region MainFunctions


		#endregion
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{	
			MyInit();
			frm.Show();
			frm.Hide();
			Application.Run();
		}
		#region Timer Functions 
		private static void CheckerTimerTic(object sender, EventArgs e)
		{
			utilities.addtoLog("(checkTimerTic)"+DateTime.Now.ToString());
			ServerStatusChecker.GetServersStatus(true);
		}
		#endregion
		#region Context Operations 
		static private void CreateMenu()
		{
			serviceContext.MenuItems.Add("Monitor");
			serviceContext.MenuItems[0].Click+=new EventHandler(Monitor);
			// serviceContext.MenuItems.Add("Others");
			// serviceContext.MenuItems[1].Click+=new EventHandler(Register);
			serviceContext.MenuItems.Add("Configuration");
			serviceContext.MenuItems[1].Click+=new EventHandler(Configration);
			serviceContext.MenuItems.Add("View Log Files");
			serviceContext.MenuItems[2].Click+=new EventHandler(ViewLogFiles);
			//
			serviceContext.MenuItems.Add("Stop Service");
			serviceContext.MenuItems[3].Click+=new EventHandler(StopService);
			serviceContext.MenuItems.Add("Run Service");
			serviceContext.MenuItems[4].Click+=new EventHandler(RunService);
			//
			///Get Status & set Icon 
			///
			switch (GetServiceStatus())
			{
				case 1:
					serviceContext.MenuItems[4].Enabled = false;
					IconChange(new Icon(structConfig.skinServiceRunningIconPath));
					break;
				case 0:
					serviceContext.MenuItems[3].Enabled = false;
					IconChange(new Icon(structConfig.skinServiceStopIconPath));
					break;
			}
			///End Get & set 
			 ///
			//Checker Timer Menue
			serviceContext.MenuItems.Add("Checker ON");
			serviceContext.MenuItems[5].Enabled=false;
			serviceContext.MenuItems[5].Click+=new EventHandler(CheckerON);
			serviceContext.MenuItems.Add("Checker OFF");
			serviceContext.MenuItems[6].Click+=new EventHandler(CheckerOFF);


			//ShowAllNotificationsForm 
			serviceContext.MenuItems.Add("ShowAllNotifications");
			serviceContext.MenuItems[7].Click+=new EventHandler(ShowNotifiesForm);



			//exit
			serviceContext.MenuItems.Add("Exit");
			serviceContext.MenuItems[8].Click+=new EventHandler(Exit);
		}
		static  void Monitor(object sender, EventArgs e)
		{
			Monitor myMonitor = new Monitor();
			myMonitor.Show();
		}
		private static void ViewLogFiles (object sender, EventArgs e)
		{
			frmLogFiles myLogFilesfrm = new frmLogFiles();
			myLogFilesfrm.Show();
		}

		private static void Exit(object sender, EventArgs e)
		{
			serviceIcon.Visible = false;
		Application.Exit();
		}
		private static void Configration(object sender, EventArgs e)
		{
		frmConfigration myConfigration = new frmConfigration();
		myConfigration.Show();
		}

		private static void StopService(object sender, EventArgs e)
		{
			windowsServiceController =  new System.ServiceProcess.ServiceController(ServiceName);
			if (windowsServiceController.Status==System.ServiceProcess.ServiceControllerStatus.Running)
			{
				windowsServiceController.Stop();
				serviceContext.MenuItems[3].Enabled = false;
				serviceContext.MenuItems[4].Enabled=true;
				/// Display info
				/// 
				utilities.Notify(StructDisplayInfo.Typeenum.Alart,"Stopped","Service Now Is stopped","Stopped!",true);
				IconChange(new Icon(structConfig.skinServiceStopIconPath));
				///End Display
			}
		}
		private static void CheckerON(object sender, EventArgs e)
		{
			CheckerTimer.Enabled=true;
			serviceContext.MenuItems[5].Enabled = false;
			serviceContext.MenuItems[6].Enabled = true;
			utilities.addtoLog("(CheckerOn)"+DateTime.Now.ToString());
			utilities.Notify(StructDisplayInfo.Typeenum.Notify,"Checker ON","Timer Checker Now Enabled");
		}
		private static void CheckerOFF(object sender, EventArgs e)
		{
			CheckerTimer.Enabled = false;
			serviceContext.MenuItems[5].Enabled = true;
			serviceContext.MenuItems[6].Enabled = false;
			utilities.addtoLog("(CheckerOFF)"+DateTime.Now.ToString());
			utilities.Notify(StructDisplayInfo.Typeenum.Alart,"Checker OFF","Timer Checker Now Disabled");
		}
		
		private static void RunService(object sender, EventArgs e)
		{
			windowsServiceController =  new System.ServiceProcess.ServiceController(ServiceName);
			if (windowsServiceController.Status==System.ServiceProcess.ServiceControllerStatus.Stopped)
			{
				windowsServiceController.Start();
				serviceContext.MenuItems[3].Enabled = true;
				serviceContext.MenuItems[4].Enabled=false;

				/// Display Fast info
				/// 
				utilities.Notify(StructDisplayInfo.Typeenum.Alart,"Running","Service Now Is Running","Service Running",true);
				IconChange(new Icon(structConfig.skinServiceRunningIconPath));
				///End Display
			}
		}
		//File Watcher Sensor
		private static void notifyFileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
		{
Notify (e.FullPath);
			//utilities.Notify(e.FullPath);
		}
		#endregion
		#region utilities Functions
		static int GetServiceStatus()
		{
			windowsServiceController = new System.ServiceProcess.ServiceController(ServiceName);
			windowsServiceController.Refresh();
			switch( windowsServiceController.Status)
			{
				case System.ServiceProcess.ServiceControllerStatus.Running:
					return 1;
				case System.ServiceProcess.ServiceControllerStatus.Stopped:
					return 0;
				default :
					return -1;
			}
		}
		protected  void Finalize()
		{
			serviceIcon.Dispose();
		}
		static void IconChange(Icon inIcon)
		{
			serviceIcon.Icon = inIcon;
			serviceIcon.Visible = true;
		}
		#endregion

		private static void ShowNotifiesForm(object sender, EventArgs e)
		{
			frmShowEvents myEvents = new frmShowEvents();
			myEvents.Show();
		}



		/// <summary>
		/// Filing informations in Structure to Display it 
		/// </summary>
		/// <param name="fileName">file name to Extract event from to Notify it</param>
		/// <returns></returns>
		public static void  Notify(string fileName)
		{
			StructDisplayInfo myInfo = new StructDisplayInfo();
			myInfo.initilize();
			// Filling structure (Acording to File Content) Sample :)
			myInfo.TypeInt =(int) StructDisplayInfo.Typeenum.Notify;
			myInfo.fileName = fileName;
			myInfo.ShowForm = true;
			myInfo.AddtoDatabase = true;
			myInfo.popupTitle = "File Recived";
			myInfo.popupPrifeInfo="File recived";
			myInfo.NotifySourceInt =(int) StructDisplayInfo.NotifySourceEnum.UnSpacified;
			myInfo.details = utilities.GetFileContent(fileName);
			NotifyMe myNotify = new NotifyMe(myInfo);
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader ArabicRobotics.com
Egypt Egypt
Tareq Gamal El-din Mohammed,
---------
Website:
www.ArabicRobotics.com

---------

Graduated from Modern Academy for Computer science and Information Technology. Egypt,
Then flow Microsoft development track Certificates:
MCAD.NET (Microsoft Certified Application Developer)
MCSD.NET (Microsoft Certified Solution Developer)
Microsoft SharePoint Administration, Configuration and Development.

Robotics fields was a Hobby since 2002,
started to develop some applications for "Robosapien", "RoboSapienV2", "RS Media", RoboMe and WowWee Rovio. from WowWee company,

Started working with robots as a professional way at 2014
By using "NAOqi" Robotics from Aldebaran.

By developing some applications and libraries like :
NAO.NET.
https://www.youtube.com/watch?v=oOyy-2XyT-c

OpenCV with NAO Robot:

- NAORobot Vision using OpenCV -TotaRobot P1
https://www.youtube.com/watch?v=MUcj8463x08

- NAO Robot Vision using OpenCV - P2
https://www.youtube.com/watch?v=93k1usaS-QM

NAO Alarm Clock :
https://www.youtube.com/watch?v=djLlMeGLqOU
-----------------------------

also Robotic Arm Project:


Other Projects Developed by Tareq Gamal El-din Mohammed :

Developed and posted some applications in Code Project web site like :

- Control your Distributed Application using Windows and Web Service
http://www.codeproject.com/Articles/101895/Control-your-Distributed-Application-using-Windows


- Quick and dirty directory copy
http://www.codeproject.com/Articles/12745/Quick-and-dirty-directory-copy

- Program Execute Timer(From the Web)
http://www.codeproject.com/Articles/12743/Program-Executer-Timer

Comments and Discussions