RE: Create Form from Async-Callback

Tech-Archive recommends: Fix windows errors by optimizing your registry



Thanks, I'll have a look at the Cassini Sample Web Server.

The Problem with your code in the post before was, that, le'ts say, every
Client that connects would open a Form. So that means there could be
unlimited Forms so I need do genereate them when a Client connects.

I by now found a little Workaround which looks like:

/************ CODE ************/
using System;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
using System.Threading;

class Program {
static SynchronizationContext ctx;

static void Main(string[] args) {
Control ctl = new Control();
ctx = SynchronizationContext.Current;
StartListening();
Application.Run();
}

static void StartListening() {
TcpListener m_tcpListener = new TcpListener(IPAddress.Any, 1234);
m_tcpListener.Start();
m_tcpListener.BeginAcceptTcpClient(new
AsyncCallback(AcceptTcpClientCallback), m_tcpListener);
}

private static void AcceptTcpClientCallback(IAsyncResult ar) {
TcpListener listener = (TcpListener)ar.AsyncState;
TcpClient client = listener.EndAcceptTcpClient(ar);
listener.BeginAcceptTcpClient(new
AsyncCallback(AcceptTcpClientCallback), listener);

// This Method has to run in the "Main Thread", where
Application.Run() was fired
Program.ctx.Send(new SendOrPostCallback(CreateForm), null);
}

static void CreateForm(object o) {
Form myForm = new Form();
myForm.Show();
}
}
/************ END CODE ************/

Well, the most important part is that I added an SynchronizationContext
which I set on the Main Thread and then run the CreateForm on that Context
(ctx.Send()). So this solution is working so far but has something that
annoys me:

The first Line in the Main() is a
Control ctl = new Control();
Without this line, the SynchronizationContext.Current would be null so I
can't Send something to it.

I looked around in Reflector and found in the Constructor of a Control the
Line:
WindowsFormsSynchronizationContext.InstallIfNeeded();

Most of what's going on in this Function is Internal so I can't just call it
in my Programm.
So I guess I have to life with that.

So, do you think that this solution with the SynchronizationContext and it's
Send and the dummy Control is ok and solid?

Thanks!
//Roman
.



Relevant Pages

  • Re: Event Processing
    ... public event MessageArrivedHandler MessageArrivedLocally; ... static void Main ... Console.WriteLine("Event Server started, press to ... Client Code: ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Event Processing
    ... public class BroadcastEventWrapper: MarshalByRefObject ... static void Main ... Console.WriteLine("Event Server started, press to ... Client Code: ...
    (microsoft.public.dotnet.distributed_apps)
  • Re: Reading data on Aysnchronous socket server
    ... This could be because they wanted to save a buffer copy to join the ... | To test this I have a simulated client that sends 100 byte packets. ... | I have set up the socket server so that its buffer is bigger than this. ... | public static void AcceptCallback{ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: dip Notions 2 Major Errors
    ... "static void Main" is *NOT* the first executed line of code. ... configuration files, evaluates code access security policies, creates ... To the Runtime the client is ... writing an additional remoting channel, you implement their interface, ...
    (comp.object)