c# - Class Inheriting from Mainwindow causes "the calling thread must be sta because many UI components require this" -


i'm working on chat server receives connections multiple clients , sends/receives messages.

this how gets connections clients:

public void startserver()     {         tcplistener = new tcplistener(ipaddress.any, 60000);         tcplistener.start();          listentask = task.factory.startnew(() => listenloop());     }     private async void listenloop()     {         int = 0;         (; ; )         {             var socket = await _tcplistener.acceptsocketasync();             if (socket == null)                 break;              var c = new client(socket, i);              i++;         }     }////got code somewhere here, not want use (discussed down) 

this client class:

public class client {     //irrelevant stuff here//     public client(socket socket, int number)      {         //irrelevant stuff here//         thread ct = new thread(this.run);         ct.start();     }     public void run()     {         writer.write("connected"); //testing connection         while (true)         {             try             {                  string read = reader.readstring();                // dispatcher.invoke(new displaydelegate(displaymessage), new object[] { "[client] : " + read });             }////////not working, because client needs inherit mainwindow.             catch (exception z)             {                 messagebox.show(z.message);             }          }     } } 

ok problem is, update ui client class must inherit mainwindow, when does, "the calling thread must sta because many ui components require this" error. when doesn't inherit works fine.

another problem is, want use client[] clients array , when user connects, adds him array can individually write/read to/from specific clients.

while (true) {    try    {       clients[counter] = new client(listener.acceptsocket(), counter);       counter ++;       messagebox.show("client " + counter.tostring());    }    catch (exception e) { messagebox.show(e.message); }  } 

problem here is, "object refrence not set instance of object" when client connects.

any ideas how fix both/any of these problems? sorry code might bit messed tried lots of stuff working ended lots of junk in code. in advance.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -