Application.Run questions

G

Guest

I need create winForm application with this architecture
I have code

[STAThread]
static void Main()
{
Application.Run();
}

Now I need show Icon in tray bar and attach some "context menu strip"
to this icon. So when user select one menu item - for example with name
"settings"
I need show Settings window and after user change it and close window
I need get it on "tray bar thread" (my main thread) and keep wating some
other action from user.

So. I don't need main window for this app and I have some question here

1) The first question can I show icon in tray if I didn't create form?
2) How show notify icon without creating form?
3) as I see after main thread run this Application.Run()
this thread became frozen. So How I can continue work with it?
 
S

Stoitcho Goutsev \(100\)

Hi
1) The first question can I show icon in tray if I didn't create form?

You don't have to have a form in order to show a notify icon, but most
application does have a main form the icon represents when the application
works on the background.
2) How show notify icon without creating form?

See my sample bellow.
3) as I see after main thread run this Application.Run() this thread
became frozen. So How I can continue work with it?

As long as you don't have to have a form runing the application message loop
is a must. You are right that the thread looks like stopped after call to
the Application.Run, but it is still running and do the job that an UI
thread should do - retrieving message from the message queue and dispatching
them. All the user code that is to be executed in the application should be
in response to these mesages ( in the form of events in .NET Windows
orms )


Here is the example:

class Program
{
static NotifyIcon ni;
static ContextMenu cm;
static void Main(string[] args)
{
cm = new ContextMenu();
cm.MenuItems.Add("Test", OnTestClick);
cm.MenuItems.Add("Exit", OnExitClick);
ni = new NotifyIcon();
ni.ContextMenu = cm;
ni.Icon = new Icon("Test.ico");
ni.Visible = true;
Application.Run();
}

static void OnTestClick(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
static void OnExitClick(object sender, EventArgs e)
{
ni.Visible = false;
Application.Exit();
}
}

HTH
Stoitcho Goutsev (100)


SushiSean said:
I need create winForm application with this architecture
I have code

[STAThread]
static void Main()
{
Application.Run();
}

Now I need show Icon in tray bar and attach some "context menu strip"
to this icon. So when user select one menu item - for example with name
"settings"
I need show Settings window and after user change it and close window
I need get it on "tray bar thread" (my main thread) and keep wating some
other action from user.

So. I don't need main window for this app and I have some question here

1) The first question can I show icon in tray if I didn't create form?
2) How show notify icon without creating form?
3) as I see after main thread run this Application.Run()
this thread became frozen. So How I can continue work with it?
 

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

Top