Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Single Instance Application using Events

Single Instance Application using Events

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++comlinux
1 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    opsoft op
    wrote on last edited by
    #1

    What can be issue of using EVENTS object for single instance application for single user on a desktop. I have seen this method given in the link, which is quite clean solution to the problem but using mutex and then boradcasting message is bit concern. http://www.codeproject.com/threads/simplesingleinstanceapp.asp[^] Here is what I inted to do when using events for process communication. step1: Create an unique event in the ininstance method in Reset mode. Check if event is already created. If event not created then create event and create screenControl thread to listen for set/reset of that event. If event is there then means window is already present and hence set the event here so that running thread can wakeup. // OnlyOne.cpp // global data HANDLE hEvent; int ShutDownFlag; void ScreenMonitor(LPVOID pParam); // COnlyOneApp initialization BOOL COnlyOneApp::InitInstance() { ///// other stuff... CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(COnlyOneDoc), RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(COnlyOneView)); AddDocTemplate(pDocTemplate); //------ hEvent = CreateEvent(NULL,TRUE,FALSE,_T("ONLYONE-APP")); AlreadyRunning = ( ::GetLastError() == ERROR_ALREADY_EXISTS || ::GetLastError() == ERROR_ACCESS_DENIED); if (AlreadyRunning) { MessageBox(NULL,"Already Running....","Message!!",MB_OK); SetEvent(hEvent); return TRUE; } //------- // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; //-------- CWinThread *wndThread = AfxBeginThread((AFX_THREADPROC)ScreenMonitor, m_pMainWnd->GetSafeHwnd(),THREAD_PRIORITY_NORMAL,0,0); if (wndThread == NULL) { MessageBox(NULL,"Could not Create Thread...","MSG",MB_OK); return FALSE; } //------- m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } step 2: Thread method. This method waits for events to set. When event is set which signifies same application tried to start, this thread brings first instance in the foreground. // COnlyOneApp message handlers void ScreenMonitor(LPVOID pParam) { CW

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups