這是我在Windows 7上發現的內容。即使先前的實例已存在,Windows上的潮汐sdk應用程序也會在快捷方式上雙擊時創建新實例。您可以在Tide SDK Developer(1.4.2)上觀察同樣的事情,只需運行該應用程序,如果再次單擊該快捷方式,它將啓動一個新實例,而不是僅顯示現有的實例。有誰知道如何解決這一問題?還是有人解決了這個問題?雖然Windows上的多個實例
2
A
回答
0
所以,我找到了一種方法在Windows中解決這個
的OSX版本不顯示這樣的問題。使用Ti.Process創建過程,並讓它運行以下命令
tasklist | find "YourAppName"
如果一個實例已經存在,它返回的字符串,如果沒有它返回一個空字符串。您可以在每次啓動應用程序時檢查此內容以避免多個實例
0
在後臺運行tasklist
取決於平臺,此解決方案僅阻止新實例啓動 - 它不會關注已運行的實例。還有另一種解決方案,它使用Ti.FileSystem.File.touch()
方法:此方法將嘗試創建指定文件,如果成功則返回true
;如果文件已存在,則返回false
。最重要的是,它是原子的,這意味着它不應該爲你的應用引入競爭條件。
要集中已經運行的實例,你需要讓它知道它應該(顯示和)自己集中。一種工作方式是在應用程序知道它是唯一運行的實例時運行HTTP服務器,並在接收到請求時自行關注。如果應用程序在啓動時發現另一個實例已在運行,請創建一個HTTP客戶端,然後連接到另一個實例的HTTP服務器並向其發送請求;當請求完成時,退出。
例實現(在你的app.js文件的開頭放):
// enclose the logic in a closure, just to play it safe
(function(pidFile) {
// if creating the PID file fails, i.e. there is another instance
// of the app already running
if (!pidFile.touch()) {
// create a HTTP client
var client = Ti.Network.createHTTPClient({});
// add some event handlers
client.onload = function() {
Ti.App.exit();
};
client.onerror = function() {
Ti.App.exit();
};
// and dispatch
client.open('GET', 'http://localhost:9731/');
client.send();
} else {
// or, if creating the PID file succeeds,
// create a HTTP server and listen for incoming requests
var server = Ti.Network.createHTTPServer();
server.bind(9731, 'localhost', function(request, response) {
// this handler gets run when another instance of the app
// is launched; that's where you want to show the app window
// if it is hidden and focus it
if (!Ti.UI.getMainWindow().isVisible()) {
Ti.UI.getMainWindow().show();
}
Ti.UI.getMainWindow().focus();
// send some response back to the other instance
response.setContentType('text/plain');
response.setContentLength(2);
response.setStatusAndReason('200', 'OK');
response.write('OK');
});
// an important thing is to clean up on application exit
// - you want to remove the PID file once the application
// exits, or you wouldn't be able to run it again until you
// deleted the file manually, something you don't want the end user
// to deal with
Ti.API.addEventListener(Ti.EXIT, function() {
server.close();
pidFile.deleteFile();
});
}
// now call the closure passing in an instance of the Ti.Filesystem.File class
// wrapping the PID file in you app data directory
})(Ti.Filesystem.getFile(Ti.API.Application.getDataPath(), 'run.pid'));
有可能是一種更輕質的方式通過Ti.Network.TCPSocket
類的兩個實例連接(我現在就看着我自己)。還有一點需要記住的是,應用程序可能會崩潰,無法自行清理,從而無法運行。因此,在HTTPClient的事件處理程序onerror
中,比戒菸更爲謹慎的方法是向用戶展示一段對話,其中包括「該應用程序已在運行且未響應,或最近已崩潰」。您想要強制啓動應用程序?「此外,服務器的端口可能已經被另一個應用程序使用,或者可能是來自應用程序崩潰前一次運行的殭屍 - 所提供的代碼沒有考慮到這一點(只是說)。
相關問題
- 1. 單個Windows Azure網站實例上的多個網站
- 2. Windows Server AppFabric配置的多個實例
- 3. Windows Azure中同一實例上的多個角色
- 4. 在Windows上有多個實例的會話變量Azure
- 5. Gedit在Windows中創建多個實例
- 6. Windows Phone多個應用程序實例
- 7. 在單臺Windows 2003計算機上安裝多個Apache實例
- 8. 如何在Windows Azure上使用asp.net緩存與多個實例
- 9. 如何在多個EC2 Windows實例上執行CMD命令?
- 10. 多個實例
- 11. 多個實例
- 12. 多個實例
- 13. 多個實例
- 14. 多個實例
- 15. 多個實例
- 16. 多個實例
- 17. 多個實例
- 18. 多個實例
- 19. Windows通用應用程序的多個實例(Windows 10)
- 20. Android上的多個地圖框實例
- 21. 背上的多個實例按
- 22. 多取()上的單個實例
- 23. preg_replace在1行上的多個實例?
- 24. 多個實例上的閉包
- 25. ubuntu上的多個實例mysql錯誤
- 26. Amazon EC上的多個Tomcat實例
- 27. 多個AWS實例/機器上的Docker jmeter實例
- 28. memcache,多個實例與單個實例
- 29. FreeBSD上的Redis多實例
- 30. 在Linux上的共享庫上存在多個單例實例