如何让程序暂停运行

如何让程序暂停运行,第1张

1)ctrl + alt + delt 进程里选程序然后关闭

2)不然 开始-〉运行 打吵渣戚入msconfig 在服务和启动里选不要的程序,

3)再不然就要改注册表了。。。这个觉得有点困难,可以写一个小的shell来做。

4)最后有一个升陵小软件叫做process explorer 是微软的一个小程序,下载以后何以察看现梁手在系统运行中的程序,然后选定一个关掉也很放便,这个程序也只不过1mb左右,对查木马也有一点用。

线程中,如果要暂停一定的时间,可以使用Thread的Sleep方法,让线程暂停。在微软新的win8API中,已经用Task来实现原来用线程实现的许多功能,同样,Task也有Delay方法,让程序暂停一定的时间。

以下程序利用System.Threading.Timer让程序每隔间隔的时间执行回调方法:

using System

using System.Collections.Generic

using System.Text

using System.Threading

namespace TimerApp{

class Program{

static void Main(string[] args){

Console.WriteLine("***** 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.

Timeout.Infinite)  //一秒钟调用一次 Interval of time between calls (in milliseconds).

Console.WriteLine("Hit key to terminate...")

Console.ReadLine()}

static void PrintTime(object state){

Console.WriteLine("Time is: {0}, Param is: {1}",

DateTime.Now.ToLongTimeString(), state.ToString())}

如果把时间间隔1000改成 Timeout.Infinite,这样回调方法PrintTime只会在1秒之后调用一次。

除了Threading下有个Timer类之外,.net中还有另一个Timer,就是System.Timer名称空间下的Timer。此Timer的用法和Threading下的Timer不太相同。

System.Timers.Timer t2 = new System.Timers.Timer(100)

t2.Elapsed += t2_Elapsed

//t1.AutoReset = false

t2.Enabled = true}}

void t2_Elapsed(object sender, System.Timers.ElapsedEventArgs e){}  

其中AutoReset属性的含升清氏义是是否在每次时间间隔到了都触发Elapsed事件还是只触发一次(t1.AutoReset = false)。

解决此类问题的一个小例子(实现在窗体上相隔指定时间显示字幕)

//创建StopNtime类

using System

using System.Collections.Generic

using System.Linq

using System.Text

using System.Threading

namespace 程序运行正顷暂停器{

class StopNtime{

public StopNtime(){

System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false//,跨线程调用控件必加上去         }

private int stopTime = 0//暂停的时间

ThreadStart myStart

Thread TheStop

public  readonly  object MyLockWord = new object()

public   void stopWay(int stopTime){

this.stopTime = stopTime

myStart = new ThreadStart(this .ToStop )

TheStop = new Thread(myStart )

TheStop.Start()

private   void ToStop(){

lock (MyLockWord){

Thread.Sleep(this .stopTime )

Thread.CurrentThread.Abort()

//主窗体代码

using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Linq

using System.Text

using System.Windows.Forms

using System.Threading

using System.Collections

namespace 程序运行暂停器{

public partial class Form1 : Form{

public Form1(){

InitializeComponent()}  

private void Form1_Load(object sender, EventArgs e){

label1.Text = ""

Thread f = new Thread(f1 )

f.Start()}  

string MyWord = "问题:1+1=? ...\n答案是:2 ..."

private void f1(){

StopNtime mmm = new StopNtime()

foreach (char m in MyWord){

lock (mmm.MyLockWord ){

label1.Text += m.ToString()                   

mmm.stopWay(300)         

//运行结果

如果是打算在竖明程序运行的某一时刻,暂停运早或行,那要用一个按键,第一次按下,进入一个循环中,在循环中只检测这个按键,不再执行其它程序。当第二次按键,跳出这余睁告个循环,返回主循环程,继续运行。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12542154.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存