1.首先在global external functions聲明外部函數如下:
FUNCTION long FindWindowA( ulong Winhandle, string wintitle ) Library ″user32″
然後在application的 Open 事件中加入如下代碼:
ulong l_handle, lu_class
string ls_name
ls_name = ″我的系統″ // 此處ls_name爲系統主窗口的標題Title
l_handle = FindWindowA(lu_class, ls_name)
if l_handle > 0 then
MessageBox(″提示信息″, ″應用程序″ + This.AppName + ″已經運行,不能多次啓動!″)
Halt Close
else
open(w_main) // 此處爲系統主窗口
end if
這種方法是PowerBuilder聯機幫助中的一個例子,是以系統主窗口的標題Title作爲判別依據,若有其它與此Title同名應用程序在運行,再想啓動此程序也會報應用程序已經運行。你可以將Title設爲“計算器”,然後啓動Windows附件中計算器程序,再運行你的PB應用程序試試。
2.聲明外部函數:
function ulong CreateMutexA (ulong lpMutexAttributes, int bInitialOwner, ref string lpName) library ″kernel32.dll″
function ulong GetLastError () library ″kernel32.dll″
然後在application的 Open 事件中加入如下代碼:
ulong ll_mutex, ll_err
string ls_mutex_name
if handle (GetApplication (), false) <> 0 then
ls_mutex_name = this.AppName + char (0)
ll_mutex = CreateMutexA (0, 0, ls_mutex_name)
ll_err = GetLastError ()
if ll_err = 183 then
// 程序已經運行
MessageBox (″提示信息″, ″程序已經運行了!″)
Halt close
else
// 程序未運行
open(w_main)
end if
else //開發模式
open(w_main)
end if
這種方法必須在應用程序編譯成可執行文件.exe後纔有效。