Search Unity

Question about polling an url

Discussion in 'Scripting' started by grka, Apr 24, 2015.

  1. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    I have some understanding problem with an error message and hope someone can help me here.

    I try to implement a paypal payment. After the user has done his payment it can take a moment till my ipn script is called from paypal to finish the transaction.

    After the user called my payment website I want to poll my php file to get to know if the paypal transaction is available in my database.

    I thought this would be easy done this way:
    Code (CSharp):
    1.  
    2. private void OpenWaitDialog ()
    3.    {
    4.         aTimer = new System.Timers.Timer(30000);
    5.         aTimer.AutoReset = true;
    6.         aTimer.Elapsed += new System.Timers.ElapsedEventHandler(WaitTimeout);
    7.         aTimer.Start();
    8.         m_bWaiting = true;
    9.         StartCoroutine(CheckPayment(this));
    10. }
    11.  
    12. private void WaitTimeout(object source, ElapsedEventArgs e)
    13.    {
    14.       if (m_bWaiting  == false)
    15.       {
    16.         aTimer.Stop();
    17.         return;
    18.       }
    19.  
    20.       StartCoroutine(CheckPayment(this));
    21.     }
    22.  
    23. private IEnumerator CheckPayment (CMyClass mainThread)
    24.    {
    25.      string strUrl;
    26.      WWW www;
    27.  
    28.      strUrl = "www.myurl.de";
    29.      www = new WWW(strUrl);
    30.          ...
    31.      if(PaymentArrived)
    32.        mainThread.m_bWaiting = false;
    33.         ...
    34. }
    35.  

    So far so well but when I play my game I get the error message "startcoroutine_auto can only be called from main thread". Can someone please tell me what I do wrong?

    so far I thought my first 2 methodes are working in the main thread and only the checkpayment methode was in another thread
     
    Last edited: Apr 24, 2015
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    WaitTimeOut is being called by your timer, that runs on another thread.

    To use this on the main thread have WaitTimeOut set a bool. Then check that bool in a coroutine started from the main thread.

    But to be honest you are better off simply running the timer in the coroutine with WaitFofSeconds.