Page 1 of 2

External program check

Posted: Mon Feb 25, 2013 6:14 am
by smolenak
Hello,

there is Run command in iX, when you want to execute the external program. Could you tell me how could I check, that if it's (process) already running (if it is, then activate focus, bring to top of the iX window) or not (then run the application)?

Thanks!

Re: External program check

Posted: Tue Feb 26, 2013 10:23 am
by Ron L.
It's not clear to me what you are asking. C# gives you a library you can use to do some things with processes.
http://msdn.microsoft.com/en-us/library ... s.90).aspx

Re: External program check

Posted: Tue Mar 12, 2013 2:42 am
by smolenak
Sorry about my bad explanation..

So I just want to run one instance of notepad from iX. Now I have button which does the job. BUT, when user presses the button many times, it just runs one more notepad session again.. until we have many notepad windows open.. So I want to limit somehow that I have just that one and only notepad session open. So if there would be solution to check, if notepad is already running.

Thank you for the support!

Re: External program check

Posted: Tue Mar 12, 2013 9:45 am
by Edmund
What is your target platform (Panel or PC/TxC)?

Here is a PC/TxC solution

Code: Select all

namespace Neo.ApplicationFramework.Generated
{
    using System.Windows.Forms;
    using System;
    using System.Drawing;
    using Neo.ApplicationFramework.Tools;
    using Neo.ApplicationFramework.Common.Graphics.Logic;
    using Neo.ApplicationFramework.Controls;
    using Neo.ApplicationFramework.Interfaces;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    public partial class Screen1
    {
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);
		
            void Button1_Click(System.Object sender, System.EventArgs e)
            {
                Process[] processes = Process.GetProcessesByName("notepad");
                if (processes.Length > 0)
                    SetForegroundWindow(processes[0].MainWindowHandle);
                else
                    Process.Start("notepad.exe");
            }	
      }
}


Re: External program check

Posted: Fri Apr 05, 2013 1:32 am
by smolenak
Yes I'm running application in PC environment.
Thanks Edmund, it works great! :)

Only one problem still exists.. when notepad-window is minimized by user, it won't open it back to screen while we press the button (script). Would there be a solution for this..?

And another question to this - how can I open (with this script) the same text-file (example, if I want to open always Text.csv -file), every time when I'm opening the notepad (executing script).

Thanks for advance!

Re: External program check

Posted: Fri Apr 05, 2013 10:48 am
by Edmund
Easy as pie ;)

1.

Second argument to Process.Start is filename, to which file you wont to open on start.

Code: Select all

Process.Start("notepad.exe", @"C:\test.txt");
2.

We can both easy Maximize and Restore the notepad window with the ShowWindow function in user32.dll.

Code: Select all

	[DllImport("user32.dll")]
		static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

Code: Select all


// Restore
ShowWindow(processes[0].MainWindowHandle, 9);

// Maximize
ShowWindow(processes[0].MainWindowHandle, 3);

So lets put this togetter, this is our new code

Code: Select all

namespace Neo.ApplicationFramework.Generated
{
	using System.Windows.Forms;
	using System;
	using System.Drawing;
	using Neo.ApplicationFramework.Tools;
	using Neo.ApplicationFramework.Common.Graphics.Logic;
	using Neo.ApplicationFramework.Controls;
	using Neo.ApplicationFramework.Interfaces;
	using System.Diagnostics;
	using System.Runtime.InteropServices;
    
	public partial class Screen1
	{
		[DllImport("user32.dll")]
		static extern bool SetForegroundWindow(IntPtr hWnd);
		[DllImport("user32.dll")]
		static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
      
		void Button1_Click(System.Object sender, System.EventArgs e)
		{
			Process[] processes = Process.GetProcessesByName("notepad");
			if (processes.Length > 0)
			{
				SetForegroundWindow(processes[0].MainWindowHandle);
				ShowWindow(processes[0].MainWindowHandle, 9);	
			}
			else
				Process.Start("notepad.exe", @"C:\test.txt");
		}   
	}
}
Best Regards

Re: External program check

Posted: Mon Apr 08, 2013 3:12 am
by smolenak
It works like a charm !!!! ;)

I really appreciate your help with this,
THANK YOU very much Edmund!! :)

Re: External program check

Posted: Mon Apr 08, 2013 3:56 am
by Edmund
You're welcome :)

Re: External program check

Posted: Tue Apr 30, 2013 5:06 am
by GoranW
Hi Edmund,
I copied your script into my test application but I got a couple of compilation errors:

"The type or namespace name "DllImport" could not be found"
and
"The type or namespace name "DllImportAttribute" could not be found"

Both referring to the line: [DllImport("user32.dll")]

What am I missing?

Also, how do you do it on other applications, for example Adobe Reader or Euroterm's ITools?

Note that I n knowledge of C# programming. I have manged so far by staeling others (like you) scripts.
So please, keep the answer on a level that even I can understand.

Re: External program check

Posted: Tue Apr 30, 2013 9:03 am
by mark.monroe
The above code examples only work on TxC and PC target panels. You can not use them on a Win CE based panel. You also need to make sure that you have all the "using" statements that are in the examples. Otherwise your script will not understand what the DllImport function is.

This is the using statement you need for dllimport:
using System.Runtime.InteropServices;