locked
Run Application In BackGround on windows 8 RRS feed

  • Question

  • Actually I am developing an application to keep records of an event like anniversary, birthday, meeting etc. I want to run this app in background and when the event date time matches this app should give a notification on the desktop automatically. means this should run in background and give notification on the wall. so please help me. how to do it using vb.net
    Sunday, March 23, 2014 10:56 AM

Answers

  • To make an application to run in Background, you need the application to run in a context that inherits from  ApplicationContext

    -------------

    Here are the steps to create this

    1- Start a new project as a window Form application

    2- Add a class and make this class to inherits from ApplicationContext

    3- in your class, add a Protected Shared Sub Main

         It should look like this at this point

    Friend Class BGApplication : Inherits ApplicationContext Protected Shared Sub main() Application.Run(New BGApplication) End Sub End Class

    4- go to the project properties, in the "Application" tab and uncheck the check box "Enable Application Framework"

    5- In the same tab, expand "Startup Object" and click your Application context class name ( From the above example, "BGApplication"

    6- Now go to the Solution Explorer and delete Form1.Vb

    At this point you have created all the context for the application to run in background.

    -------------------------

    You are not saying what your application does, if it runs on a timer, just add a timer in your class BGApplication

    -------------------------

    Sometime we like to create some background application to do something in response of hot keys.

    To react on hotkeys, an application should be able to access its message loop. The class ApplicationContext does not provide any way to do it. However, it is possible to do it indirectly by using a control as loop accessor.

    The idea is to create a class that inherits from Control and access the application message loop thru this control.

    Here an example. this application runs in background. When CTRL-A is pressed, the application starts  IExplorer  and navigate to this forum. When CTRL-Q is pressed, the application exits.

    Friend Class BGApplication : Inherits ApplicationContext
    
        Protected Shared Sub main()
            Application.Run(New BGApplication)
        End Sub
    
    
        Private WithEvents pumpAccessor As MessagePumpAccessor
    
        Sub New()
            pumpAccessor = New MessagePumpAccessor
            Native.RegisterHotKey(pumpAccessor.Handle, 1, Native.MOD_CONTROL, Keys.A)
            Native.RegisterHotKey(pumpAccessor.Handle, 2, Native.MOD_CONTROL, Keys.Q)
        End Sub
    
        Private Sub Ctrl_A_Pressed(Sender As Object, e As EventArgs) Handles pumpAccessor.Ctrl_A_Pressed
            Process.Start("iexplore.exe", "http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral")
        End Sub
    
        Private Sub Ctrl_Q_Pressed(Sender As Object, e As EventArgs) Handles pumpAccessor.Ctrl_Q_Pressed
            Native.UnregisterHotKey(pumpAccessor.Handle, 1)
            Native.UnregisterHotKey(pumpAccessor.Handle, 2)
            Me.ExitThread()
        End Sub
    End Class
    
    
    
    Friend Class MessagePumpAccessor : Inherits Control
    
        Public Event Ctrl_A_Pressed As EventHandler
        Public Event Ctrl_Q_Pressed As EventHandler
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = Native.WM_HOTKEY Then
                If m.WParam = CType(1, IntPtr) Then
                    RaiseEvent Ctrl_A_Pressed(Me, EventArgs.Empty)
                ElseIf m.WParam = CType(2, IntPtr) Then
                    RaiseEvent Ctrl_Q_Pressed(Me, EventArgs.Empty)
                End If
    
            End If
            MyBase.WndProc(m)
        End Sub
    End Class
    
    
    
    Public Class Native
        Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    
        Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
    
        Public Const WM_HOTKEY As Integer = &H312
    
        Public Const MOD_CONTROL = &H2
    End Class


    Sunday, March 23, 2014 12:37 PM