If you need to execute some long running task on the background in Unity, you may find this tool I just wrote, useful. This is BackgroundWorker for Unity 3D
BackGround Worker for Unity3D
You will only need the class BackgroundWorker
It worth noting that a lot of seemingly background tasks can be implemented with Unity Coroutines. For example using WWW service to send or receive data from a remote server. Or any case where we just wait for a response from a remote computer.
However because coroutines run on the main thread if you really have a long running, not just waiting for response but computing intensely something, you will need to run it on a background thread. Here is how you use this tool:
if (_backgroundWorker != null) _backgroundWorker.CancelAsync();
_backgroundWorker = new BackgroundWorker();
_backgroundWorker.DoWork += (o, a) =>
// executed on background thread
// do slow running computationaly intense work, check periodically
// if (a.IsCanceled) return;
// assign result as
// a.Result = a.Argument+"!";
_backgroundWorker.RunWorkerCompleted += (o, a) =>
// executed on main thread
// you can use a.Result
And then on each frame execute:
if (_backgroundWorker != null) _backgroundWorker.Update();
The tool has been tested on Windows, Mac, iPhone, and Android.
This is a sample usage of the BackgroundWorker class.