Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zhuchun · Aug 14, 2013 at 01:51 PM · coroutinewwwtimeryield

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Peter G · Aug 14, 2013 at 02:09 PM

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;
 }
              

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zhuchun · Aug 14, 2013 at 02:45 PM 0
Share

oh I see, much elegant than i did! It's helpful, thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WWW isDone when lost internet connection, it return true and not wait for internet connection (Andriod) 1 Answer

NullReferenceException in nested Coroutine 0 Answers

How to yiled a try/catch block? 2 Answers

Yield Problems 2 Answers

yield return WWW stops Coroutine? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges