I have a C# class library (.Net 3.5) compiled as dll and imported to Unity3D. In certain method I must wait on main thread before continuing main process. So I use a ManualResetEvent object to wait a specified duration before timeout. But in Unity3D it does not works. It is not waiting the defined time. When trying to work with ManualResetEventSlim same is happening, too.
Is there anyone knowing an alternative method to blocks the main thread? I already tried Thread.Sleep() and while-loop with DateTime, but in both cases Unity crashes already before I started the play mode.
EDIT:
Posting some code is difficult, I will try to explain. I want to build a network connection between a WPF application and Unity3D. When the client sends a request to server, for example to receive a list of objects, it is necessary to wait before saying goodbye. This is only an excerpt. The messenger object sends the message. Then the ManualResetEvent will be called (it is in property WaitEvent). After timeout I look which state has my waiting message. Per wait mode an exception will be thrown or the response is returned.
try
{
this.InternalMessenger.SendMessage(message);
waitingMessage.WaitEvent.WaitOne(timeout);
switch (waitingMessage.CurrentState)
{
case WaitMode.Cancelled:
throw new Exception("Disconnected before response received.");
case WaitMode.Waiting:
throw new Exception("Timeout occured. Can not received response.");
}
return waitingMessage.Response;
}
Without Unity3D it works without any problem, so it should. Until now I thought, Unity would working with .Net 3.5. But when I had to use .Net 2.0 I have to change something more.. does you know where I can get a System.Threading.dll for .Net 2.0?
Thank you for help.
Possible Solution:
Instead of calling WaitOne I call a delegate. This will be set outside of the library, I used a property for this.
In the dll:
public delegate void Wait(int timeout);
public class A
{
private int timeout = 60000;
public static Wait Wait { get; set; }
public void Request(string request)
{
this.SendMessage(request);
this.Wait(this.timeout);
// ....
}
}
In Unity3D:
public void Start()
{
A.Wait = (timeout) => { System.Threading.Thread.Sleep(timeout); };
// ... continue working
}
I know, it is a dirty hack, and it does not work as the same as I planned. Additional I have to say: Unity3D was very unstable when I test this. But maybe, someday, somebody have a better idea or solution.