Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to call a method of windows form in windows service.. i use following code

What I have tried:

Form1 frm = new ScheduleService.Form1();
        public Service1()
        {
            InitializeComponent();
        }
        Process p = new Process();
        protected override void OnStart(string[] args)
        {
            WriteToFile("Service is started at " + DateTime.Now);//used for just checking
            time.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            time.Interval = 8000;
            time.Start();
            time.Enabled = true;
        }
        public void WriteToFile(string Message)
        {
            string path = "D:\\ErrorLogs";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string filepath = "D:\\ErrorLogs\\Log_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
            if (!File.Exists(filepath))
            {
                // Create a file to write to.   
                using (StreamWriter sw = File.CreateText(filepath))
                {
                    sw.WriteLine(Message);
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(filepath))
                {
                    sw.WriteLine(Message);
                }
            }
        }
        private void OnElapsedTime(object sender, ElapsedEventArgs e)
        {
           
            try
            {
                frm.notifyIcon1.ShowBalloonTip(5);
                frm.ReadXML();
                WriteToFile("Service is Running at " + DateTime.Now);
            }
            catch
            {
                WriteToFile("Service is catch error at " + DateTime.Now);
            }
            
        }



service is working..but method i call is not working
Posted
Updated 1-Jun-20 23:49pm

I don't believe Windows supports creating and displays windows from services. Services are intended to be background processes with no UI interaction, and any windows you do create won't be displayed on the screen (in fact, I believe Windows uses a separate session to host any UI generated from services to prevent them interfering with the current user).

Instead, you should look at having your form opened separately (and therefore hosted on the current session) and use cross-process communication platforms like WCF, named pipes or even sockets to transmit data. You could also consider writing data to a file on the file system (implementing appropriate file-locking) and have the form read this file to process any UI operations.

WCF[^]
Named Pipes[^]
 
Share this answer
 
You can call methods in any EXE or DLL from a service just by referencing the EXE or DLL as usual, but ... they can't interact with teh user in any way, shape, or form: No keyboard, no mouse, and no display at all.

So you can't create a Form instance and expect the user to see it, although you can call methods within the Form class.

If you want to interact with the user, you need a separate app running under that user account that communicates with the service, and display info as needed - the service can't start the app!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900