Solved

Interfaceless Application - How to keep it running

Posted on 2006-06-16
6
362 Views
Last Modified: 2008-02-26
My past experience has always dealt with applications that have some sort of UI.  Hence, the application would run as long as the main form was left open.

I am now building an application that requires no UI.  It will run as a background application with a NotifyIcon to allow the user to end it, if necessary.

My question:  What is the technique used to keep a UI -less application running?  I currently have a module that has a Sub Main() which, at the moment, runs through some logic and then ends the application.  The application is supposed to poll a dBase 5.0 table for new records and perform some task when the appropriate records are added to the table.

I thought of using a loop, but don't want to consume 100% of the CPU while this is running.  I thought of using a timer object, but wouldn't that go away if the Main() sub ended?

How can I get a UI-less application to remain in execution, polling the table every so often without consuming the majority of the CPU cycles?

Thanks
0
Comment
Question by:Edward Stevens
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16919681
Hi, have you come across Windows Services? They're typically run in the background without any user interface, check out for more info + tutorial:

http://www.developerfusion.co.uk/show/3441/ 

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 16920174
"It will run as a background application with a NotifyIcon to allow the user to end it, if necessary."

Inherit from the ApplicationContext() class...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsapplicationcontextclasstopic.asp

    "You can use the ApplicationContext class to redefine the circumstances that cause a message loop to exit."

Here is a complete example in VB.Net 2003:

(note the instructions in code on how to embed the Icon for your NotifyIcon)

Module Module1

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

    Public Class MyContext
        Inherits ApplicationContext

        Private ni As NotifyIcon
        Private ICO As Icon
        Private WithEvents mi As MenuItem
        Private WithEvents tmr As New System.Windows.Forms.Timer

        Public Sub New()
            ' build a NotifyIcon and display it
            ' The Icon is an embedded resource:
            ' Project --> Add Existing Item... --> Select your .ICO file
            ' In the Solution Explorer, select your .ICO file
            ' Down below in the Properites Pane,
            ' change the Build Action from "Content" to "Embedded Resource"
            ni = New NotifyIcon
            ICO = New Icon(Me.GetType, "unfazed.ico") ' the .ICO filename IS CASE SENSITIVE
            ni.Icon = ICO
            ni.Visible = True

            ' make a context menu and assign it to our NotifyIcon
            Dim cm As New ContextMenu
            mi = cm.MenuItems.Add("Close Application")
            ni.ContextMenu = cm

            ' the polling loop
            tmr.Interval = 5000 ' (5 seconds) adjust to your liking...
            tmr.Start()
        End Sub

        Private Sub mi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mi.Click
            ni.Visible = False
            ni.Icon = Nothing
            ICO.Dispose()

            Me.ExitThread()
        End Sub

        Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
            tmr.Stop()

            ' do something in here...
            MessageBox.Show("Tick", "Polling...", MessageBoxButtons.OK, MessageBoxIcon.Information)

            tmr.Start()
        End Sub

    End Class

End Module
0
 
LVL 1

Author Comment

by:Edward Stevens
ID: 16920332
Idle,

If I understand the concept correctly, MyContext runs as a new, separate thread.  This is just what I'm looking for.  I'll test it tonight.

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Edward Stevens
ID: 16920345
Didn't want to leave Zephyr out.  Thanks for your suggestion.  It isn't what I'm looking for in this project but the info will be usefull for other projects.
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16920375
ok no probs! Thanks
0
 
LVL 1

Author Comment

by:Edward Stevens
ID: 16927406
Idle,

Worked perfectly.  Thanks for the help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate number of nights between two dates 5 86
SSRS 2016 Rendering HTML tables 3 56
Code enhancement 4 32
"lblTime is not declared" 3 28
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

713 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