Socket Question

  • Thread starter Thread starter jm
  • Start date Start date
J

jm

Easy probably, please read on. I know some of you have commented
already about some of my socket question. I appreciate that.

I have a Form1:

static void Main()
{
Application.Run(new Form1());
clsListening.AsynchronousSocketListener.StartListening();

}

In the clsListening below is the code. It is all straight from MSDN.
The problem is this:

If I run this the Form will load, but the the next line will not
execute until the form Form1 is closed (I just hit the X.) I know
this is true because netstat -a | find "8002" reveals it is not
listenting. Close the form and voila it is listening.

I do not understand why it is doing this. Please help me understand
this behavior. BTW, the listener and all works, I have to have the
form running because I need an icon in the system tray.

Also, if I reverse the two the Form1 will never run, because of the
While(true) for the listener (at least I think that is why.)

I just want the listener to run and the form to run:

I thought I would try this, but it failed with the same results,
except the Form1 never ran:

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
clsListening.AsynchronousSocketListener.StartListening();
}

The slight difference here was that the icon was in the system tray,
but the form was not visible! Thank you for any help here.
 
I had a similar problem, I needed to program an application with a GUI that
could be started or run alone.

When you call Application.Run, the thread is passed to the form and until
the form is disposed won't come back.

I useded a second thread to run the application, I call a method that start
the second thread and then the Application.Run

public void StartAutoRun()
{
System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(this.AutoRun));
}

protected void AutoRun(object state)
{
....
}

public static void Main(string[] args)
{
Form1 f=new Form1();
if (args.Length>0) f.StartAutoRun();
Application.Run(f);
}
 
Start the listener before you open the form. Application.Run returns only
when the form is closed.

clsListening.AsynchronousSocketListener.StartListening();
Application.Run(new Form1());

Willy.
 
I ended up with this:

static void Main()
{
// Queue the task.
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new
Form1());
Application.Run();
}

static void ThreadProc(Object stateInfo)
{
// When no state object was passed to QueueUserWorkItem, so
// stateInfo is null.
Form1 f1 = (Form1) stateInfo; //the passed form
f1.notifyIcon1.Icon = new
Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Listener1.connecting.ico"));
clsListening myLis = new clsListening(f1);
myLis.StartListening(f1);
}
 
Hi,
inline

<snip>

If you have a function that blocks then call it from a workerthread:

static void Main()
{
Application.Run(new Form1());
}


private void Form1_Load(object sender, System.EventArgs e)
{
Thread tr = new Thread( new ThreadStart( Run ) )
tr.Start();
}

private void Run()
{
clsListening.AsynchronousSocketListener.StartListening();
}

You will need to deal with marshalling between UI and workerthread. (See
Control.Invoke)

Another solution is to make AsynchrounousSocketListener really asynchronous
and make StartListening return immediatly.


HTH,
greetings
 
Back
Top