
你是要的是暂停其他进程吗?
如果是的话,blue/aiqbird的方法是可行的,但太麻烦且不稳定
最好且最简单的方法是调用系统API:ZwSuspendProcess 挂起进程
唤醒可以用:ZwResumeProcess函数
由于这两个函数是windows的非公开函数,所以要自己声明,具体方法见网上。 答案补充 函数有一个参数,即为你要暂停的进程的句柄hprocess,句柄可以用OpenProcess获得,
然后就 ZwSuspendProcess (hprocess) 就将进程挂起了,唤醒就 ZwResumeProcess(hprocess)
在线程中,如果要暂停一定的时间,可以使用Thread的Sleep方法,让线程暂停。在微软新的win8API中,已经用Task来实现原来用线程实现的许多功能,同样,Task也有Delay方法,让程序暂停一定的时间。
以下程序利用SystemThreadingTimer让程序每隔间隔的时间执行回调方法:
using System;
using SystemCollectionsGeneric;
using SystemText;
using SystemThreading;
namespace TimerApp{
class Program{
static void Main(string[] args){
ConsoleWriteLine(" Working with Timer type /n");
// Create the delegate for the Timer type
TimerCallback timeCB = new TimerCallback(PrintTime);
// Establish timer settings
Timer t = new Timer(
timeCB, // The TimerCallback delegate type
"Hello From Main", // Any info to pass into the called method (null for no info)
5000, // Amount of time to wait before starting
TimeoutInfinite); //一秒钟调用一次 Interval of time between calls (in milliseconds)
ConsoleWriteLine("Hit key to terminate");
ConsoleReadLine();}
static void PrintTime(object state){
ConsoleWriteLine("Time is: {0}, Param is: {1}",
DateTimeNowToLongTimeString(), stateToString());}
如果把时间间隔1000改成 TimeoutInfinite,这样回调方法PrintTime只会在1秒之后调用一次。
除了Threading下有个Timer类之外,net中还有另一个Timer,就是SystemTimer名称空间下的Timer。此Timer的用法和Threading下的Timer不太相同。
SystemTimersTimer t2 = new SystemTimersTimer(100);
t2Elapsed += t2_Elapsed;
//t1AutoReset = false;
t2Enabled = true;}}
void t2_Elapsed(object sender, SystemTimersElapsedEventArgs e){}
其中AutoReset属性的含义是是否在每次时间间隔到了都触发Elapsed事件还是只触发一次(t1AutoReset = false;)。
解决此类问题的一个小例子(实现在窗体上相隔指定时间显示字幕)
//创建StopNtime类
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemThreading;
namespace 程序运行暂停器{
class StopNtime{
public StopNtime(){
SystemWindowsFormsControlCheckForIllegalCrossThreadCalls = false;//,跨线程调用控件必加上去 }
private int stopTime = 0;//暂停的时间
ThreadStart myStart;
Thread TheStop;
public readonly object MyLockWord = new object();
public void stopWay(int stopTime){
thisstopTime = stopTime;
myStart = new ThreadStart(this ToStop );
TheStop = new Thread(myStart );
TheStopStart();
private void ToStop(){
lock (MyLockWord){
ThreadSleep(this stopTime );
ThreadCurrentThreadAbort();
//主窗体代码
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemLinq;
using SystemText;
using SystemWindowsForms;
using SystemThreading ;
using SystemCollections;
namespace 程序运行暂停器{
public partial class Form1 : Form{
public Form1(){
InitializeComponent();}
private void Form1_Load(object sender, EventArgs e){
label1Text = "";
Thread f = new Thread(f1 );
fStart();}
string MyWord = "问题:1+1= \n答案是:2 ";
private void f1(){
StopNtime mmm = new StopNtime();
foreach (char m in MyWord){
lock (mmmMyLockWord ){
label1Text += mToString();
mmmstopWay(300);
//运行结果
时间是不能暂停的,计算机时钟一直在走,关机后靠电池还在走。
如果你指让计算程序暂停,是可以的。
按某一个键使程序暂停, 用 _kbhit() , 检查是否 按了指定的暂停键:
#include <conioh>
#include <stdioh>
void main( void )
{
int p;
Lab:
while( !_kbhit() ){
_cputs( "Please Hit me!!\n " ); // 程序一直在执行,直到你按一个键才停
}
p = _getch(); // 判断是什么键
if (p == 's') printf( "\nStop\n"); // 如果是 s 键 停下来
else goto Lab; // 否则 回到头上
// 停下来 就到了这里,用类似方法, 添加 p = _getch(); 是否按了键,按的是否是 继续运行的键,。。。。
_getch();
}
========
遇到 system("pause") ;getch(); getchar() 等等 程序会立即停下来等待输入。
不能实现 程序在继续运行状态下 等待键的输入。只有 _kbhit 满足你的要求。
除非 用 C++ 作 键盘事件控制。
以上就是关于暂停一个进程的代码怎么写全部的内容,包括:暂停一个进程的代码怎么写、如何让C#程序在执行中暂停一段时间、c语言用哪些语句能实现时间暂停比方说按某一个键使时间暂停,再按一次使时间继续。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)