?
Solved

Run function at Visual Studio Project startup

Posted on 2012-08-21
10
Medium Priority
?
542 Views
Last Modified: 2012-08-24
Is there a way I can have my vs2008 project run a function at startup before it opens the startup form?
0
Comment
Question by:trbaze
  • 4
  • 4
  • 2
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38317731
VB.NET or C#?
0
 
LVL 3

Author Comment

by:trbaze
ID: 38318184
This would be for vb.net.  I'm making the migration from VB6.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1600 total points
ID: 38318306
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 3

Author Comment

by:trbaze
ID: 38318357
So if I uncheck the 'Enable application framework', will i still be able to use or set those properties, vb or otherwise?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38318449
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
 
LVL 40
ID: 38318838
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
 
LVL 3

Author Comment

by:trbaze
ID: 38320575
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
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 400 total points
ID: 38320710
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38322452
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
 
LVL 3

Author Closing Comment

by:trbaze
ID: 38329230
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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Integration Management Part 2
Loops Section Overview
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question