-1

In summary: I have a console app for a server with sockets and I wanted to have the functionality to be in WPF if the user types in -w as args[]. The problem is that when I call the RunServer() method the listener is listening and the WPF window is frozen. The only update I want on the window is with SendEvent() method which adds the message to the window TextBox. I tried with creating threads, background worker but nothing seems to be working. When it actually goes to the line which changes the text, there is an exception that states "The calling thread cannot access this object because a different thread owns it ".Could someone suggest a solution? The only advice I didn't do was "Switch to .Net Core".

if (GUI)
{
                 
    Window1 window = new Window1();
    RunServer();
}
public Window1(bool saving, bool logging)
        {
            InitializeComponent();
            IsSavingLogging(saving, logging);
            Events.Text += "Test\r\n";
            try
            {
                Show();
                Update("Test2\r\n");//this doesn't work
             
            }
            catch (Exception e)
            {
                // if there was an error in the processing- catch it and note the details
                //Update( "Exception: " + e.ToString());
            }
        }
public static void RunServer(Window1 pWindow1)
        {
            TcpListener listener;
            Socket connection;
            Handler requestHandler;
            try
            {
                //create a tcp socket to listen on port 43 fo incoming requests
                // and start listening
                listener = new TcpListener(IPAddress.Any, 43);
                SendEvent( "Server Started", GUI,pWindow1);
                listener.Start();

                while (true)
                {
                    connection = listener.AcceptSocket();
                    requestHandler = new Handler();
                    Thread t = new Thread(() => requestHandler.DoRequest(connection,pWindow1));
                    t.Start();

                }
            }
            catch (Exception e)
            {
                // if there was an error in the processing- catch it and note the details
                SendEvent( "Exception: " + e.ToString(),GUI,pWindow1);
            }

        }
private static void SendEvent(string pMessage, bool pGui,Window1 window1)
        {
            
            if (pGui)
            {
                window1.Events.Text += pMessage+"\r\n";
            }
            else {
                Console.WriteLine(pMessage);
            }
        }
MaykalD
  • 1
  • 3
  • I am not sure the relationship between your console app and WPF app, but you can at least fix the exception. https://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it – emoacht Mar 15 '21 at 15:38

1 Answers1

-1

If you are here because you are in the same position as me, I found that I can just create another unresponsive window and have the text there. I added

this.Show();
Thread.Sleep(15000);//15 seconds timeout
this.Close();

And this does the job Also, you have to call this window from a separate thread which is

Thread t= new Thread(()=> window=new Window(pMessage));

At least I hope this helps someone

MaykalD
  • 1
  • 3