Page 1 of 1

Make a program to run in the background No form interface, no console, no interface at all Rate Topic: -----

#1 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 03 August 2008 - 03:27 PM

This tutorial deals with how to make a standard VB.NET application run in the background, like a process. By background i mean that the application will have no interface (form or console) and will run from a Sub Main(), but will not terminate once all the code in Main has executed. That means your program will run like a normal form application, and will only exit when told to. First, create a standard Windows Form application. Then, go to the solution explorer, and double click on the item that says My Project.
[attachment=7480:attachment]
Once that is opened, you should see something like this
[attachment=7484:attachment]
There are a few very simple things you need to change there. First, uncheck the box that says "Enable Application Framework". Then, go to the box that says "Startup Object:" and change that to Sub Main. Then, delete the form that was put in your application automatically. By now, you should notice that there is no startup object in your application. In fact, there is no Code in your application. So, you need to add a Sub Main in. Add a new module to your application (call it anything you want). Then, add this sub into your module:
Sub Main()
    'This will execute when your application
    'starts up. This is the equivilent of a
    'Form_Load event in a form application.
    'Put whatever code you want in this sub,
    'but make sure you end it with this statement:
    Application.Run()
End Sub


Let me explain exactly what this is doing. When you changed the startup object to Sub Main, you application will execute from a Main routine, like a console application, so what is stopping it from showing a console window? Notice the box that says "Application Type:". You will see that that is set to "Windows Form Application". When you create a console application, that box is set to "Console Application". When you create any windows form application, there, of course, is no console window. That is because the application has a type of "Windows Form Application", which basically means your application will not show a console. Of course, in Consle Applications, there are no forms. So what happens when you application's type is Windows Form Application, and there is no form in it? Your program will have no interface at all. But, even though your application is called a Windows Form Application, it will still exit once all the code in Sub Main has executed, like a console application. To prevent that, you must have a line at the end of your Main routine that says Application.Run(). That line will prevent your application from closing right after Main has finished. Now, it will run like a standard form application, just with no form, and the only way you can close it is with an Application.Exit() call. And it's as simple as that. You can treat the module your main routine is in as if it was the code for your form. The only difference is, there is no form, and you can't add controls to it in the designer (because there is no interface there to design). So, for example, if you wanted to add a timer to your application, you can't just drag-and-drop it on the form, you have to get down and dirty and add it in manually, with something like this
Friend WithEvents Timer1 As New Timer()


That will create a timer object, exactly as if it was created by the designer. To add a tick event handler for it, go to the box near the top-left of the code editor. Select the item that says "Timer1", or whatever you called your timer. Then, select the box next to it and it will show a list of events for that control. Click the one you want, Tick in this case, and it will add an event handler for that event directly into your code.

Is This A Good Question/Topic? 3
  • +

Replies To: Make a program to run in the background

#2 Al101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 09

Posted 06 February 2009 - 12:26 PM

It is a good solution to problem that I have.
I would like to know how to make it work with C#
Thanks
Was This Post Helpful? 0
  • +
  • -

#3 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Posted 06 February 2009 - 05:50 PM

Well, it's basically the same thing. In your Program.cs file, delete the code from main, and put Application.Run(); in it just like the tut says. Do the same adjustments to the project properties as well.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_polas*


Reputation:

Posted 04 May 2010 - 12:20 AM

Just go to google and type vb.net to c online converter :)
I hope you helped ;)
Was This Post Helpful? 0

#5 Guest_bharath*


Reputation:

Posted 18 June 2010 - 03:48 AM

I want to know how to run a vb application such that its there on desktop everytime but i can run other applications do anything else , eg: just like those pair of eyes in linux , how do i make an application like that in vb.net
Was This Post Helpful? 0

#6 ???  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 48
  • Joined: 25-November 10

Posted 26 December 2010 - 02:27 PM

Great code, I didn't think it was possible to have Events in a Module. I also discovered that in a Console Application if a reference to System.Windows.Forms is added it is possible to create code to run in the background while the console application is running.
Was This Post Helpful? 0
  • +
  • -

#7 nandgate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 13

Posted 27 April 2013 - 12:14 AM

Hi,
How can i call that timer in main block?? and how to assign timer value (time period, in my case 1 minute)?
Was This Post Helpful? 0
  • +
  • -

#8 nandgate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 13

Posted 25 July 2013 - 06:00 AM

Thanks, the post was really helpful.

i have a question, how can i make this kind of application a single instance application ?
any suggestions ??


Thanks and regards,

Anand.
Was This Post Helpful? 0
  • +
  • -

#9 jamescollett  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 27-August 13

Posted 27 August 2013 - 04:28 AM

View Postnandgate, on 25 July 2013 - 06:00 AM, said:

i have a question, how can i make this kind of application a single instance application ?
any suggestions ??

Anand.

Anand,

Are you asking how to run a Window-less executable just the once and then finish, rather than have it run again and again in the background? If that is what you are asking, then I believe that you need to simply omit the end statement:-

Application.Run()



This is what I want to achieve and this seems to work for me.
Was This Post Helpful? 0
  • +
  • -

#10 nandgate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 13

Posted 27 August 2013 - 05:46 AM

View Postjamescollett, on 27 August 2013 - 04:28 AM, said:

View Postnandgate, on 25 July 2013 - 06:00 AM, said:

i have a question, how can i make this kind of application a single instance application ?
any suggestions ??

Anand.

Anand,

Are you asking how to run a Window-less executable just the once and then finish, rather than have it run again and again in the background? If that is what you are asking, then I believe that you need to simply omit the end statement:-

Application.Run()



This is what I want to achieve and this seems to work for me.

Was This Post Helpful? 0
  • +
  • -

#11 nandgate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 13

Posted 27 August 2013 - 05:57 AM

Hi James,

Thanks for the reply.
But that is not what i have meant. Whenever we build/rebuild a project a .exe files gets created. If we double click on that .exe file, the application starts working. If i make a windows form project a single instance application, no matter how many times i double click on that .exe file, only one instance of that windows form project will work. But if i dont make my project as a single instance application, and say i click on that exe 3 times, 3 instances of my project will run simultaneously. (aka i can see three windows form projects running). So i want to know whether i can make my back ground application a single instance application (so that my .exe file has only one running instance).

Sorry for my bad English.

With regards,

nandgate.
Was This Post Helpful? 0
  • +
  • -

#12 jamescollett  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 27-August 13

Posted 27 August 2013 - 06:29 AM

View Postnandgate, on 27 August 2013 - 05:57 AM, said:

..
But that is not what i have meant. Whenever we build/rebuild a project a .exe files gets created. If we double click on that .exe file, the application starts working. If i make a windows form project a single instance application, no matter how many times i double click on that .exe file, only one instance of that windows form project will work. But if i dont make my project as a single instance application, and say i click on that exe 3 times, 3 instances of my project will run simultaneously. (aka i can see three windows form projects running). So i want to know whether i can make my back ground application a single instance application (so that my .exe file has only one running instance).

Sorry for my bad English.

Your English is fine, nandgate! I understand your question now.

I have never purposefully used the "Make single instance application" feature, which I see is controlled via a checkbox on the My Project -> Properties -> Application page. And I see also that if you uncheck the "Enable Application Framework" checkbox, that feature becomes greyed out. Hence your problem, I think.

I have found what might be a way to achieve the required result, using Mutex, described in the thread on the following page at Stackoverflow.com :-

http://stackoverflow...at-does-this-do

(stackoverflow.com/questions/1328792/make-single-instance-application-what-does-this-do)

This might get you where you want to be. Good luck!
Was This Post Helpful? 1
  • +
  • -

#13 nandgate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 13

Posted 27 August 2013 - 06:52 AM

View Postjamescollett, on 27 August 2013 - 06:29 AM, said:

View Postnandgate, on 27 August 2013 - 05:57 AM, said:

..
But that is not what i have meant. Whenever we build/rebuild a project a .exe files gets created. If we double click on that .exe file, the application starts working. If i make a windows form project a single instance application, no matter how many times i double click on that .exe file, only one instance of that windows form project will work. But if i dont make my project as a single instance application, and say i click on that exe 3 times, 3 instances of my project will run simultaneously. (aka i can see three windows form projects running). So i want to know whether i can make my back ground application a single instance application (so that my .exe file has only one running instance).

Sorry for my bad English.

Your English is fine, nandgate! I understand your question now.

I have never purposefully used the "Make single instance application" feature, which I see is controlled via a checkbox on the My Project -> Properties -> Application page. And I see also that if you uncheck the "Enable Application Framework" checkbox, that feature becomes greyed out. Hence your problem, I think.

I have found what might be a way to achieve the required result, using Mutex, described in the thread on the following page at Stackoverflow.com :-

http://stackoverflow...at-does-this-do

(stackoverflow.com/questions/1328792/make-single-instance-application-what-does-this-do)

This might get you where you want to be. Good luck!


Thanks. it really works.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1