Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DateTimeOffset::现在在进程挂起并恢复后继续它停止的位置_C#_Powershell_Time - Fatal编程技术网

C# DateTimeOffset::现在在进程挂起并恢复后继续它停止的位置

C# DateTimeOffset::现在在进程挂起并恢复后继续它停止的位置,c#,powershell,time,C#,Powershell,Time,我有一个PowerShell脚本,返回特定PID的内存使用情况,例如输出: ... 1506277002989 (*nix TS) - Pid:11640 - 263.47 M 1506277003010 (*nix TS) - Pid:11640 - 263.47 M 1506277003033 (*nix TS) - Pid:11640 - 263.47 M ... // output continues 这是我用来暂停和恢复进程的代码: 使用系统; 使用系统诊断; 使用System.Run

我有一个PowerShell脚本,返回特定PID的内存使用情况,例如输出:

... 1506277002989 (*nix TS) - Pid:11640 - 263.47 M 1506277003010 (*nix TS) - Pid:11640 - 263.47 M 1506277003033 (*nix TS) - Pid:11640 - 263.47 M ... // output continues 这是我用来暂停和恢复进程的代码:

使用系统;
使用系统诊断;
使用System.Runtime.InteropServices;
使用System.Threading.Tasks;
命名空间进程SuspendAndCTD
{
班级计划
{
静态void Main(字符串[]参数)
{
intpid=Int32.Parse(args[0]);
暂停进程(pid);
Task.Delay(5000.Wait();
恢复过程(pid);
}
[旗帜]
私有枚举线程访问:int
{
终止=(0x0001),
SUSPEND_RESUME=(0x0002),
获取上下文=(0x0008),
设置上下文=(0x0010),
设置_信息=(0x0020),
查询信息=(0x0040),
设置线程令牌=(0x0080),
模拟=(0x0100),
直接模拟=(0x0200)
}
[DllImport(“kernel32.dll”)]
静态外部IntPtr OpenThread(ThreadAccess-dwDesiredAccess、bool-bInheritHandle、uint-dwThreadId);
[DllImport(“kernel32.dll”)]
静态外部单元SuspendThread(IntPtr-hThread);
[DllImport(“kernel32.dll”)]
静态外部intresumethread(IntPtr-hThread);
[DllImport(“内核32”,CharSet=CharSet.Auto,SetLastError=true)]
静态外部布尔闭合手柄(IntPtr手柄);
内部静态无效暂停处理(int pid)
{
var process=process.GetProcessById(pid);
if(process.ProcessName==string.Empty)
返回;
foreach(ProcessThread pT in process.Threads)
{
IntPtr pOpenThread=OpenThread(ThreadAccess.SUSPEND\u RESUME,false,(uint)pT.Id);
if(pOpenThread==IntPtr.Zero)
{
继续;
}
SuspendThread(pOpenThread);
闭合手柄(pOpenThread);
}
}
内部静态过程(int pid)
{
var process=process.GetProcessById(pid);
if(process.ProcessName==string.Empty)
返回;
foreach(ProcessThread pT in process.Threads)
{
IntPtr pOpenThread=OpenThread(ThreadAccess.SUSPEND\u RESUME,false,(uint)pT.Id);
if(pOpenThread==IntPtr.Zero)
{
继续;
}
var-suspendCount=0;
做
{
suspendCount=ResumeThread(pOpenThread);
}而(suspendCount>0);
闭合手柄(pOpenThread);
}
}
}
}

问题:如果我一次又一次地调用函数,为什么时钟会变得不同步?是因为它仅在初始化期间与系统时间同步吗?我应该如何以最一般的方式解决它?

请在问题中发布暂停/恢复代码,没有人想单击“外部阴影”links@MathiasR.Jessen这对你有帮助吗@timmyRS Approved悬架代码是问题的次要代码,因此它被添加为外部链接。至于隐藏性-可以通过删除查询部分并转到FQD验证站点(或使用服务)来轻松测试它。
DateTimeOffset.Now
只委托给
DateTime.Now
,它在Windows上(从P/Invoke调用判断)调用
GetSystemTimeAsFileTime
,因此,任何问题都应该直接归因于操作系统。请注意,通过挂起进程的所有线程来挂起/恢复进程通常是无法可靠工作的,无论您为什么这样做。请在问题中发布挂起/恢复代码,没有人希望单击“shady external”links@MathiasR.Jessen这对你有帮助吗@timmyRS Approved悬架代码是问题的次要代码,因此它被添加为外部链接。至于隐藏性-可以通过删除查询部分并转到FQD验证站点(或使用服务)来轻松测试它。
DateTimeOffset.Now
只委托给
DateTime.Now
,它在Windows上(从P/Invoke调用判断)调用
GetSystemTimeAsFileTime
,因此,任何问题都应该直接归因于操作系统。请注意,通过挂起进程的所有线程来挂起/恢复进程通常是不可靠的,无论您为什么这样做。