Search Unity

Meet new easy thread queue asset!

Discussion in 'Assets and Asset Store' started by Oksana-Iashchuk, Sep 26, 2012.

  1. Oksana-Iashchuk

    Oksana-Iashchuk

    Joined:
    Sep 10, 2012
    Posts:
    126
    Please meet new arrival ! http://u3d.as/content/orange-tree/thread-queue exactly

    If you need easy and fast way to make save or load of big files or any other heavy operations and you not guru in multi-threading - that for you!

    btw that is a part of http://u3d.as/content/orange-tree/sqlite-kit which actualy already has asynchronous sqlite database processing.

    There is copy/paste from example project.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.IO;
    6.  
    7. public class DemoObject : MonoBehaviour {
    8.    
    9.     ThreadQueue.TaskControl control = null;
    10.    
    11.     void OnGUI()
    12.     {
    13.         if( control == null )
    14.         {
    15.             if ( GUI.Button(new Rect (10,10,150,50), "Write File") )
    16.             {
    17.                 string filename = Application.persistentDataPath+"/threadqueue.test";
    18.                 control = ThreadQueue.QueueUserWorkItem(WorkCallback,CompeteCallback,filename);
    19.             }
    20.         }
    21.         else
    22.         {
    23.             GUI.Label (new Rect (10,70,600,600), "Progress:"+control.Progress);
    24.            
    25.             if ( GUI.Button(new Rect (10,10,150,50), "Cancel") )
    26.             {
    27.                 control.Cancel();
    28.                 control = null;
    29.             }
    30.         }
    31.     }
    32.    
    33.     // write file
    34.     public void WorkCallback (ThreadQueue.TaskControl control, object state)
    35.     {
    36.         string filename = state as string;
    37.        
    38.         using( FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)  )
    39.         {
    40.             for(int i = 0; i < 10000000; i++)
    41.             {
    42.                 fs.WriteByte((byte)'A');
    43.                
    44.                 // update progress
    45.                 control.Progress = (double)i;
    46.                
    47.                 // break if user puch cancel
    48.                 if(control.Canceled)
    49.                     break;
    50.             }
    51.         }
    52.     }
    53.    
    54.    
    55.     // task complete
    56.     public void CompeteCallback (object state)
    57.     {
    58.         control = null;
    59.     }
    60.  
    61.    
    62. }
     
    Last edited: Sep 26, 2012