Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. windows not in focus

windows not in focus

Scheduled Pinned Locked Moved C#
questiontutorial
32 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W wjp_auhtm

    Oh! I try to open a win app from a windows service, but I was failed. I consider that creating a socket connect between the windows service and the win app may be practicable. creating windows service like this. creating socket like this.

    M Offline
    M Offline
    michaelgr1
    wrote on last edited by
    #23

    OK. But i have another question- If i create a windows service to manage the data from the barcode reader (the data will go to clipboard and then set focus to my application), How can i install this windows service on a user computer (without installutil etc.)?

    W 1 Reply Last reply
    0
    • M michaelgr1

      OK. But i have another question- If i create a windows service to manage the data from the barcode reader (the data will go to clipboard and then set focus to my application), How can i install this windows service on a user computer (without installutil etc.)?

      W Offline
      W Offline
      wjp_auhtm
      wrote on last edited by
      #24

      You could install the windows service like this, if u are using .Net FrameWork 3.5. I found that the file named "System.Configuration.Install.dll" in .Net 2.0 is different from the .Net 3.5's. There are some simple code.

      using System.ServiceProcess;
      using System.Configuration.Install;
      using System.Collections;

              /// <summary>
              /// TODO: Is the service is existed and return true or false.
              /// </summary>
              /// <param name=" NameService ">Name of the windows service.</param>
              /// <returns>If the service is existed then return true,or return false.</returns>
              private bool isServiceIsExisted(string NameService) 
              {
                  ServiceController\[\] services = ServiceController.GetServices();
                  foreach (ServiceController s in services) 
                  {
                      if (s.ServiceName.ToLower() == NameService.ToLower()) 
                      {
                          return true;
                      }
                  }
                  return false;
              }
      
              /// <summary>
              /// TODO: Install windows service.
              /// </summary>
              /// <param name="stateSaver">Collection(default is Null)</param>
              /// <param name="filepath">Full path of the windows service file</param>
              private void InstallmyService(IDictionary stateSaver, string filepath)
              {
                  AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
                  AssemblyInstaller1.UseNewContext = true;
                  AssemblyInstaller1.Path = filepath;
                  AssemblyInstaller1.Install(stateSaver);
                  AssemblyInstaller1.Commit(stateSaver);
                  AssemblyInstaller1.Dispose();
              }
              /// <summary>
              /// TODO: Uninstall windows service.
              /// </summary>
              /// <param name="filepath">Full path of the windows service file</param>
              private void UnInstallmyService(string filepath)
              {
                  AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
                  AssemblyInstaller1.UseNewContext = true;
                  AssemblyInstaller1.Path = filepath;
                  AssemblyInstaller1.Uninstall(null);
                  AssemblyInstaller1.Dispose();
              }
      

      Good Luck!

      M 1 Reply Last reply
      0
      • W wjp_auhtm

        You could install the windows service like this, if u are using .Net FrameWork 3.5. I found that the file named "System.Configuration.Install.dll" in .Net 2.0 is different from the .Net 3.5's. There are some simple code.

        using System.ServiceProcess;
        using System.Configuration.Install;
        using System.Collections;

                /// <summary>
                /// TODO: Is the service is existed and return true or false.
                /// </summary>
                /// <param name=" NameService ">Name of the windows service.</param>
                /// <returns>If the service is existed then return true,or return false.</returns>
                private bool isServiceIsExisted(string NameService) 
                {
                    ServiceController\[\] services = ServiceController.GetServices();
                    foreach (ServiceController s in services) 
                    {
                        if (s.ServiceName.ToLower() == NameService.ToLower()) 
                        {
                            return true;
                        }
                    }
                    return false;
                }
        
                /// <summary>
                /// TODO: Install windows service.
                /// </summary>
                /// <param name="stateSaver">Collection(default is Null)</param>
                /// <param name="filepath">Full path of the windows service file</param>
                private void InstallmyService(IDictionary stateSaver, string filepath)
                {
                    AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
                    AssemblyInstaller1.UseNewContext = true;
                    AssemblyInstaller1.Path = filepath;
                    AssemblyInstaller1.Install(stateSaver);
                    AssemblyInstaller1.Commit(stateSaver);
                    AssemblyInstaller1.Dispose();
                }
                /// <summary>
                /// TODO: Uninstall windows service.
                /// </summary>
                /// <param name="filepath">Full path of the windows service file</param>
                private void UnInstallmyService(string filepath)
                {
                    AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
                    AssemblyInstaller1.UseNewContext = true;
                    AssemblyInstaller1.Path = filepath;
                    AssemblyInstaller1.Uninstall(null);
                    AssemblyInstaller1.Dispose();
                }
        

        Good Luck!

        M Offline
        M Offline
        michaelgr1
        wrote on last edited by
        #25

        Will this code start the service too?

        W 1 Reply Last reply
        0
        • M michaelgr1

          Will this code start the service too?

          W Offline
          W Offline
          wjp_auhtm
          wrote on last edited by
          #26

          If set the windows service autorun, when you restart computer, the service will start. Of course, you can start the service in manual like this.

                  /// <summary>
                  /// TODO: Start windows service.
                  /// </summary>
                  /// <param name="name">The name of windows service.</param>
                  /// <returns>If success return true,or return false.;</returns>
                  private bool StarmyService(string name)
                  {
                      ServiceController sc = new ServiceController(name);
                      if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending
                              )
                      {
                          sc.Start();
                          sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
                      }
                      else
                      {
                      }
                      sc.Close();
                      return true;
                  }
          

          And stop the service like this

                  /// <summary>
                  /// TODO: Stop windows service.
                  /// </summary>
                  /// <param name="name">The name of windows service.</param>
                  /// <returns>If success return true,or return false.;</returns>
                  private bool StopmyService(string name)
                  {
                      ServiceController sc = new ServiceController(name);
                      if (sc.Status == ServiceControllerStatus.Running ||
                          sc.Status == ServiceControllerStatus.StartPending)
                      {
                          sc.Stop();
                          sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
                      }
                      else
                      {
                      }
                      sc.Close();
                      return true;
                  }
          
          M 1 Reply Last reply
          0
          • W wjp_auhtm

            If set the windows service autorun, when you restart computer, the service will start. Of course, you can start the service in manual like this.

                    /// <summary>
                    /// TODO: Start windows service.
                    /// </summary>
                    /// <param name="name">The name of windows service.</param>
                    /// <returns>If success return true,or return false.;</returns>
                    private bool StarmyService(string name)
                    {
                        ServiceController sc = new ServiceController(name);
                        if (sc.Status == ServiceControllerStatus.Stopped || sc.Status == ServiceControllerStatus.StopPending
                                )
                        {
                            sc.Start();
                            sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10));
                        }
                        else
                        {
                        }
                        sc.Close();
                        return true;
                    }
            

            And stop the service like this

                    /// <summary>
                    /// TODO: Stop windows service.
                    /// </summary>
                    /// <param name="name">The name of windows service.</param>
                    /// <returns>If success return true,or return false.;</returns>
                    private bool StopmyService(string name)
                    {
                        ServiceController sc = new ServiceController(name);
                        if (sc.Status == ServiceControllerStatus.Running ||
                            sc.Status == ServiceControllerStatus.StartPending)
                        {
                            sc.Stop();
                            sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
                        }
                        else
                        {
                        }
                        sc.Close();
                        return true;
                    }
            
            M Offline
            M Offline
            michaelgr1
            wrote on last edited by
            #27

            OK and how can i set the service to start automatically ? i mean in the service code. Also how can i check if the service is already running?

            W 1 Reply Last reply
            0
            • M michaelgr1

              OK and how can i set the service to start automatically ? i mean in the service code. Also how can i check if the service is already running?

              W Offline
              W Offline
              wjp_auhtm
              wrote on last edited by
              #28

              OK, the simple code to check if the service is running like this.

                      /// <summary>
                      /// TODO: Is the windows service running.
                      /// </summary>
                      /// <param name="name">The name of windows service.</param>
                      private bool IsRunning(string name)
                      {
                          bool IsRun = false;
                          if (!isServiceIsExisted(name)) // this function was named previously.
                          {
                              return false;
                          }
                          ServiceController sc = new ServiceController(name);
                          if (sc.Status == ServiceControllerStatus.StartPending ||
                              sc.Status == ServiceControllerStatus.Running)
                          {
                              IsRun = true;
                          }
                          sc.Close();
              
                          return IsRun;
                      }
              

              Set the service to start automatically: Change the value of property named "StartType" of the windows service to "Automatic". BTW, owing to the limitation of my knowledge, the code are not very strong.I suggest that you can research them by yourself. I think that after you reserach them, you will learn more.

              M 1 Reply Last reply
              0
              • W wjp_auhtm

                OK, the simple code to check if the service is running like this.

                        /// <summary>
                        /// TODO: Is the windows service running.
                        /// </summary>
                        /// <param name="name">The name of windows service.</param>
                        private bool IsRunning(string name)
                        {
                            bool IsRun = false;
                            if (!isServiceIsExisted(name)) // this function was named previously.
                            {
                                return false;
                            }
                            ServiceController sc = new ServiceController(name);
                            if (sc.Status == ServiceControllerStatus.StartPending ||
                                sc.Status == ServiceControllerStatus.Running)
                            {
                                IsRun = true;
                            }
                            sc.Close();
                
                            return IsRun;
                        }
                

                Set the service to start automatically: Change the value of property named "StartType" of the windows service to "Automatic". BTW, owing to the limitation of my knowledge, the code are not very strong.I suggest that you can research them by yourself. I think that after you reserach them, you will learn more.

                M Offline
                M Offline
                michaelgr1
                wrote on last edited by
                #29

                OK , and how can i set the service to start automaticallu ? i mean programatically? also how do i check if the service is already running? or it's not matter?

                W 1 Reply Last reply
                0
                • M michaelgr1

                  OK , and how can i set the service to start automaticallu ? i mean programatically? also how do i check if the service is already running? or it's not matter?

                  W Offline
                  W Offline
                  wjp_auhtm
                  wrote on last edited by
                  #30

                  The function "IsRunning" will check if the windows service is running. I think that when creating a windows service just like creating a application, so I do not know how to create it programatically. We must set the windows service before we release it.

                  1 Reply Last reply
                  0
                  • W wjp_auhtm

                    Oh! I try to open a win app from a windows service, but I was failed. I consider that creating a socket connect between the windows service and the win app may be practicable. creating windows service like this. creating socket like this.

                    H Offline
                    H Offline
                    Heinzzy
                    wrote on last edited by
                    #31

                    I feel that using a socet connection between service and application is not a good idea because this connection can be terminated by firewall etc ps use logging instead

                    W 1 Reply Last reply
                    0
                    • H Heinzzy

                      I feel that using a socet connection between service and application is not a good idea because this connection can be terminated by firewall etc ps use logging instead

                      W Offline
                      W Offline
                      wjp_auhtm
                      wrote on last edited by
                      #32

                      I.C. But, just remind once. I know little about the Logging, I will have some research about that.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups