Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active December 16, 2024 03:10
Show Gist options
  • Select an option

  • Save unitycoder/8721e5bd6377ac2ffbba4f9b0b2766f0 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/8721e5bd6377ac2ffbba4f9b0b2766f0 to your computer and use it in GitHub Desktop.

Revisions

  1. unitycoder revised this gist Aug 30, 2023. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions multithreading.cs
    Original file line number Diff line number Diff line change
    @@ -21,3 +21,8 @@ void YourMethod(System.Object a)
    };
    Thread _thread = new Thread(starter) { IsBackground = true };
    _thread.Start();


    // call method in mainthread
    var mainThreadContext = System.Threading.SynchronizationContext.Current;
    mainThreadContext.Post(_ => YourFunction(), null);
  2. unitycoder revised this gist Jan 28, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion multithreading.cs
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    ParameterizedThreadStart start = new ParameterizedThreadStart(YourMethod);
    myThread = new Thread(start);
    myThread.IsBackground = true;
    myfileStreamerThread.Start(yourParams);
    myThread.Start(yourParams);
    void YourMethod(System.Object a)
    {
    }
  3. unitycoder revised this gist Jan 28, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions multithreading.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    // send parameter to thread method
    Thread myThread;

    ParameterizedThreadStart start = new ParameterizedThreadStart(YourMethod);
    myThread = new Thread(start);
    myThread.IsBackground = true;
  4. unitycoder revised this gist Nov 12, 2020. 2 changed files with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions multithreading.cs
    Original file line number Diff line number Diff line change
    @@ -11,3 +11,11 @@ void YourMethod(System.Object a)
    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();
    File renamed without changes.
  5. unitycoder revised this gist Nov 15, 2019. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions multithreading.cs
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    // send parameter to thread method
    ParameterizedThreadStart start = new ParameterizedThreadStart(YourMethod);
    fileStreamerThread = new Thread(start);
    fileStreamerThread.IsBackground = true;
    fileStreamerThread.Start(yourParams);



    myThread = new Thread(start);
    myThread.IsBackground = true;
    myfileStreamerThread.Start(yourParams);
    void YourMethod(System.Object a)
    {
    }

    // regular thread
    Thread thread = new Thread(new ThreadStart(YourWorkMethod));
    thread.Start();

    }
  6. unitycoder revised this gist Apr 8, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,8 @@ 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/
    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
  7. unitycoder revised this gist Feb 21, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@ setting priority, setting core
    https://stackoverflow.com/a/16917271

    for unity
    https://80.lv/articles/simple-multithreading-for-unity/
    https://80.lv/articles/simple-multithreading-for-unity/
    http://www.stevevermeulen.com/index.php/2017/09/using-async-await-in-unity3d-2017/
  8. unitycoder revised this gist Feb 21, 2019. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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/
  9. unitycoder created this gist Nov 24, 2018.
    11 changes: 11 additions & 0 deletions multithreading.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    ParameterizedThreadStart start = new ParameterizedThreadStart(YourMethod);
    fileStreamerThread = new Thread(start);
    fileStreamerThread.IsBackground = true;
    fileStreamerThread.Start(yourParams);



    void YourMethod(System.Object a)
    {

    }