代码之家  ›  专栏  ›  技术社区  ›  jcolebrand

PowerShell中的GetSystemTimeAdjustment

  •  0
  • jcolebrand  · 技术社区  · 2 年前

    我遇到的一个问题是如何检查PowerShell中的GetSystemTimeAdjustment值,这样我就可以确定等待线程在某些后台自动化中完成的最短时间,然后再检查是否应该将它们收集为PowerShell 5中的作业(由于业务原因和支持的连续性,我还不能使用7)

    我们如何找到所使用的Windows版本的默认最小线程切片时间?

    1 回复  |  直到 2 年前
        1
  •  0
  •   jcolebrand    2 年前
    Add-Type -Assembly "System.ServiceModel.Internals"
    $assembly = [System.AppDomain]::CurrentDomain.GetAssemblies().Where({ $_.GetName().Name -match 'System.ServiceModel.Internals' })
    $timer = $assembly.GetTypes().Where({ $_.Name -eq 'IOThreadTimer' })
    $timerValue = $timer.GetProperty("SystemTimeResolutionTicks").GetValue($null)
    

    注意,这是您在交互式shell中获得的值,因此不能保证每个进程都获得相同的时间片,但它是通过

    // System.Runtime.Interop.UnsafeNativeMethods
    public static extern uint GetSystemTimeAdjustment(out int adjustment, out uint increment, out uint adjustmentDisabled);
    

    此外,您的实际命令可能需要不到15毫秒的时间才能返回,这是为了确定线程调度程序在尝试切换任务之前等待的系统时间增量。