- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 BDS 2006 开发了一个应用程序,它使用 MySQL 数据库(使用 DataModule 和 MyDAC 组件连接)。
现在我想在系统启动时启动我的应用程序(Windows XP)。所以我包括了 application shortcut in the start up folder .
现在启动,我的应用程序在 MySQL 服务启动之前启动。因此我得到一个错误无法连接到 MySQL。
所以我在我的应用程序启动时插入了一个空白,并执行检查以查看 MySQL 是否正在运行。如果未运行,则等待它运行。
function ServiceGetStatus(sMachine, sService: PChar): DWORD;
{******************************************}
{*** Parameters: ***}
{*** sService: specifies the name of the service to open
{*** sMachine: specifies the name of the target computer
{*** ***}
{*** Return Values: ***}
{*** -1 = Error opening service ***}
{*** 1 = SERVICE_STOPPED ***}
{*** 2 = SERVICE_START_PENDING ***}
{*** 3 = SERVICE_STOP_PENDING ***}
{*** 4 = SERVICE_RUNNING ***}
{*** 5 = SERVICE_CONTINUE_PENDING ***}
{*** 6 = SERVICE_PAUSE_PENDING ***}
{*** 7 = SERVICE_PAUSED ***}
{******************************************}
var
SCManHandle, SvcHandle: SC_Handle;
SS: TServiceStatus;
dwStat: DWORD;
begin
dwStat := 0;
// Open service manager handle.
SCManHandle := OpenSCManager(sMachine, nil, SC_MANAGER_CONNECT);
if (SCManHandle > 0) then
begin
SvcHandle := OpenService(SCManHandle, sService, SERVICE_QUERY_STATUS);
// if Service installed
if (SvcHandle > 0) then
begin
// SS structure holds the service status (TServiceStatus);
if (QueryServiceStatus(SvcHandle, SS)) then
dwStat := ss.dwCurrentState;
CloseServiceHandle(SvcHandle);
end;
CloseServiceHandle(SCManHandle);
end;
Result := dwStat;
end;
// if MySQL not running then sleep until its running
procedure TForm1.FormCreate(Sender: TObject);
begin
while(ServiceGetStatus(nil, 'MySQL5.5') <>4 ) do
begin
sleep (200);
end;
end;
我想知道我的做法是否正确?如果不建议相同。
也可以不用windows编程也能做到吗?
最佳答案
在主线程中休眠从来都不是一个好主意。
最好在线程中进行等待,并在 MySQL 运行时向主线程发送消息。
回复@mghie 的评论:
Why would waiting on the event be any better (or any different) than calling Sleep()?
事件驱动的 GUI 被认为是良好的编程习惯。无需等待。当事件被激发时,GUI 被告知数据库连接的状态变化。如果您将在 Sleep() 循环中等待,应用程序将显示为无响应。并调用 Application.ProcessMessages 来某种程度上解决这个问题,这确实不是一个好的做法。
如何等待 MySQL 在线程中运行的示例:
const
WM_MySQL_READY = WM_USER + 1; // The unique message id
type
TForm1 = class(TForm)
...
private
procedure OnMySqlReady( var Msg: TMessage); message WM_MySQL_READY;
...
end;
在您的主题中:
Constructor TMyThread.Create( OwnerForm : TForm);
begin
Inherited Create( false);
FOwnerForm := OwnerForm; // Keep for later use
Self.FreeOnTerminate := true;
end;
procedure TMyThread.Execute;
var
SQL_started : boolean;
sleepEvent : TSimpleEvent;
begin
sleepEvent := TSimpleEvent.Create;
try
repeat
SQL_started := (ServiceGetStatus(nil, 'MySQL5.5') = 4);
sleepEvent.WaitFor(200); // Better than sleep();
until SQL_started or Terminated;
finally
sleepEvent.Free;
end;
// Inform main thread
PostMessage( FOwnerForm.Handle,WM_MySQL_READY,WPARAM(SQL_started),0);
end;
好吧,我有点误解@mghie,他的问题是为什么线程内的TSimpleEvent.WaitFor() 比Sleep() 好
。
有关背景,请参阅:thread-sleep-is-a-sign-of-a-poorly-designed-program .
简而言之,Sleep()
将线程置于深度 sleep 状态并且控制权不会以最佳周期率返回(如果在某些极端情况下出现)。
TSimpleEvent.WaitFor()
在计时和唤醒方面响应更快。 (请记住,Windows 不是真正的实时操作系统,无法保证时间)。不管怎样,根据经验,在线程中使用 TSimpleEvent.Waitfor() 而不是 Sleep()。
如果需要停止等待连接到 MySQL 服务器,可以对代码进行一些调整:
constructor TMyThread.Create(OwnerForm: TForm; cancelEvent : TSimpleEvent);
begin
inherited Create(false);
FOwnerForm := OwnerForm; // Make sure it's assigned
FCancelEvent := cancelEvent; // Make sure it's assigned
Self.FreeOnTerminate := true;
end;
procedure TMyThread.Execute;
var
SQL_started : boolean;
cancel : boolean;
begin
repeat
SQL_started := (ServiceGetStatus(nil, 'MySQL5.5') = 4);
cancel := (FCancelEvent.WaitFor(200) = wrSignaled);
until SQL_started or Terminated or cancel;
// Inform main thread
PostMessage( FOwnerForm.Handle,WM_MySQL_READY,WPARAM(SQL_started),0);
end;
要在建立连接之前中止线程,只需调用 MyEvent.SetEvent。
如果您想通知用户等待期间发生的事情,您甚至可以从线程中显示启动画面。
参见 Peter Below's Threaded Splashscreen for Delphi对于这样的例子。请注意,此代码不使用任何 VCL 组件或任何涉及与主线程同步的内容。
您可能还想看看:Show a splash screen while a database connection (that might take a long time) runs .
关于mysql - 在启动时调度服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10860234/
每当我运行命令以将 Virtualbox 驱动程序启动 Minishift 到操作系统主机时,它都需要一段疯狂的时间,而且它永远不会结束。有时我什至收到有关达到存储限制的错误消息。 不知道是不是描述h
您好,我正在使用 npm 运行一个基本的 React 项目,我正尝试在 docker 容器中启动它。但是我实际上无法让项目运行。我的 dockerfile 看起来像这样: FROM node:7.8.
所以我想从我的 SSH 终端开始游戏。 这真的很奇怪,当我直接从 Linux GUI 执行此操作时,它可以工作。但是当我使用 SSH 客户端进行远程连接时,它就崩溃了。似乎与我的显示驱动程序有关。 U
我有一个显示图像的动态壁纸。我在 Activity 中更改了该图像。然后我需要通知动态壁纸,以便它知道重新加载资源。 Intent 似乎是完美、简单的解决方案: Intent intent = new
我有一个似乎无法解决的问题。我在 Boot Dashboard 中使用 STS 3.9.2 从 Eclipse (Oxygen) 启动 Spring Boot 应用程序没有任何问题: 但是,当我尝试从
全新的 Python,在我开始摆弄东西之前先设置和安装东西。我的理解是 Python 2.7 和 Python 3.3 之间存在一些显着差异/不兼容,尽管这两个版本都得到了很好的使用,所以我认为最好安
在使用了很长时间的 jQuery 之后,我有一个问题,我正在使用 jQuery 模式(样式)编写一个简单的代码, (function(window, undefined) { var jQu
我正在尝试在 spring boot 应用程序下的非 spring 托管类中配置 Autowired。我在 tomcat 服务器下部署的 Web 应用程序下成功运行了这个。但是当我想在 spring
我对 xmonad 完全陌生,但我想开始使用它来提高我的工作效率。 这是我一直在使用的指南(我使用的是 Apple OS X Snow Leopard) http://xmonad.org/tour.
我试图将Spring Boot指南中的Managing Transactions示例扩展到两个数据源,但是@Transaction注释似乎仅对其中一个数据源有效。 在“Application.java
conEmu 有没有办法默认打开多个不同的选项卡? 我看到这个页面解释了如何使用 splits , 我意识到我可以按 Ctrl + T, 1, Enter,但我希望有一种方法可以自动执行此操作! "%
我正在寻找快速而肮脏的答案。我当时脑子一片空白,盯着屏幕看了 12 个小时以上,我想我中枪了。 我想做一个简单的 SignalR 应用程序作为教程。我找到了这个example ,但我不断收到票证未定义
我正在使用 Azure Powershell cmdlet 来启动/停止 VM。 Start-AzureVM [-ServiceName] [-Name] [ ] Stop-AzureVM [-S
我想使用Powershell脚本代码启动/停止iis和mssql 意味着当我运行ps脚本时,我想启动/停止iis和mssql 我在网上搜索了它,发现了一些代码,但按照我的要求无法正常工作 码: $ii
我在 liferay 工作。我们在我们的项目中使用一个模块来创建 liferay 主题。我使用命令 ant -Ddeploy.war=true 将它部署在服务器中。 war 文件在 liferay 部
我想在已安装 Python 2.7 的 Windows XP 计算机上运行 IPython(版本 0.12)。 我通过 Windows 二进制安装程序安装,但安装后 IPython 没有显示在菜单中,
我从创建了自己的简单图片。 FROM python:2.7.11 RUN mkdir /extra/later/ \ && mkdir /yyy 现在,我可以执行以下步骤: docker run
$(document).ready(function () { setTimeout(function() { window.location.reload(); }, 2000); // 2
我刚刚创建了一个帐户 OpenWeatherMap 我想通过城市 ID API 调用获取当前位置的天气: http://api.openweathermap.org/data/2.5/weather?
我注意到,如果我更改 xcasset 中的图像,启动 Storyboard不会更新。 例如,假设您的启动 Storyboard中有一个 UIImage View ,其中包含一个名为“logo”的蓝色图
我是一名优秀的程序员,十分优秀!