Solved

How to automate an application

Posted on 2011-09-12
18
261 Views
Last Modified: 2012-06-21
According to the related question, I have an application that performs some tasks, those taks need to be integrated in a "schedule" in order to be performed at intevarls of time, defined by the application itself, how could I do that?
0
Comment
Question by:dimensionav
  • 8
  • 4
  • 3
  • +1
18 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36526653
Is Task Scheduler an option? Then there would be no code involved. If you do prefer code, there is the Quartz project. I haven't used it myself, but one of my coworkers has. IIRC, he didn't indicate that it was difficult to use.
0
 

Author Comment

by:dimensionav
ID: 36526739
Task Scheduler is a good idea but how could I "separate" some processes from others in the task scheduler?, because my app contais not only what I want to automate (main form, child forms, etc.).

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36526746
Well Task Scheduler schedules applications (i.e. .exe files) to run at given times. You would not want to run a GUI application via Task Scheduler because it is a user-driven application. A console application would be a candidate for Task Scheduler. So my next question is, "do you have a console application in your solution, and is that the application you want to schedule?"
0
 

Author Comment

by:dimensionav
ID: 36526757
What I want to run is some code inside a button event, I wonder if this would be a matter of changing this code to a console mode and how to?
0
 

Author Comment

by:dimensionav
ID: 36526762
I am thinking that perhaps I could run that "console code" inside the button and have both options, am I right?
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 166 total points
ID: 36526792
You can't really run a section of code via Task Scheduler, at least not in the way that I feel you are thinking. You would need some application (i.e. console application) to execute for Task Scheduler to be effective. What you *could* do is write a console application that uses your Forms app as a referenced assembly (i.e. Project->Add Reference->Your Forms Project) and then create an instance of your Form, and call the click handler directly. IMHO, you shouldn't do quite this. I would break the logic inside the handler out into a new function that both your click handler and your console application call. If you invoke a handler method directly, it might confuse people that come behind you later, since handlers are generally associated with events and not typically invoked directly.
0
 

Author Comment

by:dimensionav
ID: 36526828
would be possible to develop a console app and then call it from a button ? if yes, this way could be a solution for GUI and console modes.

Would you mind to give an example of how to create a console app?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36526835
You could also pass command line switches to your forms app which would then run the code without an interface (*assuming you broke it out as kaufmed suggests).
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 36526865
would be possible to develop a console app and then call it from a button ?
For that statement, yes. But to make sure I understand what you are asking...

You want to start a Forms application (GUI). On the form, you will have a button. When this button is clicked your Console application will be executed. Is that correct?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 40
ID: 36527055
If the application is small and does nothing in between calls, it could run in the background with a set of timers that trigger the necessary operations at proper intervals.

With all the crap that runs in the background with and without your knowledge, one more (it it is small) won't make a big difference. And at least, it is something useful whose function you know.
0
 

Author Comment

by:dimensionav
ID: 36562365
kaufmed:
You want to start a Forms application (GUI). On the form, you will have a button. When this button is clicked your Console application will be executed. Is that correct?..... YES


JamesBurger:
How could I create an app in the background?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 167 total points
ID: 36562834
An application that runs in the background is an application that does not display a form.

The ideal way to do it is to create a Windows Service (File...New Project... and select Windows Service in the templates). But depending on your level of expertise, it can be more or less easy to code for a service, because you need to do everything in code, such as setting and launching the Timer.

The easiest way to do that is to start the application in a Main procedure that looks like this:
Module Module1
   Public Sub Main()
      Application.Run(New Form1)
   End Sub
End Module

Open in new window

You set Module1 as the startup object in the project's Properties Application tab.

Prepare a form with no control, except a Timer, set for the Interval you want and Enabled. The code in the form would look like this:

Public Class Form1

   Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
      'Perform your scheduled operation here
   End Sub

   Private Sub Form1_VisibleChanged(sender As Object, e As System.EventArgs) Handles Me.VisibleChanged
      If Me.Visible Then Me.Visible = False
   End Sub

End Class

Open in new window

By reacting to the VisibleChanged event the way I do, the form will never be visible (except for a little flash when you launch it).

The Timer will be active however, and will call the Tick event at the interval you have set.
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 167 total points
ID: 36563238
Just use the Startup() event to check if you have a special "silent" switch.  If yes, run your code and set "e.Cancel" to True which causes the application to quit without showing a form:
http://msdn.microsoft.com/en-us/library/w3xx6ewx(VS.80).aspx

Example:
Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If e.CommandLine(0).ToLower = "/silent" Then

                ' ... run your code from here ...

                e.Cancel = True ' Suppress the default Form and Exit the Application
            End If

            ' Otherwise the App starts as Normal with the Form specified as the "Startup object" in Project --> Properties!

        End Sub

    End Class


End Namespace

Open in new window


As suggested, break out the code in the Button handler to a its own method, such as in a Module where it can be accessed from both the Button and the Startup() event.
0
 

Author Comment

by:dimensionav
ID: 36585082
JamesBurger:
After launching this application for the first time, the task will be performed at stablished intervals, but when and how will be stopped ?

Idle_Mind:
I have to create a console application to perform what you suggested , right?

Thanks for your patience on this guys!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36585092
No sir...it could all be done with a WinForms app.
0
 

Author Comment

by:dimensionav
ID: 36585172
I finally uderstood you code ( I´m not that skilled with windows forms), I have a final couple of questions:

1. Where should I declareThe namespace my ?
2. I have created a new module called MainCore, If I understand correctly, I have to create a new sub and this should be related to the Event button handler, right?

Thanks again!
0
 

Author Comment

by:dimensionav
ID: 36585220
For the question 1, I now how to do it (VIEW APPLICATION EVENTS)
0
 
LVL 40
ID: 36587300
Stopping the application through the code, simply call Application.Exit(). Could be set for a certain number of intervals, a certain amount of time.

If you want the user to manually close the application, it can be done in the Task Manager of Windows (Ctrl-Alt-Del). It won't show in the applications, because it has no visible form, but it should show in the Processes tab. Better, you could put a NotifyIcon in the system tray of Windows.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now