• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

Run function at Visual Studio Project startup

Is there a way I can have my vs2008 project run a function at startup before it opens the startup form?
0
trbaze
Asked:
trbaze
  • 4
  • 4
  • 2
2 Solutions
 
käµfm³d 👽Commented:
VB.NET or C#?
0
 
trbazeAuthor Commented:
This would be for vb.net.  I'm making the migration from VB6.
0
 
käµfm³d 👽Commented:
Hopefully Idle_Mind will pop in here with a cleaner way to do this  : )

You can add a new module to your project, and within that file create a new Sub Main(). Within this Sub, execute your function, and then make a call to Application.Run, passing in a new instance of your form:

e.g.

Module Startup
    Sub main()
        Run()

        Application.Run(New Form1())
    End Sub

    Sub Run()
        MessageBox.Show("Running Function")
    End Sub
End Module

Open in new window


Then open up your project's properties page and on the "Application" tab uncheck the "Enable Application Framework" option. Also, click the "Startup Object" drop-down and select the new "Main" method that you created. Voila!

Screenshot
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
trbazeAuthor Commented:
So if I uncheck the 'Enable application framework', will i still be able to use or set those properties, vb or otherwise?
0
 
käµfm³d 👽Commented:
Which properties? The ones below the "Enable application framework?" If so, then no--at least not from that window. You would have to code all of those features. I am not terribly familiar with all the ins and outs of disabling that feature as I code primarily in C#.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Most professional programmers launch their applications start in a Sub Main instead of a form. This gives you a lot more control over what happens when the application launches and stops, and with time you will learn a lot of tricks that cannot be implemented easily if you start in a Form (multilingual application, global error trap and trace file only to name a few).

Most of the features in "Enable application framework" can be implemented with one line of code:

XP Visual styles can be enabled with : Application.EnableVisualStyles

To shutdown the application : Application.Exit

To save the settings : My.Settings.Save
0
 
trbazeAuthor Commented:
What about the 'Make Single Instance' option?  I've been searching for a way to enable that through VB, but have had no luck.  Also, is it true that you cannot set the aaplication icon in VB, just form icon?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Make single instance:

If Microsoft.VisualBasic.UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) >0 Then
    MessageBox.Show("Application is already running.")
    Application.Exit()
End If

Depends on what you mean by application icon.

If you mean an application in the TaskBar, then you need a form. Programs that do not have a form run without user intervention, and are thus running as a process, not an application, and they do not appear in the TaskBar.

However, if you mean the icon that shows when a short cut to the application is set on the desktop or the Programs menu, you can define one in the Project's properties Application tab.
0
 
käµfm³d 👽Commented:
Make single instance:
There is a relatively small danger using that approach in that if two different applications just happened to have the same name, then your logic fails. A common C# approach to single-instance is to use a system-wide named mutex. You try to grab the mutex when the application starts, and if you can't grab it, then the application is already running.
0
 
trbazeAuthor Commented:
Thank you both for your help.  I ended up puting the code I wanted to run at startup in the Loadevent of my splash screen, that way I could still use the settings in the property of my project.  I'm sure I will be talking with you more since I am new to VB.Net and building forms with Visual Studio 2008 and some thing are not as clear cut as MSAccess.  Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now