home
products
contribute
download
documentation
forum
Home
Forums
New posts
Search forums
What's new
New posts
All posts
Latest activity
Members
Registered members
Current visitors
Donate
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Search titles only
By:
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
MediaPortal 1
MediaPortal 1 Plugins
Executing an external application....and getting its output
Contact us
RSS
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="samuel337" data-source="post: 55504" data-attributes="member: 10347"><p>The MSDN example isn't what I was thinking of.</p><p></p><p>I was thinking of something along the lines of this:</p><p>- put all converting code in a seperate class. When you need to convert a file, instantiate that class, passing along the filename in its constructor. Give this class a class-scope variable, e.g. create a variable after the class declaration, not within a method.</p><p></p><p>e.g.</p><p>[code]</p><p>ConvertFile convertFileClass;</p><p>[/code]</p><p></p><p>Here's an example class you can build on:</p><p>[code]</p><p>using System;</p><p>using System.Collections.Generic;</p><p>using System.Text;</p><p></p><p>namespace ProcessDemo</p><p>{</p><p> public class ConvertFile</p><p> {</p><p> //enum to keep track of status</p><p> private enum ConvertState</p><p> {</p><p> Step1 = 0, </p><p> Step2 = 1, </p><p> Step3 = 2,</p><p> Finished = 3</p><p> }</p><p> </p><p> //class variables</p><p> System.Diagnostics.Process convertProcess;</p><p> ConvertState curState;</p><p> bool started;</p><p> string BurnerFilesPath;</p><p> string curFileName;</p><p> string[] curFileNames;</p><p> int curFileNameCount;</p><p> string TempFolderPath;</p><p></p><p> //constructor</p><p> public ConvertFile(string[] FileNames)</p><p> {</p><p> curFileNames = FileNames;</p><p> curFileNameCount = 0;</p><p> NextFileName();</p><p> started = false;</p><p> BurnerFilesPath = "whatever here";</p><p> TempFolderPath = "whatever here";</p><p> }</p><p></p><p> public void NextFileName()</p><p> {</p><p> curFileNameCount++;</p><p></p><p> if (curFileNameCount <= curFileNames.Length)</p><p> {</p><p> //get next filename</p><p> curFileName = curFileNames[curFileNameCount - 1];</p><p> curState = ConvertState.Step1;</p><p></p><p> //start again</p><p> NextStep();</p><p> }</p><p> else</p><p> {</p><p> //all files processed</p><p> AllFinished(this, new System.EventArgs());</p><p> }</p><p> }</p><p></p><p> public bool Start()</p><p> {</p><p> if (started == false)</p><p> {</p><p> //start</p><p> started = true;</p><p> NextStep();</p><p> return true;</p><p> }</p><p> else</p><p> {</p><p> return false;</p><p> }</p><p> }</p><p></p><p> private void NextStep()</p><p> {</p><p> switch (curState)</p><p> {</p><p> case ConvertState.Step1:</p><p> //do stuff for step one here (e.g. transcoding from AVI to MPG)</p><p> //Example:</p><p> convertProcess = new System.Diagnostics.Process();</p><p> convertProcess.StartInfo.FileName = BurnerFilesPath + "mpgtx.exe";</p><p> convertProcess.StartInfo.Arguments = "-f -d \"" + curFileName + "\" -b \"" + TempFolderPath + "\\" + curFileName + "\" ";</p><p> convertProcess.EnableRaisingEvents = true; //IMPORTANT: tells process object to raise event when finished</p><p> convertProcess.Exited += new EventHandler(ProcessExitedHandler);</p><p> convertProcess.Start();</p><p> convertProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(OutputDataReceivedHandler);</p><p> convertProcess.BeginOutputReadLine();</p><p> break;</p><p></p><p> case ConvertState.Step2:</p><p> //do next step here, e.g. apply filters, do ad-removal etc.</p><p> break;</p><p></p><p> case ConvertState.Step3:</p><p> //do last step here, e.g. burn to disc</p><p> break;</p><p></p><p> case ConvertState.Finished:</p><p> //converting process completed, raise event</p><p> FileFinished(this, new FileFinishedEventArgs(curFileName));</p><p> NextFileName();</p><p> break;</p><p> }</p><p> }</p><p></p><p> private void ProcessExitedHandler(object sender, System.EventArgs e)</p><p> {</p><p> //one process has finished, start next process</p><p> curState += 1;</p><p> NextStep();</p><p></p><p> //announce to anyone who is listening</p><p> CompletedStep(this, new System.EventArgs());</p><p> }</p><p></p><p> private void OutputDataReceivedHandler(object sender, System.Diagnostics.DataReceivedEventArgs e)</p><p> {</p><p> //announce to anyone who is listening</p><p> OutputReceived(this, e);</p><p> }</p><p></p><p> //events</p><p> //in your UI class, listen in on these events to report back to user</p><p> public event EventHandler CompletedStep;</p><p></p><p> public delegate void FileFinishedEventHandler(object sender, FileFinishedEventArgs e);</p><p> public event FileFinishedEventHandler FileFinished;</p><p></p><p> public event EventHandler AllFinished;</p><p> public event System.Diagnostics.DataReceivedEventHandler OutputReceived;</p><p> }</p><p></p><p> public class FileFinishedEventArgs : System.EventArgs</p><p> {</p><p> public string FileName;</p><p></p><p> public FileFinishedEventArgs(string FinishedFileName)</p><p> {</p><p> FileName = FinishedFileName;</p><p> }</p><p> }</p><p>}</p><p></p><p>[/code]</p><p></p><p>When you need to use this class in your UI, do this:</p><p>[code]</p><p>private void ConvertFiles()</p><p>{</p><p>convertFileClass = new ConvertFile(FilesToBurn);</p><p>convertFileClass.CompletedStep += new EventHandler(convertCompletedStep);</p><p>convertFileClass.FileFinished+= new FileFinishedEventHandler(convertFileFinished);</p><p>convertFileClass.AllFinished+= new EventHandler(convertAllFinished);</p><p>convertFileClass.OutputReceived+= new System.Diagnostics.DataReceivedEventHandler(convertOutputReceived);</p><p></p><p>convertFileClass.Start();</p><p>}</p><p>[/code]</p><p></p><p>You will need to create methods, convertCompletedStep, convertFileFinished, convertAllFinished, convertOutputReceived to handle the events raised by the ConvertFile class.</p><p></p><p>This code is untested, so there may be small problems with it, but it should work.</p><p></p><p>HTH</p><p></p><p>Sam</p></blockquote><p></p>
[QUOTE="samuel337, post: 55504, member: 10347"] The MSDN example isn't what I was thinking of. I was thinking of something along the lines of this: - put all converting code in a seperate class. When you need to convert a file, instantiate that class, passing along the filename in its constructor. Give this class a class-scope variable, e.g. create a variable after the class declaration, not within a method. e.g. [code] ConvertFile convertFileClass; [/code] Here's an example class you can build on: [code] using System; using System.Collections.Generic; using System.Text; namespace ProcessDemo { public class ConvertFile { //enum to keep track of status private enum ConvertState { Step1 = 0, Step2 = 1, Step3 = 2, Finished = 3 } //class variables System.Diagnostics.Process convertProcess; ConvertState curState; bool started; string BurnerFilesPath; string curFileName; string[] curFileNames; int curFileNameCount; string TempFolderPath; //constructor public ConvertFile(string[] FileNames) { curFileNames = FileNames; curFileNameCount = 0; NextFileName(); started = false; BurnerFilesPath = "whatever here"; TempFolderPath = "whatever here"; } public void NextFileName() { curFileNameCount++; if (curFileNameCount <= curFileNames.Length) { //get next filename curFileName = curFileNames[curFileNameCount - 1]; curState = ConvertState.Step1; //start again NextStep(); } else { //all files processed AllFinished(this, new System.EventArgs()); } } public bool Start() { if (started == false) { //start started = true; NextStep(); return true; } else { return false; } } private void NextStep() { switch (curState) { case ConvertState.Step1: //do stuff for step one here (e.g. transcoding from AVI to MPG) //Example: convertProcess = new System.Diagnostics.Process(); convertProcess.StartInfo.FileName = BurnerFilesPath + "mpgtx.exe"; convertProcess.StartInfo.Arguments = "-f -d \"" + curFileName + "\" -b \"" + TempFolderPath + "\\" + curFileName + "\" "; convertProcess.EnableRaisingEvents = true; //IMPORTANT: tells process object to raise event when finished convertProcess.Exited += new EventHandler(ProcessExitedHandler); convertProcess.Start(); convertProcess.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(OutputDataReceivedHandler); convertProcess.BeginOutputReadLine(); break; case ConvertState.Step2: //do next step here, e.g. apply filters, do ad-removal etc. break; case ConvertState.Step3: //do last step here, e.g. burn to disc break; case ConvertState.Finished: //converting process completed, raise event FileFinished(this, new FileFinishedEventArgs(curFileName)); NextFileName(); break; } } private void ProcessExitedHandler(object sender, System.EventArgs e) { //one process has finished, start next process curState += 1; NextStep(); //announce to anyone who is listening CompletedStep(this, new System.EventArgs()); } private void OutputDataReceivedHandler(object sender, System.Diagnostics.DataReceivedEventArgs e) { //announce to anyone who is listening OutputReceived(this, e); } //events //in your UI class, listen in on these events to report back to user public event EventHandler CompletedStep; public delegate void FileFinishedEventHandler(object sender, FileFinishedEventArgs e); public event FileFinishedEventHandler FileFinished; public event EventHandler AllFinished; public event System.Diagnostics.DataReceivedEventHandler OutputReceived; } public class FileFinishedEventArgs : System.EventArgs { public string FileName; public FileFinishedEventArgs(string FinishedFileName) { FileName = FinishedFileName; } } } [/code] When you need to use this class in your UI, do this: [code] private void ConvertFiles() { convertFileClass = new ConvertFile(FilesToBurn); convertFileClass.CompletedStep += new EventHandler(convertCompletedStep); convertFileClass.FileFinished+= new FileFinishedEventHandler(convertFileFinished); convertFileClass.AllFinished+= new EventHandler(convertAllFinished); convertFileClass.OutputReceived+= new System.Diagnostics.DataReceivedEventHandler(convertOutputReceived); convertFileClass.Start(); } [/code] You will need to create methods, convertCompletedStep, convertFileFinished, convertAllFinished, convertOutputReceived to handle the events raised by the ConvertFile class. This code is untested, so there may be small problems with it, but it should work. HTH Sam [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 1
MediaPortal 1 Plugins
Executing an external application....and getting its output
Contact us
RSS
Top
Bottom