Question

Here is what I am trying to do is create a service that checks if Microsoft Lync is running. If it's running then do nothing but write to eventlog. If it's not running run the exe and then log in to Lync. The problem I am getting is when it comes time to run the exe, it would go and start the process but it never actually runs the app. I tried to see if would work with notepad but all it did was create the process in task manager but never opened the actual app.

  Imports System
  Imports System.Data
  Imports System.Timers
  Imports System.Diagnostics
  Imports System.Data.SqlClient
  Imports System.ServiceProcess
  Imports System.Windows.Forms

    Public Class Service1

    Protected Overrides Sub OnStart(ByVal args() As String)
        EventLog.WriteEntry("In Onstart", "starting timer")
           Timer1.Start()
    End Sub



   Protected Overrides Sub OnStop()
   End Sub

   Private Sub Timer1_Elapsed(ByVal sender As System.Object, 
   ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

       If IsProcessRunning("communicator") Then
       EventLog.WriteEntry("no problem")
       Else
       EventLog.WriteEntry("not running")
       Dim info As New ProcessStartInfo("C:\Program Files (x86)\Microsoft Lync\communicator.exe")
       info.UseShellExecute = False
       info.RedirectStandardError = True
       info.RedirectStandardInput = True
       info.RedirectStandardOutput = True
       info.CreateNoWindow = True
       info.ErrorDialog = False
       info.WindowStyle = ProcessWindowStyle.Hidden

     Dim process__1 As Process = Process.Start(info)

     End If

   End Sub

   Public Function IsProcessRunning(ByVal name As String) As Boolean
      For Each clsProcess As Process In Process.GetProcesses()
        If clsProcess.ProcessName.StartsWith(name) Then
         Return True
      End If
      Next
         Return False
   End Function
End Class
Was it helpful?

Solution

Problem comes from the fact that in Windows (and specially since versions 6.x) services run in a completely isolated session and desktop, without any chance of user interaction, which is by design and discouraged to do so, for security reasons. The program you're launching actually does starts, but it does so in that hidden desktop (same for notepad) where no users can ever see it.

The quick and dirty workaround is to mark the service as interactive in the control panel, and start the interactive services detection service. When doing so, when your service runs the program, a window will flash in the taskbar, telling there is a message from a service, so that you can switch to that parallel desktop and actually see it. That's simply very inconvenient to the user and widely considered a BAD PRACTICE.

The real solution is to make the program a regular application and not a service, and run it though some autostart location in Windows for every user. It does not need to have visible UI, but run in the same context as you do. Or to leave the service, but also put some application in user space that communicates with the service just for the sake of running the second program. In any case, the general rule is to never have any kind of user interaction from a service process.

Here is an article that explains the problems and some workarounds http://blogs.technet.com/b/askperf/archive/2007/04/27/application-compatibility-session-0-isolation.aspx

OTHER TIPS

I was able to create a program that detects whether or not Lync is running and if it's not restart the program.

Imports System.Diagnostics
Imports Microsoft.Lync
Imports Microsoft.Lync.Model
Imports Microsoft.Lync.Model.Conversation
Imports Microsoft.Lync.Model.Group

Public Class frmCheckLync
Private _lyncClient As LyncClient
Private _contactManager As ContactManager
Private _conversationManager As ConversationManager
Private _self As Self
Private _groups As Dictionary(Of String, Group)
Private _contactSubscriptions As Dictionary(Of String, ContactSubscription)
Public Event StateChanged As EventHandler(Of ClientStateChangedEventArgs)
Public _signIn As IAsyncResult
Public asyncState As Object() = {_lyncClient}
Public clsProcess As Process


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try

        Timer1.Enabled = True

        AddHandler Timer1.Elapsed, AddressOf Timer1_Tick

        Timer1.Interval = 600

        Timer1.Start()

    Catch ex As Exception

        Diagnostics.EventLog.WriteEntry("This is a test" + ex.Message.ToString, "This is a test")

    End Try
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)

    Try

        Timer1.Stop()

        GetClient()


        Try
            CheckClient()
        Catch ex As Exception

        End Try



    Catch ex As Exception




    Finally

        Timer1.Start()

    End Try

End Sub

Public Function IsProcessRunning(ByVal name As String) As Boolean
    For Each Me.clsProcess In Process.GetProcesses()

        If Me.clsProcess.ProcessName.StartsWith(name) Then
            Return True
        End If
    Next
    Return False
End Function

Public Sub GetClient()
    If IsProcessRunning("communicator") = False Then

        Try
            Dim info As New ProcessStartInfo("C:\Program Files\Microsoft Lync\communicator.exe")
            info.UseShellExecute = False
            info.RedirectStandardError = True
            info.RedirectStandardInput = True
            info.RedirectStandardOutput = True
            info.CreateNoWindow = True
            info.ErrorDialog = False
            info.WindowStyle = ProcessWindowStyle.Normal

            Dim process__1 As Process = Process.Start(info)
        Catch ex1 As Exception

        End Try
        Try
            Dim info1 As New ProcessStartInfo("C:\Program Files (x86)\Microsoft Lync\communicator.exe")
            info1.UseShellExecute = False
            info1.RedirectStandardError = True
            info1.RedirectStandardInput = True
            info1.RedirectStandardOutput = True
            info1.CreateNoWindow = True
            info1.ErrorDialog = False
            info1.WindowStyle = ProcessWindowStyle.Normal

            Dim process__2 As Process = Process.Start(info1)
        Catch ex2 As Exception

        End Try
    End If
End Sub
Public Sub CheckClient()
    Try
        _lyncClient = LyncClient.GetClient()
    Catch
        GetClient()
    End Try


    If _lyncClient.State = ClientState.SignedOut Then
        _signIn = _lyncClient.BeginSignIn(Nothing, Nothing, Nothing,
                                          Function(result)
                                              If result.IsCompleted Then
                                                  _lyncClient.EndSignIn(result)
                                                  ' Setup application logic

                                                  ' could not sign in 
                                              Else
                                              End If

                                          End Function, TryCast("Local user signing in", Object))

    End If

End Sub



End Class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top