[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1013
  • Last Modified:

Run VB.NET App in the System Tray, Executing at 6am, every day

I have an application that interfaces with QuickBooks and runs some reports based on sales.  The program takes about an hour to run, so I'm hoping to get it to run at 6am each day, so the user can come in, review the report and do what they need to.

In addition, I'd like the program to run in the background.  I don't want the user to accidently close the program, so I figured, the systray would be the best spot...  but i've never done that before.

So...  2 things :)  1.  How to I get the program to run at 6am every day?  2.  How do you run VB.NET program in the system tray?

If you can point me in the right direction, I'd appreciate it.  Thanks!

0
slightlyoff
Asked:
slightlyoff
  • 4
  • 4
  • 3
  • +3
3 Solutions
 
Todd GerbertIT ConsultantCommented:
I would configure your application to run without any forms displayed (perhaps a console application), and use the system's task scheduler to make it run daily.
0
 
robbert1979Commented:
You also can use a timer who checks every minute what the time is
I dont know how critical your application is and if it is a problem that the procedure starts instead of 6:00:00 am on 6:00:59 am ?

You can run it in the systemtray like this
put a sytemtrayicon on your form
set visible on true
hide your form

voila
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you really want an app that stays open all the time but only has an Icon in the System Tray then here is one way to do it.

This Project can be downloaded at:
http://dl.dropbox.com/u/5131616/TrayIcon%28Formless%29.zip

The code:
Module Module1

    '  *** How to make a FORMLESS WinForms Project that has a Tray Icon ***
    '  (1) Start with a standard WinForms project
    '  (2) Add a Module and a Public Sub Main to it (see below for example)
    '  (3) Click on Project --> Properties
    '  (4) UNCHEK the "Enable Application Framework" box
    '  (5) Change the "Startup object" to "Sub Main"
    '  (6) Optional: Delete the default Form1.vb from the Project if you don't need it
    '      Note that you CAN still use Forms in your project!  Just display them via code...
    '      (For example, you can use a Form to display a "Setup" or "About" Dialog.)
    '  (7) Change the Resources Tab
    '  (8) Click on the DROPDOWN arrow next to "Add Resource" and select "Add Existing File"
    '  (9) Navigate to your Icon File (.ico) and Double Click it
    ' (10) In the Properties Pane, change the "Persistence" Property to "Embedded"
    ' (11) Add the code below to Sub Main and Paste the "MyContext" Class below the Module

    Public Sub Main()
        Application.Run(New MyContext)
    End Sub

End Module

Public Class MyContext
    Inherits ApplicationContext

    Private Busy As Boolean = False
    Private RunTime As New TimeSpan(15, 34, 0) ' 6:00 am

    Private WithEvents Tray As New NotifyIcon
    Private WithEvents CMS As New ContextMenuStrip
    Private WithEvents mnuExit As New ToolStripMenuItem("Exit")
    Private WithEvents BGW As New System.ComponentModel.BackgroundWorker

    Public Sub New()
        ' This is the new "entry point" for you application
        ' Note that the app does NOT "die" after the constructor exits!
        ' (Explicitly call Application.Exit to shut the app down.)

        ' Icon (.ico) for Tray added via Project --> Properties --> Resources --> Add Existing File
        ' Change the "Persistence" Property of the Resource to "Embedded"
        Tray.Icon = My.Resources.Happy
        Tray.Visible = True

        CMS.Items.Add(mnuExit)
        Tray.ContextMenuStrip = CMS

        BGW.WorkerReportsProgress = True
        BGW.RunWorkerAsync()
    End Sub

    Private Sub BGW_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
        While True
            ' reset the "busy" flag
            Busy = False
            BGW.ReportProgress(0, "Sleeping")

            ' sleep until the next run time:
            System.Threading.Thread.Sleep((GetNextRun(RunTime) - DateTime.Now).TotalMilliseconds)

            ' toggle the "busy" flag
            Busy = True
            BGW.ReportProgress(0, "Working")

            ' perform the actual "work":
            GenerateReport()
        End While
    End Sub

    Private Sub GenerateReport()
        System.Threading.Thread.Sleep(15000) ' simulated 15 second work

        ' *** remove the line above and place your report generation code in here ***
    End Sub

    Private Sub BGW_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BGW.ProgressChanged
        Tray.Text = CStr(e.UserState) ' change the current tooltip on the tray

        ' you can also make a balloon appear over your TrayIcon:
        Tray.ShowBalloonTip(TimeSpan.FromSeconds(10).TotalMilliseconds, "Balloon Example", CStr(e.UserState), ToolTipIcon.Info)
    End Sub

    Private Function GetNextRun(ByVal time As TimeSpan) As DateTime
        Dim dt As DateTime = DateTime.Today.Add(time)
        If DateTime.Now > dt Then
            dt = dt.AddDays(1)
        End If

        ' Optional -- If you want to skip Saturday and Sunday:
        While dt.DayOfWeek = DayOfWeek.Saturday OrElse dt.DayOfWeek = DayOfWeek.Sunday
            dt = dt.AddDays(1)
        End While

        Return dt
    End Function

    Private Sub CMS_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles CMS.Opening
        e.Cancel = Me.Busy ' keep user from seeing the exit menu while we are "working"
    End Sub

    Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        If Not Busy Then ' just in case the menu was open BEFORE we started and the user clicks on exit AFTER we begin processing
            Application.Exit() ' <-- this is how to close your application!
        Else
            Tray.ShowBalloonTip(TimeSpan.FromSeconds(10).TotalMilliseconds, "I'm Busy!", "Try again later please...", ToolTipIcon.Info)
        End If
    End Sub

    Private Sub MyContext_ThreadExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ThreadExit
        Tray.Visible = False
    End Sub

End Class

Open in new window

0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*I was testing with different times so this line (#28):

    Private RunTime As New TimeSpan(15, 34, 0) ' 6:00 am

Should be changed to:

    Private RunTime As New TimeSpan(0, 6, 0) ' 6:00 am
0
 
samsymonCommented:
you can use NotifyIcon in vb.net.
a contextmenustrip can be used for the visibility and hiding of forms.
initially make the form hidden.
then you can do that.

eg.

        private void Form2_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }


http://support.microsoft.com/kb/903898
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
 
angus_young_acdcCommented:
Not sure why it's not been suggested, but if you just want it to be in the system tray and not open to the user why not create a service?  You can use a timer / background worker to check the time, and it would run without any user intervention - and they wouldn't be able to close it.

Granted, they could stop the service, but if you ran it under a special user account and restricted access to services.msc then they couldnt.
0
 
slightlyoffAuthor Commented:
I should've been more clear, but wasn't sure on how to describe what i was hoping for.

If you look at AVG (or pretty much any anti-virus software), it runs in the system tray, but the user can click it and see the program.  

My program generates a report (hopefully automatically), so the user will need to open up the program to view the report.  The report takes a long time to run (about an hour), so I'd like it to be hidden until it's needed...  so I'd like the same concept applied to my program.

I could also do a system schedule for this program, but I'm eventually hoping to have it maintain inventory levels in real time - and i'd rather not schedule it every 20 or 30 minutes.

I think Idle_Mind is on the right track...I have to look the example to see if I can apply it to this project.

I really appreciate the help!!!
0
 
Todd GerbertIT ConsultantCommented:
@slightlyoff

What if the user logs off his computer, or Windows Updates causes it to reboot automatically overnight? Both highly likely (in my environment, anyway)...
0
 
slightlyoffAuthor Commented:
In this environment, the user has agreed not to logoff his computer, so that shouldn't be an issue (unless he forgets).  he doesn't shut it down since he logs in remotley from time to time.  The windows update issue might be a problem, from time to time it will shut down these computers.

0
 
Todd GerbertIT ConsultantCommented:
It may not be enough of an issue in your case to worry about such circumstances, just something to consider!

0
 
angus_young_acdcCommented:
Ahh, well that makes more sense to me.  Certainly an idea to create an app in the system tray.

For the possibility of a shut down, you have could implement that and a service, which you can set to automatically start upon startup should it happen.  Of course, you can set a registry key to do the exact same with an application.  I only mention it because you could run the service on a machine that you control and know wont turn off (you can disable the win updates), and allow the service to process your data and the front end simply read that data from a datasource (SQL, Access, flat file, etc).
0
 
slightlyoffAuthor Commented:
Yeah, I appreciate it!  This programming task has grown in scope since I started it, it may not always reside on a user's machine.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
With the ApplicationContext approach you can still give a full blown GUI to the user when desired.  Simply display a form in response to one of the menu events.  It's nice since the app doesn't automatically exit when the form is closed.  You have to explicitly call Application.Exit from somewhere in code to make it shutdown.

Definitely lots of ways to go about this though!  =)
0
 
slightlyoffAuthor Commented:
Thanks for your help :)  I'm still working on it, but I've got the info I need.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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