c# string类型传递参数的问题

ERROR已经存在 2013-03-02 04:35:14
我有一个类,类的作用是执行cmd命令,同时将执行结果存在于一个string里面,同时这个string的内容会实时显示到一个textbox内。
执行了下面的代码以后,发现output还是为空,经过分析,那是肯定的,虽然在this.details = details;的时候,两个string 的地址是一样,但是this.details做了修改以后,就不是同一个东西了。
求问,有什么办法可以使得this.details的变化实时的返回到details(即调用函数的output)中?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

public class RunConsole
{
#region 私有变量
private Process p;
private Thread t;
private object threadLock = new object();

string filename = String.Empty;
string arg = String.Empty;
string details = String.Empty;

AutoResetEvent resetEvent = new AutoResetEvent(false);
ManualResetEvent pauseEvent = new ManualResetEvent(true);
public AutoResetEvent thisEvent = new AutoResetEvent(false);
#endregion

public RunConsole(string file, string paras, ref string details)
{
filename = file;
arg = paras;
this.details = details;
}


public void Stop()
{
t.Abort();
if (!p.HasExited) p.Kill();
}


public void Run()
{
t = new Thread(new ThreadStart(this.ExecuteCMD));
t.Start();

resetEvent.WaitOne();
Thread.Sleep(1000);
thisEvent.Set();
}

private void ExecuteCMD()
{
p = new Process();
try
{
p.StartInfo = new ProcessStartInfo();
p.StartInfo.FileName = filename;
p.StartInfo.Arguments = arg;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.BeginOutputReadLine();
p.BeginErrorReadLine();
}
catch
{ ;}
finally
{

resetEvent.Set();
t.Abort();
t = null;
if (!p.HasExited) p.Kill();
}

}

void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
lock (threadLock)
{
details += e.Data + "\r\n";
}
//throw new NotImplementedException();
}

void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
lock (threadLock)
{
details += e.Data + "\r\n";
}
//throw new NotImplementedException();
}
}




string output = String.Empty;
RunConsole runConsole = new RunConsole(fileName, paras, ref output);
runConsole.Run();


...全文
354 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
菜牛 2013-03-02
  • 打赏
  • 举报
回复
这个引用可不是地址,你这样是没法传出数据来的,用代理吧!
stonespace 2013-03-02
  • 打赏
  • 举报
回复
string虽然是引用类型,但它的内容是固定的,这个问题应该没法解决,

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧