External program check

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
smolenak
Posts: 22
Joined: Tue Nov 20, 2012 3:06 am

External program check

Post 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!

User avatar
Ron L.
Posts: 214
Joined: Fri Jul 15, 2011 3:21 pm

Re: External program check

Post 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
Best Regards,

Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer

smolenak
Posts: 22
Joined: Tue Nov 20, 2012 3:06 am

Re: External program check

Post 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!

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: External program check

Post 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");
            }	
      }
}

Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

smolenak
Posts: 22
Joined: Tue Nov 20, 2012 3:06 am

Re: External program check

Post 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!

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: External program check

Post 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
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

smolenak
Posts: 22
Joined: Tue Nov 20, 2012 3:06 am

Re: External program check

Post by smolenak »

It works like a charm !!!! ;)

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

User avatar
Edmund
Posts: 92
Joined: Thu Nov 29, 2012 2:27 pm

Re: External program check

Post by Edmund »

You're welcome :)
Edmund Andersson

AITECH AB

Part of Beijer Integrator Group

GoranW
Posts: 7
Joined: Mon Apr 29, 2013 7:34 am

Re: External program check

Post 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.

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: External program check

Post 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;
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

Post Reply