|
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.