ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

SpinThread

Spins up and executes the action within a thread. Basically fire and forget. Real big question here. Does anybody see any issues with thread management? I would like to update this with any code necessary to manage thread cleanup if necessary. I realize that this has the ability to create unsafe thread referencing if not written such that the contents of the action are exclusive to the scope of the action, but that is outside the purview of this extension

Source

public static void SpinThread<T>(this T parms, Action<T> action)
{
    System.Threading.Thread t =
           new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(p => action((T)p)));
    t.IsBackground = true;
    t.Start(parms);
}

Example

var parms = new {
    id = 1,
    text = "asd"
};

parms.SpinThread(action => {

// perform some logic with action.id and action.text

});

Author: Daniel Gidman

Submitted on: 12 jan. 2011

Language: C#

Type: System.Object

Views: 5496