2

I am currently writing a little Web Service in C#. The only thing I want it to do when I start it, is displaying me a little GUI and instantly open a waiting TcpConnection for incoming requests, which then should be logged to the GUI and being handled ofc...

The problem is, that when I start the application I get no form, no console, nothing...

As usual, the form is being instantiated by Application.Run(new Form1());

My constructor looks like that, as I want the app to instantly open the TcpConnection...

    public Form1()
    {
        InitializeComponent();
        startTcpConn();
    }

I have the feeling, that the waiting for a connection somehow blocks the displaying of my Form :(

I also tried to start the TcpConn with a timeout, which didn't help. The process runs in the background (as you can see in the task manager, but you cannot see the form. Not in Debug or Release mode or even starting it without VS...

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Vinz
  • 3,030
  • 4
  • 31
  • 52
  • Your feeling is right. That's too early to open the connection. You also need to use some kind of asynchronous pattern. – Andrew Barber Jan 12 '14 at 01:29
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 12 '14 at 01:32
  • Show the `startTcpConn();` method. There are a few options for carrying out a connection operation asynchronously. – User 12345678 Jan 12 '14 at 01:34

2 Answers2

0

The easiest way to run the TCP connection method asynchronously is to initiate a new Task. This will run a new operation and immediately return the control to the constructor, which will now be able to finalize.

public Form1()
{
    InitializeComponent();
    Task.Run( ()=>startTcpConn() );
}

You will need to make sure that all UI operations from the new thread will run on the UI thread however, otherwise you would encounter cross-threading exceptions sooner or later.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
0

I'm not sure what startTcpConn but i guess it opens a connection and waits for network data to be received. That basically means you are "pausing" the UI thread and you don't let it handle the UI, so it doesn't show anything.

You should change your code so it does that work on a different thread, and wait to do that after the form has loaded:

public Form1()
{
    InitializeComponent();
}

public void Form1_loaded(object sender, EventArgs e)
{
    Task.Run(() => startTcpConn());
}
i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • Thank you for your tip, this worked perfectly, combined with the Form Event "Shown". I can see my UI :) But I cannot modify elements of my form from the other Task now, as you said. Would there still be a way with events or Workers to modify elements of the Form with my startTcpConn Method? – Vinz Jan 12 '14 at 02:21
  • @user2334932 You can't change the UI controls from a ThreaPool thread. You need to use Invoke to let the UI thread do the change for you. Here is an answer i just wrote to someone: http://stackoverflow.com/a/21070045/885318 – i3arnon Jan 12 '14 at 06:56