C
C#4mo ago
Stackz

Problem with RichTextBox rendering

I have a server that receives messages from clients, the problem is. when i click on the tabGroup for the Message receiver and then the client sends a message, its fine. But if they send a message before I open the tabGroub this error occurs. The message goes into a RichTextBox btw
No description
31 Replies
Pobiega
Pobiega4mo ago
Wildly guessing here, but I dont think there is a proper solution to this, just a work around - make sure that tab gets visited before any message is received. Potentially by making it the default tab, or just force swapping to it at least once
leowest
leowest4mo ago
how is the tab pages and controls added? because I have no such issue out of the box that updating the richtextbox crashes without having the tab page open I assume u might be doing one of the 2, updating it from a different thread creating it dinamycally and updating it
Pobiega
Pobiega4mo ago
updating from a non-UI thread seems like a likely guess if that is the case, the easiest fix is to just wrap the actual update of the UI (ie textbox.Add or whatever) in an action and pass it to form.Dispatcher
Stackz
Stackz4mo ago
c#
public Main(TcpServer server)
{
InitializeComponent();
tcpServer = server;
tcpServer.MessageReceived += TcpServer_MessageReceived;
}

private void TcpServer_MessageReceived(object sender, string message)
{
Logger.Log("Received from client: " + message + "\n", TaskLogger);
}
c#
public Main(TcpServer server)
{
InitializeComponent();
tcpServer = server;
tcpServer.MessageReceived += TcpServer_MessageReceived;
}

private void TcpServer_MessageReceived(object sender, string message)
{
Logger.Log("Received from client: " + message + "\n", TaskLogger);
}
`
c#
public event EventHandler<string> MessageReceived;
c#
public event EventHandler<string> MessageReceived;
c#
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();

byte[] messageBuffer = new byte[4096];
int bytesRead;

while (true)
{
try
{
// read
bytesRead = clientStream.Read(messageBuffer, 0, messageBuffer.Length);

if (bytesRead == 0)
{
// disconnected
break;
}
string receivedMessage = Encoding.ASCII.GetString(messageBuffer, 0, bytesRead);

MessageReceived?.Invoke(this, receivedMessage);
}
catch (Exception ex)
{
// todo
break;
}
}

tcpClient.Close();
}
c#
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();

byte[] messageBuffer = new byte[4096];
int bytesRead;

while (true)
{
try
{
// read
bytesRead = clientStream.Read(messageBuffer, 0, messageBuffer.Length);

if (bytesRead == 0)
{
// disconnected
break;
}
string receivedMessage = Encoding.ASCII.GetString(messageBuffer, 0, bytesRead);

MessageReceived?.Invoke(this, receivedMessage);
}
catch (Exception ex)
{
// todo
break;
}
}

tcpClient.Close();
}
HandleClientComm method is inside the TcpServer class
leowest
leowest4mo ago
that is not enough information unhappily do u have a github of this project?
Stackz
Stackz4mo ago
Yes but its private
Pobiega
Pobiega4mo ago
can you unprivate it for a bit?
Stackz
Stackz4mo ago
No permissions
leowest
leowest4mo ago
well we need t oknow how the tabcontrol is added how the pages are added and how the logger gets there
Pobiega
Pobiega4mo ago
(and how the tcpserver is threaded)
Stackz
Stackz4mo ago
Ok ill provide everything
leowest
leowest4mo ago
we also need 1 bar of gold each
Stackz
Stackz4mo ago
Sure, I gotchu after $paste
MODiX
MODiX4mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
leowest
leowest4mo ago
:catlaugh:
Stackz
Stackz4mo ago
BlazeBin - nmbvofjmfbwx
A tool for sharing your source code with the world!
Stackz
Stackz4mo ago
I'm guessing this is how the RichTextBox is getting added
Stackz
Stackz4mo ago
BlazeBin - emqxqprdumqo
A tool for sharing your source code with the world!
Stackz
Stackz4mo ago
And this is how the TcpServer runs
leowest
leowest4mo ago
yep looks good ah I see so the window and hidden and u want to update the box?
Stackz
Stackz4mo ago
Well, in the second paste thats a different form Just asking for a port number, which then starts the server and runs the main form
leowest
leowest4mo ago
ah ok
Stackz
Stackz4mo ago
BlazeBin - hpdfawvxfhkx
A tool for sharing your source code with the world!
Stackz
Stackz4mo ago
TcpServer class
leowest
leowest4mo ago
can u show the form where the tabs are
Pobiega
Pobiega4mo ago
Sorry to hijack but @leowest I need your help for an urgent thing, check your dms real quick
Stackz
Stackz4mo ago
The code or what
leowest
leowest4mo ago
is it the serverinfo?
Stackz
Stackz4mo ago
No That would be the main form
leowest
leowest4mo ago
akjshdjaksd so you keep it hidden after u take the input
Stackz
Stackz4mo ago
What do you mean by take the input Like the message? From the client* Even when the main form is open and the TaskLogger (RichTextBox) hasn't been loaded, it still crashes
Want results from more Discord servers?
Add your server
More Posts