Solved

Interfaceless Application - How to keep it running

Posted on 2006-06-16
6
356 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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