Click here to Skip to main content
15,902,032 members
Home / Discussions / C#
   

C#

 
GeneralRe: Class Not Registered Error (HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Pin
Clifford Anup4-Mar-10 8:25
Clifford Anup4-Mar-10 8:25 
GeneralRe: Class Not Registered Error (HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)) Pin
Dave Kreskowiak4-Mar-10 9:05
mveDave Kreskowiak4-Mar-10 9:05 
QuestionC# Code example for adding File Attachments (when using Zetafax) Pin
But_Im_a_Lady4-Mar-10 3:35
But_Im_a_Lady4-Mar-10 3:35 
QuestionDetect if Application.Restart Fails. Pin
kevinnicol4-Mar-10 3:26
kevinnicol4-Mar-10 3:26 
AnswerRe: Detect if Application.Restart Fails. Pin
Alan N4-Mar-10 8:09
Alan N4-Mar-10 8:09 
GeneralRe: Detect if Application.Restart Fails. Pin
kevinnicol4-Mar-10 8:17
kevinnicol4-Mar-10 8:17 
GeneralRe: Detect if Application.Restart Fails. [modified] Pin
Alan N4-Mar-10 13:19
Alan N4-Mar-10 13:19 
QuestionMy service must run my form Pin
Andy_R4-Mar-10 2:37
Andy_R4-Mar-10 2:37 
Hi!
This code Service1. Service call notepad.exe (with my form the same). In task manager on tab process I see notepad.exe. But on tab application it is not.
using System;
using System.ServiceProcess;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using System.Collections;


namespace InteractiveService
{

    public class Service1 : System.ServiceProcess.ServiceBase
    {
 

        private System.Diagnostics.EventLog eventLog1;
         /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;


        public Service1()
        {
            // This call is required by the Windows.Forms Component Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitComponent call

            //if (!System.Diagnostics.EventLog.SourceExists("KepWareConnectMySQL"))
            //{
            //    System.Diagnostics.EventLog.CreateEventSource("KepWareConnectMySQL", "Application");
            //}

            //eventLog1.Source = "KepWareConnectMySQL";
            //eventLog1.Log = "Application";
        }

        // The main entry point for the process
        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;

            // More than one user Service may run within the same process. To add
            // another service to this process, change the following line to
            // create a second service object. For example,
            //
            //   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
            //
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

            System.ServiceProcess.ServiceBase.Run(ServicesToRun);

        }

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.eventLog1 = new System.Diagnostics.EventLog();
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
            // 
            // eventLog1
            // 
            this.eventLog1.Log = "Application";
            this.eventLog1.Source = "InteractiveService";
            // 
            // Service1
            // 
            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.ServiceName = "InteractiveService";
            //
             
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
            try
            {
                // TODO: Add code here to start your service.
                InteractiveService.MakeServiceInteractive();
                eventLog1.WriteEntry("Service Started Succesfully");

                //MessageBox.Show("Service Started Succesfully");
                startprc(@"C:\WINDOWS\system32\notepad.exe");
                //startprc(@"c:\SAP\FormKepWareAndMySQL.exe");
                eventLog1.WriteEntry("Started Process Succesfully");
               
            }
            catch (System.Exception ex)
            {
                eventLog1.WriteEntry(String.Format("Service OnStart ERROR :'" + ex.Message + "'"));
            }
        }

        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
            eventLog1.WriteEntry("Service Stoped Succesfully");
            Killprc("notepad");
            //Killprc("FormKepWareAndMySQL");
            
        }
        protected override void OnContinue()
        {
            eventLog1.WriteEntry("Service work started after pause succesfully");
        }
        protected override void OnPause()
        {
            eventLog1.WriteEntry("Service work paused succesfully");
        }
        private void startprc(string prcName)
        {
            try
            {
                //'Using whole path is required (you could use opendialog to find)
                eventLog1.WriteEntry(String.Format("startprc :'" + prcName + "'"));
                ShellLib.ShellExecute shellExecute = new ShellLib.ShellExecute();
                shellExecute.Path = prcName;
                shellExecute.Execute(); 
            }
            catch (System.Exception ex)
            {
                eventLog1.WriteEntry(String.Format("Service work start process ERROR :'" + ex.Message + "'"));
                //MsgBox(ex.Source & ": " & ex.Message)
            }
            //        Return 0
        }
        private void Killprc(string processName)
        {
            //'Prc is short for process...you could have called it anything
            foreach (Process p in System.Diagnostics.Process.GetProcessesByName(processName))
            {
                try
                {
                    p.Kill();
                    p.WaitForExit(); // possibly with a timeout
                    eventLog1.WriteEntry(String.Format("Process Kill Succesfully :'" + processName + "'"));
                }
                catch (InvalidOperationException invalidException)
                {
                    // process has already exited - might be able to let this one go
                    eventLog1.WriteEntry(String.Format("process has already exited - might be able to let this one go, InvalidOperationException :'" + invalidException + "'"));
                }
                catch (System.Exception ex)
                {
                    eventLog1.WriteEntry(String.Format("Service work start process, System.Exception :'" + ex.Message + "'"));
                }
            }
        }
    }    
}


In this case, Form call notepad.exe (with my form the same). In task manager on tab process I see notepad.exe. And on tab application I see notepad.exe too.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinTester2
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox grpLaunchApp;
		private System.Windows.Forms.Button btnExploreFolder;
		private System.Windows.Forms.Button btnFindInFolder;
		private System.Windows.Forms.Button btnEditBmp;
		private System.Windows.Forms.Button btnShowBmp;
		private System.Windows.Forms.Button btnOpenExe;
		private System.Windows.Forms.GroupBox grpFileOp;
		private System.Windows.Forms.Button btnCopy;
		private System.Windows.Forms.Button btnMove;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.Button btnClearList;
		private System.Windows.Forms.Button btnAddFile;
		private System.Windows.Forms.GroupBox grpRecentDocs;
		private System.Windows.Forms.GroupBox grpPrinter;
		private System.Windows.Forms.Button btnOpenPrinter;
		private System.Windows.Forms.Button btnShowProperties;
		private System.Windows.Forms.Button btnTestPage;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}


		[STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

		

		private void btnOpenExe_Click(object sender, System.EventArgs e)
		{
            ShellLib.ShellExecute shellExecute = new ShellLib.ShellExecute();
            //shellExecute.Path = @"c:\SAP\FormKepWareAndMySQL.exe";
            shellExecute.Path = @"C:\WINDOWS\system32\notepad.exe";
            shellExecute.Execute();
        }
      }    
}

I think that this is the reason that myForm is not working as it should be.
What can I run the myForm from the service, that it should appear on the tab application? Or how to make a different decision?
AnswerRe: My service must run my form Pin
Gonzoox4-Mar-10 3:35
Gonzoox4-Mar-10 3:35 
GeneralRe: My service must run my form Pin
Andy_R4-Mar-10 3:42
Andy_R4-Mar-10 3:42 
GeneralRe: My service must run my form Pin
Andy_R5-Mar-10 2:08
Andy_R5-Mar-10 2:08 
GeneralRe: My service must run my form Pin
Andy_R5-Mar-10 3:10
Andy_R5-Mar-10 3:10 
Questionparameter count mismatch listBox. Pin
arsendem4-Mar-10 2:30
arsendem4-Mar-10 2:30 
AnswerRe: parameter count mismatch listBox. Pin
Not Active4-Mar-10 3:04
mentorNot Active4-Mar-10 3:04 
AnswerMessage Closed Pin
4-Mar-10 3:05
stancrm4-Mar-10 3:05 
GeneralRe: parameter count mismatch listBox. Pin
arsendem4-Mar-10 3:20
arsendem4-Mar-10 3:20 
Questionappend to excel file c# !! Pin
noamtzu004-Mar-10 1:42
noamtzu004-Mar-10 1:42 
AnswerRe: append to excel file c# !! Pin
Not Active4-Mar-10 1:57
mentorNot Active4-Mar-10 1:57 
GeneralI know this, but more comfortable working with ..... Pin
noamtzu004-Mar-10 2:41
noamtzu004-Mar-10 2:41 
GeneralRe: I know this, but more comfortable working with ..... Pin
Not Active4-Mar-10 2:59
mentorNot Active4-Mar-10 2:59 
GeneralRe: I know this, but more comfortable working with ..... Pin
noamtzu004-Mar-10 7:16
noamtzu004-Mar-10 7:16 
GeneralRe: I know this, but more comfortable working with ..... Pin
Mirko19804-Mar-10 3:15
Mirko19804-Mar-10 3:15 
GeneralRe: I know this, but more comfortable working with ..... Pin
noamtzu004-Mar-10 7:15
noamtzu004-Mar-10 7:15 
GeneralRe: I know this, but more comfortable working with ..... Pin
Dave Kreskowiak4-Mar-10 3:45
mveDave Kreskowiak4-Mar-10 3:45 
GeneralRe: I know this, but more comfortable working with ..... Pin
noamtzu004-Mar-10 7:14
noamtzu004-Mar-10 7:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.