有关handle的问题

smartcobra 2006-11-08 06:22:06
在application事件的open事件中加入如下脚本

if handle(this,true)<>0 then
messagebox("","程序已运行")
return
end if
但已启动的程序依然可以再启动,何故?
...全文
168 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamx 2006-11-10
  • 打赏
  • 举报
回复
建议不要照帮助上说的做,那个做法有bug的。
我是这样写的:

public boolean of_hasrun()
il_handle = createmutex(0, 1, is_appname)
if waitforsingleobject(il_handle, 0) > 0 then
return true //
else
return false
end if

is_appname 是应用名,随便取值。il_handle 是我作其他用的.

当然啦,你还要先声名这些APIs:
Public Function long CreateMutex(long security, long hasOwner, string Name) library "kernel32.dll" alias for "CreateMutexA;Ansi"

Public Function long WaitForSingleObject(long hHandle, long dwMilliseconds) library "kernel32.dll" Alias for WaitForSingleObject

Public Function long CloseHandle(long hHandel) library "kernel32.dll"

Public Function long GetWindow(long hHandle, long wCmd) library "user32.dll"

当然最后要 CloseHandle(il_handle)。
leyueming 2006-11-08
  • 打赏
  • 举报
回复
if handle(this,true)<>0 then
messagebox("","程序已运行")
return
end if
判断已找开,你应该关闭啊,没关闭,他当然可以再找开罗
youqiong 2006-11-08
  • 打赏
  • 举报
回复
一起学习,帮助如下:
For applications, the Handle function does not return a useful value when the previous flag is true. You can use the FindWindow Windows function to determine whether a Windows application is already running. FindWindow returns the handle of a window with a given title.
Declare FindWindow and SetForegroundWindow as global external functions:

PUBLIC FUNCTION unsignedlong FindWindow (long &

classname, string windowname) LIBRARY "user32.dll" &

ALIAS FOR FindWindowA

PUBLIC FUNCTION int SetForegroundWindow (unsignedlong &

hwnd) LIBRARY "user32.dll" ALIAS FOR &

SetForegroundWindowA

Then add code like the following to your application's Open event:

unsignedlong hwnd



hwnd = FindWindow( 0, "Main Window")

if hwnd = 0 then

// no previous instance, so open the main window

open( w_main )

else

// open the previous instance window and halt

SetForegroundWindow( hwnd )

HALT CLOSE

end if

740

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 脚本语言
社区管理员
  • 脚本语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧