Sign in to follow this  
Kryptus

[.net] Async Sockets, Threads, and a Problem

Recommended Posts

I've made a server using Asynchronous sockets. I've used threads on them as well. The problem is once the server starts listening for incoming connections my application is designed to put itself with the tray icons. This all works fine but when you right click the icon nothing happens. No menu appears! I was told that I have to put that on a separate thread or something, but where do I start looking for stuff like this? I've searched the Internet and haven't found anything so far... Any help is appreciated!!

Share this post


Link to post
Share on other sites
Here's what I have done so far, but how would I get the menu to display?


TrayIcon.Click += new EventHandler(TrayIcon_Click);
TrayIcon.Text = "Smoke Server";
TrayIcon.Icon = Program.FormMain.Icon;
TrayIcon.Visible = true;
TrayIcon.ShowBalloonTip(1, "My Server", "The server is now listening for incoming connections...", ToolTipIcon.Info);

private void TrayIcon_Click(object sender, System.EventArgs e)
{
// Something similar to this???
TrayMenu.Show(null, new System.Drawing.Point(xpos, ypos));
}



Do I need to run this on a separate thread? Being that the sockets are all running on threads?

Share this post


Link to post
Share on other sites
Quote:
Original post by Kryptus
Here's what I have done so far, but how would I get the menu to display?

*** Source Snippet Removed ***

Do I need to run this on a separate thread? Being that the sockets are all running on threads?


You probably need to make sure that you create the popup menu in the same thread as your main message loop. Isn't there a .NET method similar to the Win32 API call TrackPopupMenu()?

Share this post


Link to post
Share on other sites
Are you using the .NET Notify Icon for this?

If you are, you need to attach a context menu to it (in the designer).

Share this post


Link to post
Share on other sites
If I create and show the tray icon and menu in the initial thread, then the menu works. When a user connects I have it so a balloon appears. This is in another thread at the time and nothing happens. How do I access the first thread to do this? Or is there another way?

Share this post


Link to post
Share on other sites
Your last message seems to imply that you are manipulating the GUI from another thread. If it's the case: don't do it.

From the MSDN doc, the only methods that are safe to call on a Control derived class are: CreateGraphics, Invoke, BeginInvoke and EndInvoke. All other methods have to be called from the thread that created the controls. If you need to call those from another thread, use the Invoke methods.

Share this post


Link to post
Share on other sites
In the designer window, create a ContextMenu. Assign it to the NotifyIcon.ContextMenu. If you need menu item customizations, override the ContextMenu.Popup

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this