Results 1 to 8 of 8

Thread: [RESOLVED] making a vb.net app start 2 times

  1. #1

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    63

    Resolved [RESOLVED] making a vb.net app start 2 times

    so Basically, i want my vb.net application to start 2 times, by itself, how to do that, should i use a timer ?

    i have an other silly question, i want to remove the blue default icon of VB.net, to a default windows exe icon, how to do dat?

    PLease help

  2. #2
    Addicted Member AnthonyGrimes's Avatar
    Join Date
    Sep 2008
    Location
    United States
    Posts
    131

    Re: making a vb.net app start 2 times

    Changing the default icon was answered here: http://www.vbforums.com/showthread.p...(default-icon)

    A timer would work to start your application multiple times.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: making a vb.net app start 2 times

    The issue you have when running any sort of code to open your app multiple times is that, it's going to run in the second application too. What I would do is in you form_load event, check if there is an instance of your application, if not then create a new instance, if so then do nothing:
    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            'Get all instances of WindowsApplication1
            'If your application name is something else, then change the "WindowsApplication1" to it's name
            Dim my_application() As Process = Process.GetProcessesByName("WindowsApplication1")
    
            'Check if there is only 1 instance of my application
            If my_application.Count <= 0 Then
                'Create new instance
                Dim new_form As New Form1
                new_form.Show()
                new_form.Focus()
            End If
    
        End Sub
    But even in this example, it will open multiple instances of Form1 because it keeps running until the my_application.Count is greater than 0. So perhaps someone else could shed some light.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: making a vb.net app start 2 times

    I'd create a bootstrap program... when it starts up, it launches the "real" app twice then shuts down... A console app with two calls to process.Start should be sufficient.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: making a vb.net app start 2 times

    dday9 has the correct idea, however since you're using VS2008 there is no Count method on the array returned by GetProcessByName since that is an extension method introduced in VS2010. Hard coding in the process name is also not really recommended, you can use a function such as this:
    Code:
      Public Function PreviousInstanceCount() As Integer
        Return Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length - 1
      End Function
    You can call this from your Form_Load event and check if the return value is 0 then you need to launch your application a second time.
    Code:
        If PreviousInstanceCount = 0 Then
          Process.Start(Process.GetCurrentProcess.MainModule.FileName)
        End If
    Note that this will not work while you run the application in debug mode from Visual Studio but it will work when you launch the compiled application (or start it with full compile, Ctrl+F5)

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: making a vb.net app start 2 times

    Could there not be any conflict if another application had the same name?

    vb Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As Object,
    3.                            ByVal e As System.EventArgs) Handles Me.Load
    4.  
    5.         Dim instance As Boolean
    6.         Dim mutex As New Threading.Mutex(False, "Local\ApplicationName", instance)
    7.  
    8.         If instance Then
    9.             Process.Start(Process.GetCurrentProcess.MainModule.FileName)
    10.         End If
    11.     End Sub
    12. End Class

  7. #7

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    63

    Re: making a vb.net app start 2 times

    Thanks guys that really helped !

  8. #8
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: making a vb.net app start 2 times

    Please mark your thread resolved from the tools menu.

Posting Permissions

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



Click Here to Expand Forum to Full Width