call a console application [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the code to call a console application within a WPF? I have an application in WPF you need:
call a console application;
the console application closes the WPF
The console application again calls WPF and close console application.
This is necessary because I am doing a system where updates to the application must close the files to copy.
For close application WPF I am using the following:
Process wpfProc = Process.GetProcessesByName("MainWindow.exe").First();
wpfProc.Kill();
return in console application:
Unhandled Exception: System.InvalidOperationException: Sequence sontains no elements
at System.Link.Enumerable.First[TSource]<IEnumerable'1 source>
at Updater.Program.Main<String[] args> in d:\endereçodoUpdater\Program.cs:line 17
in line 17 have the following:
Process wpfProc = Process.GetProcessesByName("MainWindow").First();
How do I resolve this?

You do this with Process.Start:
Process myProc = Process.Start("MyConsoleApp.exe");
//Close gracefully
Application.Exit();
In MyConsoleApp.exe, you would need to use GetProcessByName to kill your WPF app, and then Process.Start again to restart it:
Process wpfProc = Process.GetProcessesByName("MyWpfApp.exe").First();
//If you want to directly kill it
wpfProc.Kill();
//Or be nice and let it kill itself
wpfProc.WaitForExit();
//Do stuff
Process.Start("MyWpfApp.exe");
System.Diagnostics.Process on MSDN

Related

Process exit on closing WPF application [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
A WPF appplication is hosted using
System.Diagonistics.Process
I have subscribed to the exit event of the process. I wanted to know how the window's close triggers the exit event.
The process exits when the call to Application.Run() in the Main method returns.
Application.Run() in turns calls Dispatcher.Run() which keeps the dispatcher loop running until the framework calls App.CriticalShutdown, assuming the App.ShutdownMode is set to either ShutdownMode.OnLastWindowClose or ShutdownMode.OnMainWindowClose. Otherwise you shut down explicitly.
If you look in the source code, you'll see an example of how App.CriticalShutdown is called from the Window class:
if (((App.Windows.Count == 0) && (App.ShutdownMode == ShutdownMode.OnLastWindowClose))
|| ((App.MainWindow == this) && (App.ShutdownMode == ShutdownMode.OnMainWindowClose)))
{
App.CriticalShutdown(0);
}

How to restrict .exe file to run from C# WPF app only [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have an WPF applications which is used to download several application and run those applications from this wpf app.(Can think of as similar to "Windows Store app")
Now I need to restrict the other application exe files that are downloaded from my wpf app to only run from my wpf app. If user tries to run the exe from files system it should prompt an error message saying restricted access.
How can I lock the exe to run from only my app after downloading it from my wpf app.
How can I achieve this in C# WPF app?
Do you have acces to the source code of the exes? Cause then you can add some sort of login to the downloaded exe.
static void Main(string[] args)
{
if (args[0] != "password")
{
Environment.Exit(0);
}
}
In case you don't have acces to the source code of the downloaded exes you can use this solution. This does require to have the application running at all times.
bool notepadopened = false;
while (true)
{
foreach (Process p in Process.GetProcessesByName("notepad"))
{
if (!notepadopened && p.ProcessName.ToString() == "notepad")
{
p.Kill();
}
}
Thread.Sleep(1000);
}

From multiprocess to multithreading [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I recently joined a team responsible for the enhancement of an existing 5-year old program.
It was developed with multithreading in mind, but not correctly implemented.
The solution is composed of multiple Windows Forms programs (let's call them Screen 1 and Screen 2).
Screen 1 can launch Screen 2 multiple times with differents parameters, but launches them as Process, not as Threads, Tasks, or by using BackgroundWorker.
Here is how it's done :
public void RunProcess(Arguments Arguments,string ExcutableName)
{
Process Prc = new Process();
Prc.StartInfo.FileName = ExcutableName;
Prc.StartInfo.Arguments = Arguments.GetProcessArguments();
Prc.Start();
_ListProcess.Add(Prc);
}
The processes are kept in memory and killed when Screen 1 closes.
I would like to refactor this in a cleaner way, going for a "single process" approach. What would you advise?
It looks like you need to port Screen 2's logic into the Screen 1 project, then replace the "Process" logic with threaded code.

How do I keep a process open? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a web application in ASP.Net/C# (using MVS), and I want to be able to launch some programs on the client side (this should be safe since it's only for intranet use), and keep these programs open in order to give them some command inputs through the web app.
For example, I would like to open "cmd.exe" client-side, and then being able to send multiple commands one after another (synchronized with some buttons of my web form) to the process.
How can I do this? I've read a lot about using a block of Javascript with an ActiveX object, or in C# with System.Diagnostics.Process, but I'm quite stuck on how to proceed.
function RunEXE()
{
var oShell = new ActiveXObject("WScript.Shell");
var prog = "c:\\WINDOWS\\system32\\notepad.exe";
oShell.Run('"'+prog+'"',1);
}
You could also have a look at custom protocol handlers, that's what e.g. Steam uses. You'd simply have a link like myprotocol://dosomething/whatever and it will launch your client-side application with the given URL.
It's basically just about writing a registry key: http://msdn.microsoft.com/en-us/library/ie/aa767914(v=vs.85).aspx
For example, to register a protocol named alert, you can do this:
HKEY_CLASSES_ROOT
alert
(Default) = "URL:Alert Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "alert.exe,1"
shell
open
command
(Default) = "C:\Program Files\Alert\alert.exe" "%1"
This will cause the application in command to be launched when you navigate to an url that starts with alert://.
Using c#
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "CMD.EXE";
startInfo.Arguments = "list of args";
Process.Start(startInfo);
Now after that you can keep checking weather the is open or not by
Process[] processes = Process.GetProcesses();
or
Process[] chromes = Process.GetProcessesByName("chrome");
Check if you process is open or not then pass args to it

How to sync system clock with global date/time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My system clock is going crazy randomly at any moment and changing the system clock's date/time to a random one. It's not the lithium battery nor a virus because I checked. Also it's not something from the Windows.System.Time itself.
I want to create a process that will, on an interval, check to see if the system clock's date/time matches the global date/time and if not, it would sync.
I need this to run in the background. I am not even sure if a Windows process is correct way to accomplish this. I am open to any other solutions as well.
Create a new c# empty project. Click on the project and go to Properties change the output type to Windows Application (This will remove the console).
Create a new class example: Example.cs
Write the static entry point eg:
public class Example
{
static void Main(string[] args)
{
}
}
Insert your code in the Main routine.
This will create a process that contains no console/window/service.
I'm guessing this is what you want.

Categories