notifyicon and context menu on windows service

M

Michael Yip

Dear All :

I encounter the problem on create windows service program

I have create c# class jobservice the code as below :

partial class sampleService : ServiceBase
{
private System.Windows.Forms.NotifyIcon TheIcon;
private System.Windows.Forms.ContextMenu TheMenu;
private System.Windows.Forms.MenuItem TheMenuItem;
private System.ComponentModel.IContainer components;

static void Main()
{
ServiceBase.Run(new sampleService());
}
public sampleService()
{
this.ServiceName = "sampleService";
this.CanStop = true;
this.CanPauseAndContinue = true;
this.AutoLog = true;

InitializeComponent();
}

protected override void OnStart(string[] args)
{
this.components = new System.ComponentModel.Container();
this.TheMenu = new System.Windows.Forms.ContextMenu();
this.TheMenuItem = new System.Windows.Forms.MenuItem();
this.TheMenu.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { this.TheMenuItem });


this.TheMenuItem.Index = 0;
this.TheMenuItem.Text = "E&xit";
this.TheMenuItem.Click += new
System.EventHandler(this.TheMenuItem_Click);
this.TheIcon = new
System.Windows.Forms.NotifyIcon(this.components);

TheIcon.Icon = new Icon("IcoOpen.ico");
TheIcon.ContextMenu = this.TheMenu;
TheIcon.Text = "sampleService";
TheIcon.Visible = true;
TheIcon.DoubleClick += new
System.EventHandler(this.TheIcon_DoubleClick);
TheIcon.MouseClick += new MouseEventHandler(TheIcon_MouseClick);
// TODO: Add code here to start your service.
}
void TheIcon_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("run here");
//throw new Exception("The method or operation is not
implemented.");
}
protected override void OnStop()
{

}
private void TheMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("stop");
}

private void TheIcon_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show("run here");
}

I have sccess install on windows service, and the serivce properties have
check "allow service to interact with desktop" and can start the service,
the icon can show on system tray. But it still cannot show contxt menu,
whatever I use mouse click , right click or double click

Any wrong in my coding

Pls kindly advise to me


Thanks

Michael
 
F

Frank Uray

Hi Michael

Well, I am not sure if this is really possible.

What you maybe can do is, instead of creating a
Windows Service, just create it as NotifyIcon.
Or you create a Windows Service and a NotifyIcon
Process Application corresponding with your Service.

Best regards
Frank Uray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top