Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Calling a class locks the player until done.

Discussion in 'Scripting' started by shaun, May 9, 2007.

  1. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Im probably doing something thats obviously wrong, but I dont know which class to make the changes in.

    I call a coroutine:
    ReadXMLClass.cs
    Code (csharp):
    1. void Awake () {
    2.     StartCoroutine("fncGetXMLData");
    3. }
    the IEnum function reads in some XML and then inits a download:
    ReadXMLClass.cs
    Code (csharp):
    1.     IEnumerator fncGetXMLData(){
    2. //Init XML read
    3.    
    4.     //Read XML Code...
    5.  
    6.     //Begin File Download
    7.         FTPManager ftplib = new FTPManager();
    8.         ftplib.Connect("ftp.ftp.com",21,"username","password");
    9.  
    10.  
    11.  
    12.         while(ftplib.DoDownload() > 0)
    13.  
    14.         {
    15.  
    16.             int perc = (int)(((ftplib.BytesTotal) * 100) / ftplib.FileSize);
    17.  
    18.             string strProgress;
    19.             strProgress = string.Format("\rDownloading: {0}/{1} {2}%", ftplib.BytesTotal, ftplib.FileSize, perc);
    20.             Debug.Log(strProgress);
    21.  
    22.         }
    23.     }
    I have another large script which is an FTP manager that i use to connect and download stuff into Unity - problem is, whenever i connect or download, the engine is frozen until the request completes:
    FTPManager.cs
    Code (csharp):
    1.  
    2. //Connect function example...
    3.         public int Connect()
    4.  
    5.         {
    6.  
    7.             if (server == null)
    8.  
    9.                 throw new Exception("No server has been set.");
    10.  
    11.             if (user == null)
    12.  
    13.                 throw new Exception("No username has been set.");
    14.  
    15.  
    16.  
    17.             if (main_sock != null)
    18.  
    19.                 if (main_sock.Connected)
    20.  
    21.                     return -1;
    22.  
    23.  
    24.  
    25.             main_sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    26.  
    27.             main_ipEndPoint = new IPEndPoint(Dns.GetHostByName(server).AddressList[0], port);
    28.  
    29.            
    30.  
    31.             try
    32.  
    33.             {
    34.  
    35.                 main_sock.Connect(main_ipEndPoint);
    36.  
    37.             }
    38.  
    39.             catch(Exception ex)
    40.  
    41.             {
    42.  
    43.                 throw new Exception(ex.Message);
    44.  
    45.             }
    46.  
    47.  
    48.  
    49.             ReadResponse();
    50.  
    51.             if (response != 220)
    52.  
    53.                 Fail();
    54.  
    55.  
    56.  
    57.             SendCommand("USER " + user);
    58.  
    59.             ReadResponse();
    60.  
    61.  
    62.  
    63.             switch(response)
    64.  
    65.             {
    66.  
    67.                 case 331:
    68.  
    69.                     if (pass == null)
    70.  
    71.                     {
    72.  
    73.                         Disconnect();
    74.  
    75.                         throw new Exception("No password has been set.");
    76.  
    77.                     }
    78.  
    79.                     SendCommand("PASS " + pass);
    80.  
    81.                     ReadResponse();
    82.  
    83.                     if (response != 230)
    84.  
    85.                         Fail();
    86.  
    87.                     break;
    88.  
    89.                 case 230:
    90.  
    91.                     break;
    92.  
    93.             }
    94.  
    95.            
    96.  
    97.             return  1;
    98.  
    99.         }
    I initially thought yield could be an answer, but I dont have control over .NET FTP function after I call it. I thought invoking a new thread could work, but I'm not 100% sure about how that works with Unity - which is why im posting ;)

    Also - Reading the XML works fine (no lock while requesting data), as I'm using the built-in WWW in Unity- but Im assuming other .Net components are not aware of of Unity and thus lock until complete.

    Cheers
    Shaun
     
  2. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    bump! - really need some help with this ;)

    Im guessing its a Threading issue?

    Thanks!

    Shaun
     
  3. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    Coroutines aren't thread-based, so that's probably the reason for the hang.

    I'm not sure of a way to spawn a background process like this for any .NET function call that will wait before returning. OTEE or someone more experienced can hopefully help...
     
  4. Samosito

    Samosito

    Joined:
    Jul 10, 2009
    Posts:
    7
    Hi

    This thread started long time ago, but I'd like to know if anyone could find a solution?
    I'm having exactly the same problem: when I use ftplib to get some files from a server, player is blocked until it finishes.
    I'm using ftplib because iPhone version of Unity only accepts .NET 1.1 so I can't use .NET functions for ftp.