[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

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

Interfaceless Application - How to keep it running

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
Edward Stevens
Asked:
Edward Stevens
  • 3
  • 2
1 Solution
 
Ravi SinghSenior Software EngineerCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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
 
Edward StevensIT ConsultantAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Edward StevensIT ConsultantAuthor Commented:
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
 
Ravi SinghSenior Software EngineerCommented:
ok no probs! Thanks
0
 
Edward StevensIT ConsultantAuthor Commented:
Idle,

Worked perfectly.  Thanks for the help.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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