I have an AutoUpdater application that launches another application. Is there anyway to force that second application to ONLY run if it was started by the AutoUpdater?
The problem is, some end-users will create a shortcut to the main app file on their desktop. This becomes a problem because it's supposed to check for updates to the app before it launches, and they don't receive our updates.
One thought I had was to create an IPC channel via WCF and issue a very simple command from the AutoUpdater to the other application. If the other app doesn't receive that command within 2 or 3 seconds it would close itself.
This seems like more code/overhead than what should be needed though. Is there an easier way?
Thanks!
Windows forms applications also have a main method, which can take parameters. You could read some parameter, and if it doesn't conform to your rules, you can either keep the form from opening (therefore the user wouldn't see a thing), or you can chastise the user, I mean, give a message that they shouldn't open your app that way. I think this is simpler than using WCF.
You can run your App by using
System.Diagnostics.Process.Start("", "TestString");
The first string is the path of App you want to start, something like "C:/AppName.exe" and the second string is any text you want but it must be the same string that you check in the 'if' below.
In the App you need to check the text given trough the second string.
static void Main(string[] args)
{
if(!args.Length == 1 || !args[0]=="TestString") Environment.Exit(0);
//↑ lenght check to avoid exception, then check for the string itself
//the OS gives the string to args, you don't need to take care about that
//rest of your code
}
I hope I helped to solve it.
Related
I am hoping to check at the beginning of an automated test if an application is open. I can check if the process is running by doing the following
foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.Contains(name))
{
return true;
}
}
However, the process I want to find starts up about a minute before the application actually opens and is ready to be used by the test methods (its a very slow starting application). The above code sample looks at all windows processes running, but I am wondering, is there a way to do a similar method but to look at windows applications running?
There is a method already in class Process that you can use to check if an app with a UI has fully started:
Process.WaitForInputIdle(int milliseconds)
This will wait up to milliseconds ms for the message loop to become idle (and returns a bool to indicate success status). Depending on the application you're waiting for, you might want to allow 30 seconds or longer.
This might work for you, but be aware that in my experience for some applications it is not totally reliable!
The Windows API documentation has more details about the Windows API function that WaitForInputIdle() calls behind the scenes.
When a process is started, you can say application has started.
What you want is to wait until application startup progress has completed or not.
This means, when process is started, application startup begins. When application startup is completed, is becomes ready for user input. So I think you should have a look at following question and its answers.
Programmatically, how does this application detect that a program is ready for input
Apllication is proces.
If you can modify app, at app start you can create file and at end delete it. So you can chceck file existance. If file exist app starting/started.
If you need info when main form is created use:
WINFORMS
Form.Shown event.
WPF Loaded Event
uITestControl.Exists did the trick for me.
This method will return a boolean value corresponding to the existence of the application window being open. This allows an if statement to be created that can open the application if not already open, or do nothing if its already open.
I am developing a Forms application that is either supposed to start on Windows boot (by registering a key in the registry) or by the actual user.
In case the user starts it by hand (i.e. clicking the executable file), I want the form to show up. In case Windows triggered the event, I want the application to be hidden (into the tray bar).
Is there a way to programmatically determine which of those cases occured?
I would suggest adding a command line parameter, something like -minimized and pass that when starting it on boot.
See this thread if you are looking for how to read the arguments.
You can start you application upon Windows startup with a certain parameter, while a user will start it without this parameter.
The easiest way to do that, that I can think about, is:
When you register to run in Windows boot, pass a parameter (some default parameter), in case when user will run it, parameter will be absent.
In this way you would be able, depending on presence or absence of a paremeter, to determine the startup option of your program.
I'm not going to go into details why am trying to do this, instead of making the main application do the work. I think it's currently easier for me. But I'm not going to use this technique in the future.
In my case, the main form has a button that opens another form. In the second one for you can adjust the amount, pause, resume and stop the work of the console application (sound totally useless (and maybe stupid), but, like I said, I'm not going to go into details why). This means that the application must have access to all the variables and resources of the whole program and vise versa.
I know how to launch a new form trough a main form, but I don't know how to launch a console application.
EDIT:
I forgot to mention, that the console application is a part of the solution.
Your requirement is a bit vague; "the application must have access to all the variables and resources of the whole program and vise versa". 'Variables and resources' cannot be shared across processes, you will instead need interprocess communication of some form.
If your console app merely needs to communicate back to the calling forms app that a RPC has succeeded then use exit codes in the console app, see: How do I return a value from a console application to a service in .NET?
Otherwise this has been answered before: Getting the ouput from Console window into Winform application
You'll need to either create a console emulator (time consuming and difficult to get right), or fire up cmd.exe in another process and use remote procedure calls (or another inter process communication method) to communicate between the two processes
If you want to communicate between the two processes, take a look at this library here:
https://github.com/TheCodeKing/XDMessaging.Net
It allows you to send messages from one app to the other. For example, App1 sends a message "stop" on the channel "randomkey" to ConsoleApp1, ConsoleApp1 can listen on the channel "randomkey" and intercept the "stop" message and stop its current processing.
If you wanted to just open the console window, just use System.Diagnostics.Process.Start();
You can just call Main directly. Beware of doing this on the UI thread directly though!
SomeConsoleApp.Main(new string[]{"-O", "File 1.txt", "-some-parameter"});
Or if you only have an exe, you can do:
System.Diagnostics.Process.Start("someconsoleapp.exe");
How would I go about modifying my c# console application to retrieve values or have methods or events triggered by a shell command.
Basically after the applications loads I want the ability to be able to execute functions in the application as well as pass data to those functions.
Any Direction?
.NET 3.5 Adds Named Pipes Support, I think it can be a solution.
EDIT: better link.
You may use SingleInstance pattern - C# : how to - single instance application that accepts new parameters? .
you should accept input from the console using something like:
Console.ReadLine();
it would work, then the user types a command and clicks enter and you parse the command and execute the actions you need to execute.
it's a bit old fashion like DOS programs, and keep in mind that depending on your design the Console application you are writing would hang until the user enters value and presses ENTER key, means cannot execute anything else because the thread which callls Console.ReadLine waits that command to come from the user.
How can I make my already running C# Windows Form Application be able to receive commands from the command line while it is already running?
For example: if my application is playing a video now then I want to be able to type on the command line "MyApp /stop" so that while the application is still running it stops the playing the video without exiting from current session.
From your question it seems that your first process is still running and you start a second instance of it, and you wish that instance to communicate with the first.
What you are looking for is called inter-process communication (IPC). The standard way of doing this in .NET is to use Windows Communication Foundation (WCF).
One way would be to make your app a singleton, and whenever another instance is run, it will pass arguments to the already running process.
Example: http://www.codeproject.com/KB/cs/SingletonApplication.aspx
By sending a command like that, you'd be firing up another process. Certain command line arguments could do some kind of IPC to signal the "main" instance of the running app.
Without changing your design structure and assuming your application is a standalone application (running on a local PC),
One method is to make one thread of your application wait (WaitOne()) for a named mutex or semaphore (link text).
When you start your application (your second instance), you parse your commandline (via the args arguments). If the args[0] contains your "/stop" command, you "Release()" the named mutex/semaphore. Then your thread (in the first instance) will be waken to stop the playing the video.
Then again (having said all the above), a more simple solution is to have a STOP button in your application where the user can click on it.