最近在做和buildbot(一个持续集成工具)相关的程序 ,buildbot有一个特性,就是控制台程序全部不会谈出cmd,只是在后台运行,比如我开一个线程在后台跑,读取串口的数据,然后进行相关的处理,我如果想关闭这个串口,结束这项操作怎么告诉后台的程序呢?如果是正常情况,从键盘输入特定的字符串,或者ctrl+c就可以停止了,但是由于buildbot的自身特性,如果要实现这个功能,可能需要更改许多它本身的结构,这样不合理,而且难度较大,浪费时间。
通过研究我发现,虽然buildbot不能弹出控制台,却能弹出窗口(dialog),那么可不可以利用这点来对后台程序进行控制呢?
于是我写了一个messagebox的程序,如下
//
DownLoadTool.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
"
DownLoadTool.h
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
#ifdef _DEBUG
#define
new DEBUG_NEW
#undef
THIS_FILE
static
char
THIS_FILE[]
=
__FILE__;
#endif
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/**/
/////
//
The one and only application object
CWinApp theApp;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
using
namespace
std;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
_tmain(
int
argc, TCHAR
*
argv[], TCHAR
*
envp[])
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int nRetCode = 0;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// TODO: code your application's behavior here.
switch(argc)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case 2:
if (strcmp(argv[1],"disable")==0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
nRetCode = 1;
break;
}
if (AfxMessageBox(argv[1],MB_YESNO, MB_ICONINFORMATION )==IDNO)
nRetCode = 1;
break;
case 3:
if (AfxMessageBox(argv[1],MB_YESNO, MB_ICONINFORMATION )==IDNO)
nRetCode = 1;
else
nRetCode = system(argv[2]);
break;
case 4:
if (atoi(argv[3])==1)
nRetCode = 1;
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (AfxMessageBox(argv[1],MB_YESNO, MB_ICONINFORMATION )==IDYES)
nRetCode = system(argv[2]);
}
break;
default:
cout<<"MessageBox [description] [program] [disabledialog(=1,disable)]"<<endl;
nRetCode = 1;
break;
}
}
return nRetCode;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
这个程序负责弹出一个messagebox,由参数传入messagebox的提示信息及需要启动的程序,如果启动了其他程序,则返回其他程序的返回值,如果没有启动其他程序,则返回yes(0)no(1)
在python程序和批处理(bat)中可以分别利用os.system()函数和errorlevel来判断该程序的执行情况,这样,就可以控制后台运行的程序了