使用定时器等进行应用?
我需要按如下方式提出申请。
如果该字段的值为 1 或 0,则需要每秒执行 10 次查询来检查数据库 (Ms SQL 2005)。如果为 1,则应用程序需要启动 Windows Media Player(全屏)并运行视频。 应用程序需要在视频播放过程中休眠,视频结束后,应用程序需要唤醒并关闭 Windows Media Player,打开 Internet Explorer(全屏)并显示网站。
我不知道如何做到这一点,因为我对 Delphi 没有那么丰富的经验。
I need to make an application as followed.
A query needs to be executed 10 times a second to check in the database (Ms SQL 2005) if the value of the field is 1 or 0. if its 1 then the appication needs to start Windows Media Player (Fullscreen) and run a video.
The application needs to sleep during the video and when the video is finished the application needs to awake and close Windows Media Player and open Internet Explorer(fullscreen) and show a website.
I have no idea how to make this im not that experienced with Delphi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
TTimer
拖放到 Delphi 表单上。将计时器的间隔属性设置为 100(以毫秒为单位)。为检查数据库的OnTimer
事件分配一个事件处理程序。将Enabled
属性设置为False
。当您希望计时器开始触发事件时,请将“Enabled”设置为“True”。如果您希望禁用它,请将
Enabled
设置回False
。需要注意的一件事是,计时器事件处理程序的运行时间是否超过计时器间隔。如果发生这种情况,则处理程序的调用之间将不会有暂停。这不会造成任何严重的危害,但可能会导致您的应用消耗 100% CPU。一种防御技术是在事件处理程序开始时将计时器的 Enabled 属性设置为 False,然后在退出处理程序之前将其设置回 True。
重新阅读你的问题,我突然想到,也许你是在问如何进行数据库访问、启动媒体播放器、在视频播放期间睡觉、等待它完成、打开 IE 等。
你不能问如何做所有这些在一个问题中(请参阅常见问题解答,了解 Stack Overflow 上有效问题的描述)。如果您确实想要所有这些问题的答案,那么我们将结束这个问题,因为它太宽泛了。
我上面的回答为您问题的计时器部分提供了一些指导。
Drop a
TTimer
onto a Delphi form. Set the timer's interval property to 100 (measured in milli-seconds). Assign an event handler for theOnTimer
event that checks your database. Set theEnabled
property toFalse
.When you want the timer to start firing events set
Enabled
to True. When you want it disabled, setEnabled
back toFalse
.One thing to be careful of is if your timer event handler takes more time to run than the timer interval. If that happens then there will be no pause in between invocations of the handler. This won't do any terrible harm, but it may result in your app consuming 100% CPU. A defensive technique is to set the timer's Enabled property to False at the beginning of your event handler, and then back to True just before you exit the handler.
Re-reading your question, it occurs to me that perhaps you are asking how to do the database access, starting Media Player, sleeping during the video, waiting for it finish, opening IE etc.
You can't ask how to do all of that in one question (please see the FAQ for a description of what is a valid question on Stack Overflow). If you really want answers to all of that then we'll just close the question as being too broad.
My answer above gives you some orientation for the timer part of your question.
如果您不需要 delphi 表单应用程序,您可以使用服务器端网页(php、asp.net、jsp 无论您喜欢什么)来完成此操作。
初始页面加载(服务器端)将检查您的数据库(1 或 0)?
客户端页面每n秒刷新一次。
...或播放视频并重定向到网站
n秒后网站重定向到初始页面。
如果您无法控制需要显示的网站,您可以在框架内调用它。
If you don't require a delphi form application you could do this with a server-side web page (php, asp.net, jsp whatever's your favorite).
Inital page load (server side) would check your database (1 or 0)?
Client side page would refresh every n sec.
... or play video and redirect to website
Website redirects to Inital page after n sec.
If you can't control the website you need to display you could call it inside a frame.
我不会连续轮询该记录,而是使用触发器来告诉我的应用程序值何时发生更改 - 当然,如果您可以将这样的触发器添加到数据库中。
如果您无法添加触发器,我将使用单独的线程来检查字段更改(使用使用绑定变量准备的适当查询以避免语句重新解析开销)。当应用程序播放视频时,线程可以进入睡眠状态 - 检查运行媒体播放器的最佳方式是什么(嵌入式或非嵌入式),因为您需要知道它何时完成播放,而且我不知道这是否容易如果 Media Player 使用 ShellExecute 或 CreateProcess 作为外部应用程序运行,则可以获取该信息,而运行外部应用程序并等待其终止则很容易。
打开资源管理器只需使用正确的参数调用 ShellExecute(或 CreateProcess)即可。
Instead of polling that record continuosly, I'd use a trigger to tell my application when the values has changed - of course if you can add such a trigger to the database.
If you can't add a trigger, I'd use a separate thread to check for the field change (use a proper query prepared with bind variables to avoid statement reparsing overhead). The thread can be put to sleep while the application plays the video - check what's the best way to run Media Player (embedded or not), because you need to know when it has finished playing, and I do not know if it is easy to obtain that information if Media Player is run as an external application using ShellExecute or CreateProcess, while it is easy enough to run an external application and wait for it to terminate.
Opening Explorer is just a matter of calling ShellExecute (or CreateProcess) with the right parameters.