Dispose in Unity

I had some codes that were written in C#:

BackgroundWorker bw = new BackgroundWorker();
....
bw.RunWorkerCompleted += new RunWorkerCompletedEeventHandler(bw_RunWorkerCompleted);
....

void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
   ...
   ((BackgroundWorker)sender).Dispose();
}

But I got the error:

Type 'System.ComponentModel.BackgroundWorker' does not contain a definition for 'Dispose' and no extension method 'Dispose' of type 'System.ComponentModel.BackgroundWorker' could be found

I had included these namespaces:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Text;
using System.Net;
using System.Net.Socket;
using System.Threading;
using System.IO;

Thkans for help.

1 Answer

1

It’s because you are using the .NET 2.0 Subset in Player Settings/Optimization.

You might be interested in the Unity Gems threading article which has a neat piece of code at the end to allow you to write threads easily in Unity.

Thanks for the tips, it works if I use .Net 2.0 instead