Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2003, 06:52 PM
Registered User
 
Join Date: Sep 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default How can we prevent a form's session from starting

Hello all,
I want to know how we can prevent a session of form(both Mdi and simple)if its session is already on.Is there any method or property available that tells us if the session of the same instance of the form is on.
I will be really thankful.
Thanks,
Misbah H. Ansari.


 
Old September 26th, 2003, 02:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

If I understand your question correctly, you can determine whether a WINDOW IS OPEN ALREADY using API calls. Here's some logic from an old VB6 program of mine that will iterate through all the open windows using a callback function and set a global boolean flag. Sorry if the formatting is off in this little window. Hopefully you can figure out how to put the code back together and change it to check for the title you're looking for:

Public Declare Function EnumWindows Lib "user32.dll" (ByVal _ lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowText Lib "user32.dll" _ Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As _ String, ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32.dll" _ Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

'************************************************* ********************' EnumWindowsProc enumerates through all of the open windows on the
' system to check for a specific title

Public Function EnumWindowsProc(ByVal lngHwnd As Long, _
                                ByVal lngParam As Long) As Long
Dim lngTitleLength As Long
Dim strBuffer As String ' title bar text length and strBuffer
Dim lngRetVal As Long ' return value

' get length of title bar text for current window

  lngTitleLength = GetWindowTextLength(lngHwnd) + 1

  If lngTitleLength > 1 Then
    strBuffer = Space(lngTitleLength) ' make room in the buffer

    ' get title bar text

    lngRetVal = GetWindowText(lngHwnd, strBuffer, lngTitleLength)
  End If

  If InStr(UCase$(Left(strBuffer, lngTitleLength - 1)), _
   "YOUR WINDOW TITLE GOES HERE") > 0 Then
   ' SET A GLOBAL BOOLEAN OR WHATEVER HERE
   ' TO INDICATE YOUR WINDOW WAS FOUND
  End If

  EnumWindowsProc = 1 ' return value of 1 means continue enumeration
End Function ' EnumWindowsProc

Then call the function using this whereveryou need to from your code

Dim lngRetVal As Long

  ' enumerate through the open windows

  lngRetVal = EnumWindows(AddressOf EnumWindowsProc, 0)
 
Old September 26th, 2003, 03:48 PM
Authorized User
 
Join Date: Jun 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually there are several different ways to do this is .Net

For MIDI forms I usually us a private withevents variable that is local to the whole class and if it isn't nothing then I can't create a new form. You must handle the Closing or Closed event and set the variable to Nothing.

For other forms I use a simple bit of code.

#Region "PREVIOUS INSTANCE"
    Public Function PREVIOUS_INSTANCE() As Boolean
        If UBound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
#End Region

OR YOU CAN USE A MUTEX

#Region "Declarations"
    Private Shared _config As AppStartConfiguration = Nothing
    Private Shared _exePath As String = ""
    Private Shared _process As Process
    Private Shared _appStartMutex As Mutex
    Private Shared _mutexGuid As New Guid(New Byte() {&H5F, &H4D, &H69, &H63, &H68, &H61, &H65, &H6C, &H20, &H53, &H74, &H75, &H61, &H72, &H74, &H5F})
#End Region
#Region "Main"
    <STAThread()> _
     Shared Sub Main()
        Dim isOwned As Boolean = False

        _appStartMutex = New Mutex(True, _config.ExePath + _mutexGuid.ToString(), isOwned)

        If Not isOwned Then
            MessageBox.Show(String.Format(CultureInfo.CurrentC ulture, "There is already a copy of the application '{0}' running. Please close that application before starting a new one.", _config.ExePath))
            Environment.Exit(1)
            Exit Sub
        End If

        StartApp_Process()
    End Sub
#End Region

It's important for us to explain to our nation that life is important. It's not only life of babies, but it's life of children living in, you know, the dark dungeons of the Internet."— George W. Bush - Arlington Heights, Ill., Oct. 24, 2000





Similar Threads
Thread Thread Starter Forum Replies Last Post
Form's calculated field not working properly PaulJH Access 11 March 5th, 2007 09:52 AM
How to put a value into another form's text box? alastair Access VBA 3 December 20th, 2006 06:19 PM
Form's Unbound field question ebburks Access 4 June 6th, 2006 07:37 AM
Form's nightmare C# 3 December 19th, 2004 12:40 AM
Question about form's design view reindeerw Access 15 April 12th, 2004 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.