I need a slow C# function

前端 未结 6 2047
耶瑟儿~
耶瑟儿~ 2020-12-05 09:47

For some testing I\'m doing I need a C# function that takes around 10 seconds to execute. It will be called from an ASPX page, but I need the function to eat up CPU time on

6条回答
  •  天命终不由人
    2020-12-05 10:26

    You can use a 'while' loop to make the CPU busy.

        void CpuIntensive()
        {
            var startDt = DateTime.Now;
    
            while (true)
            {
                if ((DateTime.Now - startDt).TotalSeconds >= 10)
                    break;
            }
        }
    

    This method will stay in the while loop for 10 seconds. Also, if you run this method in multiple threads, you can make all CPU cores busy.

提交回复
热议问题