Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active August 30, 2023 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unitycoder/8721e5bd6377ac2ffbba4f9b0b2766f0 to your computer and use it in GitHub Desktop.
Save unitycoder/8721e5bd6377ac2ffbba4f9b0b2766f0 to your computer and use it in GitHub Desktop.
MultiThreading Start Thread with Parameters
// send parameter to thread method
Thread myThread;
ParameterizedThreadStart start = new ParameterizedThreadStart(YourMethod);
myThread = new Thread(start);
myThread.IsBackground = true;
myThread.Start(yourParams);
void YourMethod(System.Object a)
{
}
// regular thread
Thread thread = new Thread(new ThreadStart(YourWorkMethod));
thread.Start();
// wait for thread to finish
ThreadStart starter = YourMethod;
starter += () =>
{
Console.WriteLine("Done!");
};
Thread _thread = new Thread(starter) { IsBackground = true };
_thread.Start();
// call method in mainthread
var mainThreadContext = System.Threading.SynchronizationContext.Current;
mainThreadContext.Post(_ => YourFunction(), null);
avoiding context switching
https://social.msdn.microsoft.com/Forums/vstudio/en-US/ab44ec59-d695-41b5-8e47-1ddd204345ca/prevent-from-switching-to-another-thread?forum=csharpgeneral
https://blogs.msdn.microsoft.com/andrewarnottms/2012/12/28/the-cost-of-context-switches/
https://docs.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices
setting priority, setting core
https://stackoverflow.com/a/16917271
for unity
https://80.lv/articles/simple-multithreading-for-unity/
http://www.stevevermeulen.com/index.php/2017/09/using-async-await-in-unity3d-2017/
threading visualizer tool
https://docs.microsoft.com/en-us/visualstudio/profiling/concurrency-visualizer?view=vs-2019
common bad patterns https://docs.microsoft.com/en-us/visualstudio/profiling/common-patterns-for-poorly-behaved-multithreaded-applications?view=vs-2019
@unitycoder
Copy link
Author

@unitycoder
Copy link
Author

@unitycoder
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment