C
C#9mo 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
Pobiega9mo 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
leowest9mo 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
Pobiega9mo 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
StackzOP9mo 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
leowest9mo ago
that is not enough information unhappily do u have a github of this project?
Stackz
StackzOP9mo ago
Yes but its private
Pobiega
Pobiega9mo ago
can you unprivate it for a bit?
Stackz
StackzOP9mo ago
No permissions
leowest
leowest9mo ago
well we need t oknow how the tabcontrol is added how the pages are added and how the logger gets there
Pobiega
Pobiega9mo ago
(and how the tcpserver is threaded)
Stackz
StackzOP9mo ago
Ok ill provide everything
leowest
leowest9mo ago
we also need 1 bar of gold each
Stackz
StackzOP9mo ago
Sure, I gotchu after $paste
MODiX
MODiX9mo 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
leowest9mo ago
:catlaugh:
Stackz
StackzOP9mo ago
BlazeBin - nmbvofjmfbwx
A tool for sharing your source code with the world!
Stackz
StackzOP9mo ago
I'm guessing this is how the RichTextBox is getting added
Stackz
StackzOP9mo ago
BlazeBin - emqxqprdumqo
A tool for sharing your source code with the world!
Stackz
StackzOP9mo ago
And this is how the TcpServer runs
leowest
leowest9mo ago
yep looks good ah I see so the window and hidden and u want to update the box?
Stackz
StackzOP9mo 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
leowest9mo ago
ah ok
Stackz
StackzOP9mo ago
BlazeBin - hpdfawvxfhkx
A tool for sharing your source code with the world!
Stackz
StackzOP9mo ago
TcpServer class
leowest
leowest9mo ago
can u show the form where the tabs are
Pobiega
Pobiega9mo ago
Sorry to hijack but @leowest I need your help for an urgent thing, check your dms real quick
Stackz
StackzOP9mo ago
The code or what
leowest
leowest9mo ago
is it the serverinfo?
Stackz
StackzOP9mo ago
No That would be the main form
leowest
leowest9mo ago
akjshdjaksd so you keep it hidden after u take the input
Stackz
StackzOP9mo 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