并非所有代码路径都在c#中返回错误

时间:2014-09-04 10:40:46

标签: c# wrapper

我希望将实时信号从情绪传递到八度音 我试着为octave写一个c#包装器。这是代码。

namespace LibSharpTave {
    public class Octave {
        Process OctaveProcess { get; set; }
        private string OctaveEchoString { get; set; }
        public Octave(string PathToOctaveBinaries) {
            StartOctave(PathToOctaveBinaries, false);
        }

        public Octave(string PathToOctaveBinaries, bool CreateWindow) {
            StartOctave(PathToOctaveBinaries, CreateWindow);
        }

        string ptob;
        bool cw;
        private void StartOctave(string PathToOctaveBinaries, bool CreateWindow) {
            ptob = PathToOctaveBinaries;
            cw = CreateWindow;
            this.OctaveEchoString = Guid.NewGuid().ToString();
            OctaveProcess = new Process();
            ProcessStartInfo pi = new ProcessStartInfo();
            if (PathToOctaveBinaries[PathToOctaveBinaries.Length - 1] != '\\')
                PathToOctaveBinaries = PathToOctaveBinaries + "\\";
            pi.FileName = PathToOctaveBinaries + "octave.exe";
            pi.RedirectStandardInput = true;
            pi.RedirectStandardOutput = true;
            pi.RedirectStandardError = true;
            pi.UseShellExecute = false;
            pi.CreateNoWindow = !CreateWindow;
            pi.Verb = "open";
            //
            pi.WorkingDirectory = ".";
            OctaveProcess.StartInfo = pi;
            OctaveProcess.Start();
            OctaveProcess.OutputDataReceived += new DataReceivedEventHandler(OctaveProcess_OutputDataReceived);
            OctaveProcess.BeginOutputReadLine();
            OctaveEntryText = ExecuteCommand(null);

            //OctaveProcess.OutputDataReceived += new DataReceivedEventHandler(Oc


        }





        public double GetScalar(string scalar) {
            string rasp = ExecuteCommand(scalar, 30000);
            string val = rasp.Substring(rasp.LastIndexOf("\\") + 1).Trim();
            return double.Parse(val);
        }

        public double[] GetVector(string vector) {
            string rasp = ExecuteCommand(vector, 30000);
            string[] lines = rasp.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            int i = 0;
            //catam urmatorul entry
            List<double> data = new List<double>();
            while (i != lines.Length) {
                string line = lines[i];
                if (line.Contains("through") || line.Contains("and")) {
                    i++;
                    line = lines[i];
                    string[] dataS = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int k = 0; k < dataS.Length; k++) {
                        data.Add(double.Parse(dataS[k]));
                    }
                }
                i++;
            }
            //caz special in care a pus toate rezultatele pe o singura linie
            if (data.Count == 0) {
                string[] dataS = lines[lines.Length - 1].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (dataS.Length != 0)
                    for (int k = 0; k < dataS.Length; k++) {
                        data.Add(double.Parse(dataS[k]));
                    }
            }
            return data.ToArray();
        }

        public double[][] GetMatrix(string matrix) {
            //string rasp = ExecuteCommand(matrix);
            //aflam numarul de randuri
            string rasp = ExecuteCommand(matrix + "(:,1)", 30000);
            string[] lines = rasp.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            double[][] mat = new double[lines.Length - 1][];
            for (int i = 0; i < mat.Length; i++) {
                mat[i] = GetVector(matrix + "(" + (i + 1) + ",:)");
            }
            return mat;
        }

        StringBuilder SharedBuilder = new StringBuilder();
        ManualResetEvent OctaveDoneEvent = new ManualResetEvent(false);
        public string OctaveEntryText { get; internal set; }

        public void WorkThread(object o) {
            string command = (string)o;
            SharedBuilder.Clear();
            OctaveDoneEvent.Reset();
            if (command != null) {
                OctaveProcess.StandardInput.WriteLine(command);
            }
            //ca sa avem referinta pentru output
            OctaveProcess.StandardInput.WriteLine("\"" + OctaveEchoString + "\"");
            OctaveDoneEvent.WaitOne();
        }
        public string ExecuteCommand(string command, int timeout) {
            if (OctaveProcess.HasExited) {
                StartOctave(ptob, cw);
                if (OctaveRestarted != null) OctaveRestarted(this, EventArgs.Empty);
            }
            exitError = false;

            Thread tmp = new Thread(new ParameterizedThreadStart(WorkThread));
            tmp.Start(command);

            if (!tmp.Join(timeout)) {
                tmp.Abort();
                throw new Exception("Octave timeout");
            }
            if (exitError) {
                throw new Exception(errorMessage);
            }
            return SharedBuilder.ToString();
        }
        public string ExecuteCommand(string command) {
         //   Thread tmp = new Thread(new ParameterizedThreadStart(WorkThread));
         //   tmp.Start(command);

        //    tmp.Join();

          //  return SharedBuilder.ToString();

            if (OctaveProcess.HasExited)
            {
                OctaveProcess.Start();
            }
            SharedBuilder.Clear();
            if (command != null)
            {
                OctaveProcess.StandardInput.WriteLine(command);
                OctaveDoneEvent.Reset();
                OctaveDoneEvent.WaitOne();
                return SharedBuilder.ToString();
            }

            Octave octave = new Octave(@"c:\software\Octave-3.6.4",false);
            octave.ExecuteCommand("a=[1,2;3,4];");
            octave.ExecuteCommand("result=a;");
            double[][] m = octave.GetMatrix("result");


        }
        bool exitError = false;
        string errorMessage = null;
        void OctaveProcess_OutputDataReceived(object sender, DataReceivedEventArgs e) {
            if (e.Data == null) {
                SharedBuilder.Clear();
                //errorMessage = OctaveProcess.StandardError.ReadToEnd();
                SharedBuilder.Append("Octave has exited with the following error message: \r\n" + errorMessage);
                //exitError = true;
                OctaveDoneEvent.Set();
                return;
            }
            if (e.Data.Trim() == "ans = " + OctaveEchoString)
                OctaveDoneEvent.Set();
            else
                SharedBuilder.Append(e.Data + "\r\n");
        }
        public event OctaveRestartedEventHandler OctaveRestarted;
        public delegate void OctaveRestartedEventHandler(object sender, EventArgs e);
    }

    //custom class
   // void OctaveProcess_OutputDataReceived (object sender, DataReceivedeEventArgs e)
    /*{
        if (e.data == null)
        {
            SharedBuilder.Clear();
            SharedBuilder.Append("Octave has exited with the following error message: \r\n" + OctaveProcess.StandardError.ReadToEnd());
            OctaveDoneEvent.Set();
            return;
        }
        if (e.data.Trim == "ans =" + OctaveEchoString())
            OctaveDoneEvent.set();
        else
            SharedBuilder.Append(e.Data + "\r\n");

    }*/




}

它返回错误:“并非所有代码路径都返回一个值” 我该如何解决这个错误?

3 个答案:

答案 0 :(得分:2)

您的ExecuteCommand函数应该返回一个字符串,但不是。这是ExecuteCommand重载,它接受一个参数。

答案 1 :(得分:0)

此函数的返回类型为string,但if (command != null)语句为false时不返回任何内容 -

     public string ExecuteCommand(string command) {
     //   Thread tmp = new Thread(new ParameterizedThreadStart(WorkThread));
     //   tmp.Start(command);

    //    tmp.Join();

      //  return SharedBuilder.ToString();

        if (OctaveProcess.HasExited)
        {
            OctaveProcess.Start();
        }
        SharedBuilder.Clear();
        if (command != null)
        {
            OctaveProcess.StandardInput.WriteLine(command);
            OctaveDoneEvent.Reset();
            OctaveDoneEvent.WaitOne();
            return SharedBuilder.ToString();
        }

        Octave octave = new Octave(@"c:\software\Octave-3.6.4",false);
        octave.ExecuteCommand("a=[1,2;3,4];");
        octave.ExecuteCommand("result=a;");
        double[][] m = octave.GetMatrix("result");

        //**** The Error is here *****
        //return a string here 
    }

在提及部分返回一个字符串。我在评论中突出显示为 - //**** The Error is here *****

答案 2 :(得分:0)

你的

public string ExecuteCommand(string command) {
即使您指定了返回类型字符串,

也没有return语句。

返回一些字符串或使其返回类型为void,如下所示:

public void ExecuteCommand(string command) {