Community Forums Archive

Go Back

Subject:"Cancel" button
Posted by: bejj
Date:8/30/2013 11:23:18 AM

I have a long .cs script that will run during 1 hour. I have no feature to stop / cancel the script : I have to quit SF10 if I want to stop...

Is there a way for adding a "CANCEL" button (in the status bar) like in all SF Tools ?

or

Is there a way for adding a keyboard shortcut to stop a script ? (like CTRL C)

Thanks in advance.

Subject:RE: "Cancel" button
Reply by: roblesinge
Date:8/30/2013 1:12:41 PM

The short answer is, "maybe." Post your code and perhaps I can help.

Rob.

Subject:RE: "Cancel" button
Reply by: bejj
Date:8/30/2013 1:55:45 PM

Hello,
Thank you for your answer.
As I work with 5000 wav files, I would like to be able to stop the script at any time. (Either with CTRL+C or with a "Cancel" button in the status like in all SF effect tools)

Here is the code :


using System;
using System.IO;
using System.Windows.Forms;
using SoundForge;

public class EntryPoint {
public string Begin(IScriptableApp app)
{

string[] fileHolder = Directory.GetFiles(@"D:\Temp", "*.wav", SearchOption.AllDirectories);

for (int x = 0; x < fileHolder.Length ; x++)
{
app.OutputText(fileHolder[x]);

ISfFileHost file = app.OpenFile(fileHolder[x], false, true);

// here there is 10 steps like for example :
// file.DoEffect(...);
// file.CropAudio(...);
// file.DoEffect(...);
// file.DoEffect(...);


file.Close(CloseOptions.SaveChanges);

}

return null;
}

public void FromSoundForge(IScriptableApp app)
{
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
string msg = Begin(app);
app.SetStatusText(msg != null ? msg : String.Format("Script '{0}' is done.", Script.Name));
}
public static IScriptableApp ForgeApp = null;
public static void DPF(string sz) { ForgeApp.OutputText(sz); }
public static void DPF(string fmt, params object[] args) { ForgeApp.OutputText(String.Format(fmt, args)); }
public static string GETARG(string key, string str) { string val = Script.Args.ValueOf(key); if (val == null) val = str; return val; }
} //EntryPoint

Subject:RE: "Cancel" button
Reply by: roblesinge
Date:8/30/2013 5:00:39 PM

This isn't as optimized as it could be, but here is what I came up with. You basically have to roll your own GUI to get buttons that can stop the script. So, when you fire it off, you'll have to hit the "Start Script" button for it to run. Then you should be able to hit the "Stop Script" button and it *should* stop.

I can't fully test this to your circumstances since I don't have your files. So, maybe try it out on a smaller test batch of files, but obviously large enough to allow you time to hit the stop button.

I added a progress bar just to give you a visual sense of how much further you have to go. You could also implement a label to give you an exact file count.

Let me know if this doesn't work. Like I said, it's untested.


using System;
using System.IO;
using System.Windows.Forms;
using SoundForge;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;

public class EntryPoint
{
public void Begin(IScriptableApp app)
{
Form1 thisForm = new Form1();
thisForm.MyForm(app);
}

public class Form1 : Form
{
Form form1;
Button stopButton, startButton;
ProgressBar prog;
public bool keepGoing = true;
IScriptableApp appx;

public void MyForm(IScriptableApp app1)
{
//You'll use this appx var when trying to call methods on the SF app.
appx = app1;

this.form1 = new Form1();

//set up our startButton
startButton = new Button();
startButton.Name = "startButton";
startButton.Text = "Start Script";
startButton.Location = new System.Drawing.Point(20, 10); //change this to change location.
startButton.Click += new EventHandler(startButton_Click);

//set up our stopButton
stopButton = new Button();
stopButton.Name = "stopButton";
stopButton.Text = "Stop Script";
stopButton.Location = new System.Drawing.Point(startButton.Right + 10, 10); //change this to change location.
stopButton.Click += new EventHandler(stopButton_Click);

//set up our progress bar
prog = new ProgressBar();
prog.Name = "progressBar";
prog.Location = new System.Drawing.Point(20, startButton.Bottom + 10);
prog.ClientSize = new System.Drawing.Size(startButton.Width + stopButton.Width + 10, startButton.Height);

//set up our form
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
form1.Text = "Your Script";
form1.MaximizeBox = false;
form1.MinimizeBox = false;
form1.StartPosition = FormStartPosition.CenterParent;
form1.ClientSize = new System.Drawing.Size(200, 80); //size of window for button. Might be too big.

//add our stopButton to our form
form1.Controls.Add(stopButton);
form1.Controls.Add(startButton);
form1.Controls.Add(prog);

form1.ShowDialog();
}

public void stopButton_Click(object sender, EventArgs e)
{
/*
* Event handler for the stopButton. When clicked it sets keepGoing to false
* and stops the script on its next pass and closes the window.
*/

keepGoing = false;
form1.Close();
return;
}

public void startButton_Click(object sender, EventArgs e)
{
string[] fileHolder = Directory.GetFiles(@"D:\Temp", "*.wav", SearchOption.AllDirectories);

this.prog.Maximum = fileHolder.Length;
this.prog.Minimum = 0;
this.prog.Value = 0;

for (int x = 0; x < fileHolder.Length; x++)
{
if (this.keepGoing)
{
appx.OutputText(fileHolder[x]);
ISfFileHost file = appx.OpenFile(fileHolder[x], false, true);

//Your code goes here.

file.Close(CloseOptions.SaveChanges);
this.prog.Value++;
}
}
}

}

public void FromSoundForge(IScriptableApp app)
{
ForgeApp = app; //execution begins here
app.SetStatusText(String.Format("Script '{0}' is running.", Script.Name));
Begin(app);
app.SetStatusText(String.Format("Script '{0}' is done.", Script.Name));
}
public static IScriptableApp ForgeApp = null;
public static void DPF(string sz) { ForgeApp.OutputText(sz); }
public static void DPF(string fmt, params object[] args) { ForgeApp.OutputText(String.Format(fmt, args)); }
} //EntryPoint

Subject:RE: "Cancel" button
Reply by: bejj
Date:8/31/2013 3:30:38 AM

Hi roblesinge,

Thanks a lot. In the meantime, I have added some WaitForSuccessorCancel() everywhere and now I often have some Cancel button, so it is enough for my needs now.
But I will try your nice solution as well!

Thanks a lot.

Go Back