When If App.PrevInstance doesn't detect a previous instance - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: When If App.PrevInstance doesn't detect a previous instance

  1. #16
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Mutex...Really nice solution

    Originally posted by hspc
    anyway this is my solution for this prolem :
    [....]

    How does it work ?
    it creates a mutex (shared object that hace a uniqe name)..
    if you try to recreate it you get the error :
    ERROR_ALREADY_EXISTS
    this is the whole idea...
    I like your solution.

    Another way (not this good, however)
    is to have a check in sub main for a particular form
    (FindWindow api will find it even if it is not visible)
    you load and do not show. If you find it, program is
    already running, if not, it is first instance
    ie attached code

    But your solution is better...
    Attached Files Attached Files
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  2. #17
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,209
    Cimperiali..
    thank you for the nice comment
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #18
    Join Date
    Jan 2002
    Location
    Quebec/Canada
    Posts
    124
    If anyone is interested, here is my solution to PreviousInstance : DDE or LinkItem as VB call it.

    On the main form, have these properties set :
    LinkMode : 1 - Source
    LinkTopic : "AppLinkTopic" [Can be anything]

    Then on my Form Load using an invisible textbox I named TxtDDE :
    Code:
    TxtDDE.LinkMode = 0 ' None
    TxtDDE.LinkTopic = "YourApplicationTitleHere|AppLinkTopic"
    On Error goto NoDDE
    TxtDDE.LinkMode = 2 ' Manual - Link to existing dde host
    TxtDDE.LinkExecute "SHOWUP" ' When host receive this, it should activate itself
    Unload Me
    End
    
    NoDDE:
    ' Continu loading here.
    Here is the code I use to deal with the SHOWUP command (on the main form) :
    Code:
    Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
      If (UCase(CmdStr) = "SHOWUP") Then 
        FrmMain.Visible = True
        FrmMain.WindowState = vbNormal
        FrmMain.SetFocus
      End If
      ' I use the same method to notify my application to quit ( for updates )
      If (UCase(CmdStr) = "QUIT") Then FrmLoad.Menu_TrayQuit_Click 
    End Sub

  4. #19
    Join Date
    Nov 2005
    Posts
    1

    Arrow Re: When If App.PrevInstance doesn't detect a previous instance

    this works for me but only shows a message:

    If App.PrevInstance Then
    MsgBox "Application is already running"

    n i need to load the previnstance, here's the code for getting the program to the taskbar:

    Private Sub Form_Resize()

    If Me.WindowState = 1 Then
    'The user has minimized his window

    Call Shell_NotifyIcon(NIM_ADD, IconData)

    ' Add the form's icon to the tray

    Me.Hide

    ' Hide the button at the taskbar

    End If

    End Sub

    Private Sub Form_Load()

    With IconData

    .cbSize = Len(IconData)
    ' The length of the NOTIFYICONDATA type

    .hIcon = Me.Icon
    ' A reference to the form's icon

    .hwnd = Me.hwnd
    ' hWnd of the form

    .szTip = "Cyber-Net Lite" & Chr(0)
    ' Tooltip string delimited with a null character

    .uCallbackMessage = WM_MOUSEMOVE
    ' The icon we're placing will send messages to the MouseMove event

    .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
    ' It will have message handling and a tooltip

    .uID = vbNull
    ' uID is not used by VB, so it's set to a Null value

    End With

    End Sub

    Private Sub mnuExit_Click()

    Unload Me
    ' Unload the form

    End
    ' Just to be sure the program has ended

    End Sub

    Private Sub mnuShow_Click()

    Me.WindowState = vbNormal
    Shell_NotifyIcon NIM_DELETE, IconData
    Me.Show

    End Sub
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim msg As Long

    msg = X
    ' The message is passed to the X value

    ' You must set your form's ScaleMode property to pixels in order to get the correct message

    If msg = WM_LBUTTONDBLCLK Then
    ' The user has double-clicked your icon

    Call mnuShow_Click
    ' Show the window

    ElseIf msg = WM_RBUTTONDOWN Then
    ' Right-click

    PopupMenu mnuPopup
    ' Popup the menu

    End If

    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    Shell_NotifyIcon NIM_DELETE, IconData
    Cancel = -1
    End Sub

    Can anybody please help me?

    thnx

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center