だから私はC#を初めて使っています。私はPowerShellで私のツール(7年分のツール)をビルドしていましたが、Visual Studioライセンスを手に入れることはできませんでしたが、今は持っています...c#Powershell Consoleでリアルタイムにテキストボックスを更新する
私はsystem.componentmodel.backgroundworkerについて読んでいます私はそれを実装するのに問題があります。
Heres my code with Background worker and with。誰かが
using System;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.IO;
namespace ServerStatusChecks
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// run our script and put the result into our textbox
// NOTE: make sure to change the path to the correct location of your script
textBox1.Text = RunScript(LoadScript(@"c:\utils\Script.ps1"));
}
// helper method that takes your script path, loads up the script
// into a variable, and passes the variable to the RunScript method
// that will then execute the contents
private string LoadScript(string filename)
{
try
{
// Create an instance of StreamReader to read from our file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filename))
{
// use a string builder to get all our lines from the file
StringBuilder fileContents = new StringBuilder();
// string to hold the current line
string curLine;
// loop through our file and read each line into our
// stringbuilder as we go along
while ((curLine = sr.ReadLine()) != null)
{
// read each line and MAKE SURE YOU ADD BACK THE
// LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
fileContents.Append(curLine + "\n");
}
// call RunScript and pass in our file contents
// converted to a string
return fileContents.ToString();
}
}
catch (Exception e)
{
// Let the user know what went wrong.
string errorText = "The file could not be read:";
errorText += e.Message + "\n";
return errorText;
}
}
// Takes script text as input and runs it, then converts
// the results to a string to return to the user
private string RunScript(string scriptText)
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = pipeline.Invoke();
// close the runspace
pipeline.Dispose();
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
// return the results of the script that has
// now been converted to text
return stringBuilder.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
は今ここにあるテキストボックスに(多少)私はPowerShellのジョブをキックオフするとき、私のUIが応答しなく行かないと私は実際に得ることができるので、私はそれを実装することができますどのように実際の更新を見に私を助けることができます私と一緒にコードの問題はあなたが何を実行していないということであるBW(私はそれがいくつかの方法を動作するように取得しようとしました、それは今混乱だとしてそれはモンスターのビットです)
using System;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.ComponentModel;
using System.Threading;
using System.IO;
namespace ServerStatusChecks
{
public partial class Form1 : Form
{
private BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
}
private string LoadScript(string filename)
{
try
{
// Create an instance of StreamReader to read from our file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(filename))
{
// use a string builder to get all our lines from the file
StringBuilder fileContents = new StringBuilder();
// string to hold the current line
string curLine;
// loop through our file and read each line into our
// stringbuilder as we go along
while ((curLine = sr.ReadLine()) != null)
{
// read each line and MAKE SURE YOU ADD BACK THE
// LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
fileContents.Append(curLine + "\n");
}
// call RunScript and pass in our file contents
// converted to a string
return fileContents.ToString();
}
}
catch (Exception e)
{
// Let the user know what went wrong.
string errorText = "The file could not be read:";
errorText += e.Message + "\n";
return errorText;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
// run our script and put the result into our textbox
// NOTE: make sure to change the path to the correct location of your script
textBox1.Text = RunScript(LoadScript(@"c:\utils\Script.ps1"));
}
private void bw_DoWork(object sender, EventArgs e)
{
}
// Takes script text as input and runs it, then converts
// the results to a string to return to the user
private string RunScript(string scriptText)
{
// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = pipeline.Invoke();
// close the runspace
pipeline.Dispose();
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
// return the results of the script that has
// now been converted to text
return stringBuilder.ToString();
}
// helper method that takes your script path, loads up the script
// into a variable, and passes the variable to the RunScript method
// that will then execute the contents
private void Form1_Load(object sender, EventArgs e)
{
Thread.Sleep(1000);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
Thread.Sleep(1000);
}
}
}
おかげで次のステップに進んでくれました!ただし、呼び出しはスレッドセーフではなく、戻りデータがクラッシュしています。エラー:{"クロススレッド操作が有効でない:コントロール 'textBox1'が作成されたスレッド以外のスレッドからアクセスされました。}} – Ericrs
回答を更新しました(2回、初めて不足していました!それは助ける! – 3burk
@ king3vbo [クロススレッド操作が有効でない:作成されたスレッド以外のスレッドからアクセスされたコントロール](http://stackoverflow.com/q/142003/1115360) - OPはコントロールが終了したときだけでなく、BGWが実行されている間に更新されます。 –