Search Unity

Process.GetCurrentProcess().Threads.Count returns zero all the time!

Discussion in 'Scripting' started by BrightBit, Jan 19, 2015.

  1. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    The following method always prints "Process Thread Count = 0" to the console:

    Code (csharp):
    1. void Start()
    2. {
    3.     System.Diagnostics.Process process  =
    4.         System.Diagnostics.Process.GetCurrentProcess();
    5.  
    6.     process.Refresh();
    7.  
    8.     UnityEngine.Debug.Log("Process Thread Count = " + process.Threads.Count);
    9. }
    That's weird even if I wouldn't start separate threads but I do. Is this a bug? Or am I doing something wrong? I also asked this question on stackoverflow but I didn't get an answer yet: Current process without threads?

    I am using Unity 4.5.1f3 (Mono 2.0.50727.1433)
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    Can someone at least try this piece of code and tell me if it works for him/her?
     
  3. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    (The one time I did research into Unity and threads, I learned that Unity cannot do multi-threading... I hope that answers part of your question?)
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    Well, if you mean that you can't use Unity's API from threads other than the main thread, you're right. However, you can use System.Threading.Threads or System.Threading.ThreadPool if you don't make them call Unity methods!

    Here you can see the workload for my CPUs caused by a Unity application that creates 1000 threads that are running some heavy duty calculations (just a loop with several sqrt calls, i.e. no Unity methods):



    So, it is possible! The question is why does process.Threads.Count return zero?
     
  5. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    It returns 0 because you didn't create any thread ?

    I don't see any bugs here.
     
  6. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    Even if I do not create threads on my own, as far as I know there should always be at least one thread already there: the main thread!

    Apart from that: I did create threads! The post above your answer mentions 1000 threads but nevertheless process.Threads.Count prints zero!
     
  7. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Hmm.

    Okay, I was thinking that the method would count the number of thread created by you. But I guess it doesn't.

    So I don't know, but interesting question. Let see if someone has the answer! :p
     
  8. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    I've created a Bug Report about this issue, now. I will keep you posted.
     
    wu_0614 likes this.
  9. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    A small addition: Now I've tested this issue with the latest Unity version, too. The problem still exists. Should I create another bug report or is there a way to update my last report?
     
    Last edited: Jan 21, 2015
  10. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    Okay, it's a bug! I've created a bug report some days ago and today I got the following answer:

    "[...] We have fixed this and [the] problem will not appear in the next major version. [...]"

    Now I'm curious what the next major version will be.
     
    BAIZOR and s_guy like this.
  11. s_guy

    s_guy

    Joined:
    Feb 27, 2013
    Posts:
    102
    it still doesn't work in Unity 5.3
     
  12. Questioning

    Questioning

    Joined:
    May 17, 2015
    Posts:
    17
    Just adding to this; I also have this issue.
     
  13. timr

    timr

    Joined:
    Aug 5, 2012
    Posts:
    3
    +1 to this issue
     
  14. bteitler

    bteitler

    Joined:
    May 11, 2014
    Posts:
    52
    Same issue.
     
  15. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    I've created another bug report. I will keep you posted.
     
  16. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    I've got a first answer:
    My next question was, if it will be implemented in the next version of Unity. I will keep you posted again.
     
  17. spvn

    spvn

    Joined:
    Dec 10, 2013
    Posts:
    80
    Has this bug really not been solved after 2 years? Is it an issue with Mono or something? Is there no way to access process.Threads at all in unity?
     
  18. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    Bug still applies to the version 2017.1.0.9b
     
  19. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Lol
     
  20. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    112
    Any news on the bug report?
    Can you link to it here?

    ThreadPool.GetAvailableThreads is also not supported. :/

    Guess this explains why people are rolling their own thread pools.
     
  21. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    I still got no answer to that last question... :(
     
  22. FeynmanRen100

    FeynmanRen100

    Joined:
    Sep 6, 2013
    Posts:
    22
    Still happen in v2018.2.f1
     
  23. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    265
    Maybe you should file a bug report, too.
     
  24. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    You guys are persistent :)
     
  25. scarofsky

    scarofsky

    Joined:
    Sep 29, 2016
    Posts:
    11
    Persistence & curiosity bring us here, finding the issue still not solved...
     
  26. hellaeon

    hellaeon

    Joined:
    Jul 20, 2010
    Posts:
    90
    I would love an answer to this. I want to ensure my threads finish when I expect them to! I'm unsure if there is another non code way to check this?
     
  27. webtrainingroom

    webtrainingroom

    Joined:
    Feb 21, 2020
    Posts:
    1
    I am getting 13 count with above code
    int threadCount = Process.GetCurrentProcess().Threads.Count;
    Console.WriteLine($"threadCount-{threadCount}");

    But when i try to get the count of current thread pool, i get the right number, for example,

    // call (1)
    ThreadPool.QueueUserWorkItem(a => Method1(4, "my param 1"));
    // call (2)
    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate (object state)
    { Method1(6, "my param 10"); }), null);

    int max, max2;
    ThreadPool.GetMaxThreads(out max, out max2);

    int available, available2;
    ThreadPool.GetAvailableThreads(out available, out available2);

    int runningThread = max - available;

    Console.WriteLine($"running therads count: {runningThread}");

    I am getting right count 2
     
  28. z3nth10n

    z3nth10n

    Joined:
    Nov 23, 2013
    Posts:
    55
    They updated the Mono version but I'm having this issue too.
     
  29. JaredThomson

    JaredThomson

    Joined:
    Nov 15, 2013
    Posts:
    16
    > Has this bug really not been solved after 2 years?

    *laughs in 2021*
     
  30. BAIZOR

    BAIZOR

    Joined:
    Jul 4, 2013
    Posts:
    112
    I guess the bug is even deeper. Because the whole number of properties is incorrect when you getting Processes (all).
    Almost all data from the processes are lost. AND even that pieces of data may not be got properly from the first try, AND sometimes even from the first build start (I'm talking just about Windows platform in Mono compilation)

    System.Diagnostics.Process.GetProcesses()
     
  31. KnifeOffWork

    KnifeOffWork

    Joined:
    Jun 4, 2016
    Posts:
    4
    Still in 2021, lol , it will be 10 year bug i think
     
  32. ReGaSLZR

    ReGaSLZR

    Joined:
    Jun 11, 2015
    Posts:
    13
    Yep. Using Unity 2019.4.17.f1 and
    Code (CSharp):
    1. Process.GetCurrentProcess().Threads.Count == 0
    always. Even if you create your own thread and try to re-log that thread count, it's still zero.