locked
Windows Application in Windows Service RRS feed

  • Question

  • Hello..

    i have a windows application completed and now i want to add a windows service to the existing application. and i want to call a method of windows application from windows service. i use below code.. plz help

     public partial class Service1 : ServiceBase
        {
            Timer time = new Timer();
            string filename = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\BackupDetails.xml";
            DBinfo1 dbinfo = new DBinfo1();
            public Service1()
            {
                InitializeComponent();
            }
            Process p = new Process();
            protected override void OnStart(string[] args)
            {
                time.Elapsed += new ElapsedEventHandler(OnElapsedTime);
                time.Interval = 1000 * 60;
                time.Start();
                time.Enabled = true;
            }
    
            private void OnElapsedTime(object sender, ElapsedEventArgs e)
            {
                p.StartInfo.FileName = @"C:\Users\Mousmi Muthalib\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                p.Start();
            }
    
            
            protected override void OnStop()
            {
                p.Kill();
            }


    Mrs Nishad

    Monday, June 1, 2020 11:08 AM

Answers

  • Hello..

    i have a windows application completed and now i want to add a windows service to the existing application. and i want to call a method of windows application from windows service. i use below code.. plz help

     public partial class Service1 : ServiceBase
        {
            Timer time = new Timer();
            string filename = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\BackupDetails.xml";
            DBinfo1 dbinfo = new DBinfo1();
            public Service1()
            {
                InitializeComponent();
            }
            Process p = new Process();
            protected override void OnStart(string[] args)
            {
                time.Elapsed += new ElapsedEventHandler(OnElapsedTime);
                time.Interval = 1000 * 60;
                time.Start();
                time.Enabled = true;
            }
    
            private void OnElapsedTime(object sender, ElapsedEventArgs e)
            {
                p.StartInfo.FileName = @"C:\Users\Mousmi Muthalib\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                p.Start();
            }
    
            
            protected override void OnStop()
            {
                p.Kill();
            }


    Mrs Nishad

    Hello,

    your source code looks like C#, so ask in a C# forum: https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

    Regards, Guido

    Tuesday, June 2, 2020 6:49 AM

All replies

  • Hello,

    See the following, search for 64

    https://stackoverflow.com/questions/5307968/how-can-i-run-an-exe-program-from-a-windows-service-using-c


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, June 1, 2020 1:06 PM
  • You can't make direct contact with a Windows desktop program from a Windows service in the manner you may be trying to do in executing a method on the running desktop program that I know about.

    There are ways to do it like using WCF.

    Just what is it that you are trying to do? 

    Monday, June 1, 2020 2:28 PM
  • Hello..

    i have a windows application completed and now i want to add a windows service to the existing application. and i want to call a method of windows application from windows service. i use below code.. plz help

     public partial class Service1 : ServiceBase
        {
            Timer time = new Timer();
            string filename = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\BackupDetails.xml";
            DBinfo1 dbinfo = new DBinfo1();
            public Service1()
            {
                InitializeComponent();
            }
            Process p = new Process();
            protected override void OnStart(string[] args)
            {
                time.Elapsed += new ElapsedEventHandler(OnElapsedTime);
                time.Interval = 1000 * 60;
                time.Start();
                time.Enabled = true;
            }
    
            private void OnElapsedTime(object sender, ElapsedEventArgs e)
            {
                p.StartInfo.FileName = @"C:\Users\Mousmi Muthalib\Documents\Visual Studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
                p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                p.Start();
            }
    
            
            protected override void OnStop()
            {
                p.Kill();
            }


    Mrs Nishad

    Hello,

    your source code looks like C#, so ask in a C# forum: https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral

    Regards, Guido

    Tuesday, June 2, 2020 6:49 AM
  • i tried what i told above..its not working so i changed..

    now i create a windows service project named scheduler. and in that i add windows form . Now i want to call a function from windows form in windows service. 

    //this is my service Class  
      public partial class Service1 : ServiceBase
        {
            Timer time = new Timer();
            string filename = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\BackupDetails.xml";
            Form1 frm = new ScheduleService.Form1();
            public Service1()
            {
                InitializeComponent();
            }
            Process p = new Process();
            protected override void OnStart(string[] args)
            {
                time.Elapsed += new ElapsedEventHandler(OnElapsedTime);
                time.Interval = 1000 * 60;
                time.Start();
                time.Enabled = true;
            }
    
            private void OnElapsedTime(object sender, ElapsedEventArgs e)
            {
                frm.notifyIcon1.ShowBalloonTip(5);// i want to display notify icon too
                frm.ReadXML();
                
            }

    Mrs Nishad

    Tuesday, June 2, 2020 6:54 AM