连续循环不循环 [英] Continual Loop Not Looping

查看:63
本文介绍了连续循环不循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

J的代码假定可以继续循环查找任何打开的记事本实例.但是:打开后它会检查一次,然后关闭控制台.如何保持控制台打开和检查/循环?

J have code that is suppose to continue looping looking for any instance of notepad that is open. However: it checks once when opened and then closes the console. How can I keep the console open and checking/looping?

#include "stdafx.h"

using namespace System;
using namespace System::Diagnostics;
using namespace System::ComponentModel;

int main(array<<system::string>>{
    for(;;){
	Process^ currentProcess = Process::GetCurrentProcess();
	array<process^>^localByName = Process::GetProcessesByName("notepad" );
	Console::WriteLine(localByName->Length);
	//Console::ReadKey();
	return 0;
    }
}

推荐答案

那是因为您在循环中有一个return语句,将return 0;移出了循环
Thats becaue you have a return statement in your loop move return 0; out side the loop


这种循环方式是错误的,因为它会通过全速运行一个紧密的循环来浪费电池(如果在笔记本电脑上运行),从而浪费了CPU周期.永远做循环几乎没有任何意义,只不过浪费了CPU周期.

至少要增加一个延迟,以便您每秒检查一次几次(也许3或4次).它将几乎不使用CPU.
This way of looping is wrong as it waste CPU cycle by running a tight loop at full speed draining batteries (if run on a laptop). It does not make sense to do loop forever doing almost nothing but wasting CPU cycles.

At least add a delay so that you would check that only a few time per second (maybe 3 or 4 times). It will the uses almost no CPU.


This looks like it should help you as there are various ways of looping, you may have to use a certain function in order to stop the command from crashing, perhaps?


这篇关于连续循环不循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆