- Home /
Multi threading problem !
public void SendVoice() {
StartCoroutine(call.postWWW(lip, lipSync, sentVoiceNumber - 1));
do
{
//Debug.Log(call.wwwPostRequest.isDone);
} while (!call.wwwPostRequest.isDone);//ablabal
if (call.wwwPostRequest.isDone)
{
string tempPath = Path.Combine(Application.persistentDataPath, "Audio");
tempPath = Path.Combine(tempPath, "returnAudio.wav");
i need to multithread this while loop because it is freezing my app , but i cannot because im calling postrequest that is creating a new object in it
postrequest :
public IEnumerator postWWW(LipSync lip, LipSyncData lipSync, int i) {
this.lip = lip;
this.lipSync = lipSync;
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/octet-stream");
string fileName = "sentRecord" + i + ".wav";
//string fileName = "carolyne1.wma";
byte[] byteData = File.ReadAllBytes("Assets/Audio/" + fileName); ;
wwwPostRequest = new WWW("http://192.168.31.5:8001/voice", byteData, headers);
yield return wwwPostRequest;
}
Well, you do start an infinite loop on the main thread just after you start the coroutine with call.postWWW()
in it. Either start a different coroutine that waits for the result of call.wwwPostRequest
(recommended way), or check it in an Update()
function (not so good).
Answer by hugodeheld · May 17, 2018 at 08:48 AM
You could look into the new job system in unity 2018.1, other than that unity doesn't nativly support multi-threading. You can try it but i cant recomend it as it just crashes unity allot and will be a pain.
But if you really insist on it, this could help:
https://gamedev.stackexchange.com/questions/113096/how-to-not-freeze-the-main-thread-in-unity
well either way my app is freezing everytime i run the while loop you got any other options ?
i have already checked it out its not about multithreading its about i cannot multithread while creating an object not in the main thread .. i need an easy way to use callback for this im totally messed up right now lol
Answer by Jerry-FFG · May 17, 2018 at 03:20 PM
Coroutines in unity do NOT run on a different thread !