- Home /
Having problems with Thread and ToString
Hi everyone!
Im working on a personal project that started as a console program, now i wanted to port the code to unity and it doesnt want to cooporate unfortunatly.
So a controller in unity shall update objects with data from a server
The class that communicates with the server looks something like this. Readwrite runs on a separate thread thats starts when the controller calls for the "server class"
private void readWrite()
{
/*Runs a for loop to gain access to data, sends commands via write, and then reads the network stream*/
}
public void write(string message)
{
/***Works****/
}
public string read()
{
byte[] data = new byte[1024];
StringBuilder recieved = new StringBuilder();
int numberOfBytesRead = 0;
// Incoming message may be larger than the buffer size, therefore reads all available data through this loop before stitching it togheter.
do
{
numberOfBytesRead = networkStream.Read(data, 0, data.Length);
recieved.AppendFormat("{0}", Encoding.ASCII.GetString(data, 0, numberOfBytesRead));
}
while (networkStream.DataAvailable);
return recieved.ToString(); // <---- This gives the exception "ToString can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene."
}
As you can see the the read does not work, and throws an exception talking about ToString must be on the main thread. I cant really figure out how to solve this so any help would be very appriciated. Thank you beforehand!
You miserably failed in presenting your problem. You included a lot of unrelated boiler plate code that does not contribute to your problem in any way. The actual code that might give you problems is not included in your description...
Furthermore you get some sort of exception but you haven't included the exact description of that exception and it's stacktrace. You really should update your question, remove unnecessary code and include the relevant parts. It's enough to state that a certain method runs on a seperate thread
// runs on a seperate thread
private void ReadWrite()
{
// your offending / problematic code here
Im sorry, kinda new, i have updated the post now :)
I can't reproduce this problem. If i use a System.Text.StringBuilder
inside a seperate thread i can call ToString without any problems or errors. Are you 100% sure that you actually use the StringBuilder class from the System.Text namespace? Also are you 100% sure that the exception is actually thrown on the line you've mentioned? Have you checked the stacktrace and line numbers given in the exception?
hmm ill try, but that will require alot of rewriting
Your answer
![](https://koobas.hobune.stream/wayback/20220612161158im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Job System without using the main thread 1 Answer
[C#] How to use a Multi Threaded Job Queue for Math function 2 Answers
Enum to int issue 1 Answer