0

プロセスを開始し、フォーム (winform) に表示するためにプロセスの出力を取得したいので、ProgressChanged イベントを作成しました。私の問題は、何らかの理由で、progressChanged 関数にアクセスできないことです。

私のコード:

BackgroundWorker bgWorker = null;
string pcapFilePathSniffer = "";

これは、プロセスを開始する私のクラスです:

public class Tshark
{
    public int _interfaceNumber;
    public string _pcapPath;
    Process _tsharkProcess;
    public int _packetsCount;
    public string _packet;        
    public string _tsharkPath = @"C:\Program Files\Wireshark\tshark.exe";
    public delegate void dlgPackProgress(int progress);
    public event dlgPackProgress evePacketProgress;

    public Tshark(int interfaceNumber, string pcapPath)
    {
        _interfaceNumber = interfaceNumber;
        _pcapPath = pcapPath;
    }

    public void startTheCapture()
    {
        try
        {
            List<string> list = new List<string>();
            _tsharkProcess = new Process();
            _tsharkProcess.StartInfo.FileName = @"C:\Program Files\Wireshark\tshark.exe";
            //_tsharkProcess.StartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -c " + int.MaxValue + " -w " + _pcapPath);
            _tsharkProcess.StartInfo.Arguments = string.Format("-i " + 2);
            _tsharkProcess.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
            _tsharkProcess.StartInfo.RedirectStandardOutput = true;
            _tsharkProcess.StartInfo.UseShellExecute = false;
            _tsharkProcess.StartInfo.CreateNoWindow = true;
            _tsharkProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            _tsharkProcess.Start();
            StreamReader myStreamReader = _tsharkProcess.StandardOutput;                

            while (!myStreamReader.EndOfStream)
            {
                evePacketProgress(_packetsCount);
                _packet = myStreamReader.ReadLine();
                list.Add(_packet);
                _packetsCount++;

                private void OnPacketProgress(int packet)
                {
                  var handler = evePacketProgress;
                  if (handler != null)
                  {
                     handler(packet);
                  }
                }
            }         

            _tsharkProcess.WaitForExit();                

        }
        catch (Exception e)
        {

        }
    }
}

処理を開始する開始ボタン:

private void btnStartCapture_Click(object sender, EventArgs e)
{
    bgWorker = new BackgroundWorker();
    bgWorker.WorkerReportsProgress = true;
    bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWSniffer_ProgressChanged);
    bgWorker.DoWork += new DoWorkEventHandler(
        (s3, e3) =>
        {
            tshark = new Tshark(2, pcapFilePathSniffer);
            tshark.startTheCapture();

            tshark.evePacketProgress += new Tshark.dlgPackProgress(
                (packet) =>
                {
                    bgWorker.ReportProgress(packet, tshark);
                });
        });

    bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
        (s3, e3) =>
        {

        });

    bgWorker.RunWorkerAsync();
}

ProgressChanged 関数:

private void bgWSniffer_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    tshark = e.UserState as Tshark;
    listBoxPacketsSnifferTab.Text += tshark._packet;
    lblPacketsReceived2.Text = tshark._packetsCount.ToString("#,##0");
}
4

1 に答える 1

0

から call にTshark.evePacketProgressイベントを発生させる必要があります。このようなもの:startTheCaptureBackgroudWorker.ReportProgress

private void OnPacketProgress(int packet)
{
  var handler = evePacketProgress;
  if (handler != null)
  {
     handler(packet);
  }
}

public void startTheCapture()
{
        // your code here

        while (!myStreamReader.EndOfStream)
        {
            _packet = myStreamReader.ReadLine();
            list.Add(_packet);
            OnPacketProgress(_packetsCount++);
        }

        // your code here
}
于 2012-09-26T06:38:59.593 に答える