Solved

Interfaceless Application - How to keep it running

Posted on 2006-06-16
6
359 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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