private void DoSomeThreadWork() { // Thread needs callback and sync context so it must be wrapped in a class. SendOrPostCallback callback = new SendOrPostCallback(ReceiveThreadData); SomeThreadTask task = new SomeThreadTask(_synchronizationContext, callback); Thread thread = new Thread(task.ExecuteThreadTask); thread.Start(); }
private void DoBackgroundWork() { // Create a ThreadTask object. SomeThreadTask threadTask = new SomeThreadTask(); // Create a task id. Quick and dirty here to keep it simple. // Read about threading and task identifiers to learn // various ways people commonly do this for production code. threadTask.TaskId = "MyTask" + DateTime.Now.Ticks.ToString(); // Set the thread up with a callback function pointer. threadTask.CompletedCallback = new SomeThreadTaskCompleted(SomeThreadTaskCompletedCallback); // Create a thread. We only need to specify the entry point function. // Framework creates the actual delegate for thread with this entry point. Thread thread = new Thread(threadTask.ExecuteThreadTask); // Do something with our thread and threadTask object instances just created // so we could cancel the thread etc. Can be as simple as stick 'em in a bag // or may need a complex manager, just depends. // GO! thread.Start(); // Go do something else. When task finishes we will get a callback. }