Process.GetProcessessByName doesnt work - c#

i have just made an updater whihch also controls if app running.
Process[] processOfApp = Process.GetProcessesByName(setting.ExeName);
if (processOfApp.Length == 0)
{
Program = StartProcess(realPath + "\\" + setting.ExeName + ".exe");
AppDir = realPath + "\\" + setting.ExeName + ".exe";
ProcessExtensions.StartProcessAsCurrentUser(AppDir);
}
If no app process then start process. But these code start multiple instances.
What i did wrong?
Edit: these code block is in while(true) and this is the only start process in the code.

You may have copied this from somewhere without knowing what it does? I am guessing StartProcess function starts a process. And I assume, so does ProcessExtensions.StartProcessAsCurrentUser. So yes, it would start multiple (two) processes instead of one. If the updater should run a program for the current user from a windows service, delete the Program = ... line and retrive that information later on (using Process.GetProcessesByName, for example), if it is needed for something. That way the updater will be run as Current User, which is probably what you want.
// This line starts an instance
Program = StartProcess(realPath + "\\" + setting.ExeName + ".exe");
AppDir = realPath + "\\" + setting.ExeName + ".exe";
// This line also starts an instance
ProcessExtensions.StartProcessAsCurrentUser(AppDir);

Related

c# Launch process without taking focus

I've created a GUI app that basically loads a video file, launches 3 processes with arguments in sequential order, (ffmpeg, x264, mp4box) and has the ability to abort if needed.
I'm trying to figure out a way to launch these processes without them stealing focus from whatever program is opened. Not necessarily the original form itself...it could be IE, MS WORD, etc...I know about ProcessWindowStyle, however, all options still take focus away. I do not want to start these processes hidden either.
This is an example of my code for starting a process
Process ffmpegProcess = new Process();
ffmpegProcess.StartInfo.FileName = Settings.Default.ffmpeg;
ffmpegProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
string outputaud = text2 + "_audio.aac";
string AudioBitrate = this.cBAudiobitrate.GetItemText(this.cBAudiobitrate.SelectedItem);
string ffargs = "-i " + quote + SourceFile + quote + " -filter_complex " + quote + "[0:2][0:3]amerge=inputs=2,pan=stereo|c0=c0+c1|c1=c0+c1[aout]" + quote + " -map " + quote + "[aout]" + quote + " -strict experimental -acodec aac -b:a " + AudioBitrate + "k " + quote + outputaud + quote;
ffmpegProcess.StartInfo.Arguments = ffargs;
ffmpegProcess.Start();
while (!ffmpegProcess.HasExited)
{
if (this.bw404p_withaudio.CancellationPending)
{
ffmpegProcess.Kill();
e.Cancel = true;
return;
}
Thread.Sleep(1000);
}
I know about using the Microsoft.VisualBasic Reference and Shell command with AppWinStyle as it has options for launching with no focus, I just can't seem to figure out how to modify my code to make it work with that.
Any ideas?
Thanks
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
Is that would you're looking for? More info below
How to start Process hidden?
http://www.dotnetperls.com/process-start
ProcessWindowStyle Enumeration

Enreco Video Rotation in Asp.Net Virtual Machine is not completing

I am developing an ASP.NET Web Api in which I need to concatenate some video clips and rotate them. I could achieve the same when I tried in my local system. When I deployed the same project to an Azure Virtual Machine I am not getting response. I am pretty sure that there isn't any issue till video concatenation because I could see the concatenated video in the expected folder. Here is the code snippet.
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
NReco.VideoConverter.ConcatSettings set = new NReco.VideoConverter.ConcatSettings();
ffMpeg.GetVideoThumbnail(_fileNames[0], imageRootPath + tobename + ".jpg");
if (_fileNames.Count() > 1)
{
ffMpeg.ConcatMedia(_fileNames, videoRootPath + tobename + "_r.mp4", NReco.VideoConverter.Format.mp4, set);
string path = HttpContext.Current.Server.MapPath("~\\bin\\");
System.Diagnostics.Process ffmpeg = new System.Diagnostics.Process();
ffmpeg.StartInfo.FileName = path + "\\" + "ffmpeg.exe";
ffmpeg.StartInfo.Arguments = "-i " + videoRootPath + tobename + "_r.mp4" + " -c copy -metadata:s:v:0 rotate=90 " + videoRootPath + tobename + ".mp4";
ffmpeg.Start();
ffmpeg.WaitForExit();
}
ffmpeg.ConcateMedia is working fine. I can't figure out why the External process that I have invoked does not complete. The same piece is working fine in my local Visual Studio.
Thank you in advance
It seems you are using Nreco VideoConvertor for joinging videos and external process to rotate the video.
You can always use Invoke method to write the custom commandline. something like this
ffMpeg.Invoke("-i " + videoRootPath + tobename + "_r.mp4" + " -c copy -metadata:s:v:0 rotate=90 " + videoRootPath + tobename + ".mp4");
Hope this Helps...
Your path ends with a slash, and when adding the paths together you also add a slash.
Use Path.Combine:
string path = HttpContext.Current.Server.MapPath("~\\bin");
ffmpeg.StartInfo.FileName = System.IO.Path.Combine(path, "ffmpeg.exe");

Java (Minecraft) doesnt start from c#

When I copy paste the javaw.exe -arguments to console it works but, when I launch it like this it doesn't work.
string directory = "C:\\Users\\Can\\AppData\\Roaming\\.minecraft";
string java = #"C:\windows\system32\javaw.exe";
string javaLocation = "C:\\Program Files\\Java\\jre7\\bin\\javaw.exe";
string RAM = "1G";
string username = "namehere";
string token = "--session token:"+tokenGenerated;
string version = "1.6.2";
string launch = "-Xmx" + RAM + " -Djava.library.path={0}\\versions\\1.6.2\\1.6.2-natives-7453523379463 -cp {0}\\libraries\\net\\sf\\jopt-simple\\jopt-simple\\4.5\\jopt-simple-4.5.jar;{0}\\libraries\\com\\paulscode\\codecjorbis\\20101023\\codecjorbis-20101023.jar;{0}\\libraries\\com\\paulscode\\codecwav\\20101023\\codecwav-20101023.jar;{0}\\libraries\\com\\paulscode\\libraryjavasound\\20101123\\libraryjavasound-20101123.jar;{0}\\libraries\\com\\paulscode\\librarylwjglopenal\\20100824\\librarylwjglopenal-20100824.jar;{0}\\libraries\\com\\paulscode\\soundsystem\\20120107\\soundsystem-20120107.jar;{0}\\libraries\\argo\\argo\\2.25_fixed\\argo-2.25_fixed.jar;{0}\\libraries\\org\\bouncycastle\\bcprov-jdk15on\\1.47\\bcprov-jdk15on-1.47.jar;{0}\\libraries\\com\\google\\guava\\guava\\14.0\\guava-14.0.jar;{0}\\libraries\\org\\apache\\commons\\commons-lang3\\3.1\\commons-lang3-3.1.jar;{0}\\libraries\\commons-io\\commons-io\\2.4\\commons-io-2.4.jar;{0}\\libraries\\net\\java\\jinput\\jinput\\2.0.5\\jinput-2.0.5.jar;{0}\\libraries\\net\\java\\jutils\\jutils\\1.0.0\\jutils-1.0.0.jar;{0}\\libraries\\com\\google\\code\\gson\\gson\\2.2.2\\gson-2.2.2.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl\\2.9.0\\lwjgl-2.9.0.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl_util\\2.9.0\\lwjgl_util-2.9.0.jar;{0}\\versions\\1.6.2\\1.6.2.jar net.minecraft.client.main.Main --username " + username + " " + token + " --version " + version + " --gameDir {0} --assetsDir {0}\\assets";
launch = String.Format(launch, directory);
string text = launch;
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
Directory.SetCurrentDirectory(#"C:\windows\system32\");
Process.Start("javaw.exe",
Path.Combine(launch));
What am I doing wrong?
Why do you need to call Path.Combine if your whole path is in one string?
Assuming your javaw.exe is actually in C:\windows\system32\, Process.Start("java.exe", launch); should work as intended.
Source - Path on MSDN: http://msdn.microsoft.com/en-us/library/system.io.path.aspx
Just use Java's real location instead of "javaw" like "c:\programfiles\Java\jre7\bin\javaw.exe"

Inconsistent results during debugging - Web API

I have some code that uses a library for a product called Fusion Pro. Based on some config and data files it will generate a PDF file. I have been getting consistent result of a complete lack of output without any thrown exceptions. However when debugging I have been able to get the PDF to generate by attaching the debugger to the code running in my local IIS and stepping through line by line. Nothing is consistent though. It seems to have a higher success rate if I use F11 instead of F10 and going "slow but steady" through each line. Unfortunately it doesn't always produce a PDF even with stepping through the code.
I have never experienced code behaving so inconstantly with the same inputs each time.
The only other relevant bits of information is this code is working in another asmx service without issue, it is running under a specific domain account (requires special security), and it is running in 32bit mode.
DLQueueClient client = new DLQueueClient();
DLQueueJob vdpQueueJob = new DLQueueJob();
string fileName = message.OutputFilePath;
var dataFilePath = CreateDataFile(fileName, message.DefFilePath, message.VariableDataContent, message.QTY);
var pdfFilePath = Path.Combine(new[] { message.OutputFilePath, dataFilePath.Replace(".txt", ".pdf") });
vdpQueueJob.Priority = priority;
vdpQueueJob.Queue = queueName;
string cmd = "\"" + Settings.FusionProExe + "\" \"" + dataFilePath + "\" \""
+ message.DiffFilePath + "\" \"" +
(isProof ? message.ProofConfigFilePath : message.PrintConfigFilePath) + "\" \""
+ pdfFilePath + "\"";
vdpQueueJob.CommandLine = cmd;
client.ConnectRemote(Settings.FusionProServer, Settings.FusionProUser, Settings.FusionProPassword, Settings.FusionProTimeout);
client.SubmitJob(vdpQueueJob, Settings.FusionProTimeout, true);
return pdfFilePath;

Unable to generate file from ASP.NET with inkscape

I have an ASP.NET application on my local machine that works. This application takes an SVG file and creates a PNG from it using inkscape. I have tried to migrate that application to my production server. Oddly, the png creation is not working. The really strange part is, an Exception is not being thrown either. I have taken the command line parameters that are being created and copied and pasted them into the command line environment and they work. For instance, here is the line:
inkscape.exe -f "C:\inetpub\wwwroot\MyTest\sample.svg" -e "C:\inetpub\wwwroot\MyTest\sample.png"
I thought it was something simple, so I extracted the code into a sample web project. This project just converts a .svg to a .png. Once again, it worked in my local environment, but not in the production environment. Here is the code:
protected void executeButton_Click(object sender, EventArgs e)
{
try
{
string sourceFile = Server.MapPath("svg") + "\\" + ConfigurationManager.AppSettings["sourceFile"];
string targetFile = Server.MapPath("png") + "\\" + ConfigurationManager.AppSettings["targetFile"];
string args = "-f \"" + sourceFile + "\" -e \"" + targetFile + "\" -w100 -h40";
string inkscape = ConfigurationManager.AppSettings["inkscapeExe"];
// Generate the png via inkscape
ProcessStartInfo inkscapeInfo = new ProcessStartInfo(inkscape, args);
Process inkscape = Process.Start(inkscapeInfo);
inkscape.WaitForExit(5000);
runLiteral.Text = "Success!<br />" + args;
}
catch (Exception ex)
{
runLiteral.Text = ex.GetType().FullName + "<br />" + ex.Message + "<br />" + ex.StackTrace;
}
}
Can someone tell me what I'm doing wrong?
Thank you!
A couple things to check:
Make sure that the application pool identity for the web application (found in IIS, usually NetworkService) has permissions to execute inkscape.exe
If that is fine, check to make sure that the directory grants Modify permissions to the apppool identity on the directory(ies) you are writing the PNG to ("C:\inetpub\wwwroot\MyTest")
Alternatively, you can use impersonation to run the executable under a specific Windows account.

Categories

Resources