Question

I'm writing an application that resides in a tray icon, no forms.

I already implemented a custom ApplicationContext. This custom ApplicationContext creates the tray icon and adds a context menu. If I open that context menu and choose Exit, the application closes as expected. I also have a class that holds the functionality that the application will use.

Code as follows:

public class MyClassApplicationContext : ApplicationContext
{
    public Container container;
    public NotifyIcon trayIcon;
    public ContextMenuStrip contextMenu;

    public MyClassApplicationContext()
    {
        InitializeContext();
    }

    private void InitializeContext()
    {
        container = new Container();
        contextMenu = new ContextMenuStrip(container);
        contextMenu.Items.Add("Salir");
        contextMenu.Items[0].Click += MenuExit_Click;
        trayIcon = new NotifyIcon(container);
        trayIcon.Icon = Resources.IconTray;
        trayIcon.ContextMenuStrip = contextMenu;
        trayIcon.Visible = true;
    }

    private void MenuExit_Click(object sender, EventArgs e)
    {
        ExitThread();
    }

    protected override void ExitThreadCore()
    {
        trayIcon.Visible = false;
        base.ExitThreadCore();
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && container!= null)
        {
            container.Dispose();
        }
        base.Dispose(disposing);
    }
}

public class MyClass
{
    public string fileName;

    public bool CheckConditions()
    {
        if (Environment.GetCommandLineArgs().Count() < 2)
        {
            MessageBox.Show("No file specified.");
            return false;
        }
        fileName = Environment.GetCommandLineArgs().ElementAt(1).ToString();
        if (!File.Exists(fileName))
        {
            MessageBox.Show("File specified does not exist.");
            return false;
        }
        return true;
    }
}

Problem is, if I add the following in the MyClassApplicationContext constructor:

MyClass main = new MyClass();
if (!main.CheckConditions())
{
    ExitThread();
}

the application disposes of the tray icon, but stays open.

Any pointers in the right direction to solve this would be greatly appreciated.

Was it helpful?

Solution

I would check the conditions before even launching the application

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        MyClass main = new MyClass();
        if (main.CheckConditions()) {
            MyClassApplicationContext context = new MyClassApplicationContext();
            Application.Run(context);
        }
    }
}

It would probably be easier to use, if the condition check would be a public member of the context.

var context = new MyClassApplicationContext();
if (context.CheckConditions()) {
    Application.Run(context);
}

This CheckConditions method could of course be a wrapper that in turn calls the corresponding method of MyClass.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top