How to set timer for WWW helper?

Hi

I’m trying to check status from WWW termly, then I recall a timer can do that in c# programming.

Void Start(){
                MyTimer = new System.Timers.Timer ();
    			MyTimer.Elapsed += _timer_Elapsed;
    			MyTimer.Interval = 3000;
    			MyTimer.Enabled = true;	
}

Definitely, WWW call may take time and we dont want the UI freeze.

So this event might use StartCoroutine

IEnumerator _timer_Elapsed (object sender, System.Timers.ElapsedEventArgs e)
	{
		MyTimer.Enabled = false;
        yield return StartCoroutine(WWWHelper.CheckReady());
        MyTimer.Enabled = true;
    }

But the editor said Timer.Elapsed can’t return such a type.

It looks like system.timers.timer is not what unity would take.

How to approach this task? How to set a timer and call func without a not responding UI?

Thank you :slight_smile:

The problem is with the implied delegate ElapsedEventHandler. Your function _timer_Elapsed doesn’t fit the signature because it returns an IEnumerator.

Now you have two choices how to fix it. You can switch entirely to cotoutines/yields, or you can keep some elements of your timer intact. Personally I’d switch to coroutines simply because you’ll be more consistent, and its generally how people do delayed execution in Unity. Also, coroutines do not freeze control like you are worried about. They run in the main thread, but they just check whatever condition they’re waiting for then pass control on to the next object so nothing freezes.

So here’s how I’d do it.

void Start(){
       StartCoroutine(_timer_Elapsed);
       //Start the timer.
}

bool isDone = false;

IEnumerator _timer_Elapsed () {
       isDone = false;
        while ( !isDone ) {
        //Are we done yet?
             yield return new WaitForSeconds(3);
             //wait 3 seconds
             yield return StartCoroutine(WWWHelper.CheckReady() );
             //check whatever this is
        }
}

//Have someone tell us that we've got the info that we need.
public void Finish () {
     isDone = true;
}