External program check
External program check
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!
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
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
http://msdn.microsoft.com/en-us/library ... s.90).aspx
Best Regards,
Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer
Beijer Electronics, Inc.
Ron Lloyd | Applications Engineer
Re: External program check
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!
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
What is your target platform (Panel or PC/TxC)?
Here is a PC/TxC solution
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
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!
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
Easy as pie
1.
Second argument to Process.Start is filename, to which file you wont to open on start.
2.
We can both easy Maximize and Restore the notepad window with the ShowWindow function in user32.dll.
So lets put this togetter, this is our new code
Best Regards
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");
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");
}
}
}
Re: External program check
It works like a charm !!!!
I really appreciate your help with this,
THANK YOU very much Edmund!!
I really appreciate your help with this,
THANK YOU very much Edmund!!
Re: External program check
You're welcome
Re: External program check
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.
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.
-
- Posts: 824
- Joined: Tue Mar 13, 2012 9:53 am
Re: External program check
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;
This is the using statement you need for dllimport:
using System.Runtime.InteropServices;
Best Regards,
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer
Mark Monroe
Beijer Electronics, Inc. | Applications Engineer