How to find current thread's max stack size in .net?

前端 未结 2 1790
暖寄归人
暖寄归人 2021-01-12 18:39

How can I find current thread\'s maximum stack size?

I am getting a stack overflow exception while executing a function from MMC UI but not from Powershell (command-

2条回答
  •  别那么骄傲
    2021-01-12 19:07

    From Windows 8, there is the GetCurrentThreadStackLimits() function. You can use it from C# via PInvoke like this:

    [DllImport("kernel32.dll")]
    static extern void GetCurrentThreadStackLimits(out uint lowLimit, out uint highLimit);
    
    uint low;
    uint high;
    
    GetCurrentThreadStackLimits(out low, out high);
    var size = (high - low) / 1024; // in KB
    

    On my machine, this yields 1MB in a console application, 256KB in a web application (IIS).

提交回复
热议问题