Last active
August 30, 2023 10:44
-
-
Save unitycoder/8721e5bd6377ac2ffbba4f9b0b2766f0 to your computer and use it in GitHub Desktop.
MultiThreading Start Thread with Parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
lock free queue https://wiki.unity3d.com/index.php/Lock_Free_Queue
get process output async https://stackoverflow.com/a/29753402/5452781
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
async task https://gist.github.com/unitycoder/f0eeb2e93882fea553941ce5e11daf26