Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Running OMC from C# gives access error

Running OMC from C# gives access error

I'm trying to generate a simulation executable from a C# application. I do the following:


    create a temp folder
    copy the model to simulate (dcmotor example) in the temp folder
    create a mos-script that loads the modelica library, enters the temp folder, loads the model, enters the executable directory and calls simulate(...)
    execute omc using mos-script


When running my program, omc runs, but upon calling the simulate(...) statement, the following error is returned:

Code:

the process cannot access the file because it is being used by another process

Because of this, the executable is not generated (the other files are succesfully put into the executable folder). However, when I manually call the generated mos-script from the command line (from the same working directory as my program), it works flawlessly.

Does anybody have ideas what might cause the access error? My code is below (uploading attachments does not work):

Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace modelicasimulation
{
    public partial class Form1 : Form
    {
        String OPENMODELICAHOME;
        String baseDir;
        String execDir;

        public Form1()
        {
            setupPaths();
            File.Copy(Directory.GetCurrentDirectory() + "\\DCMotorCircuit.mo", baseDir + "\\DCMotorCircuit.mo", true);
            createMos();
            setupExec();
           
            InitializeComponent();
        }

        void setupPaths()
        {
            OPENMODELICAHOME = Environment.GetEnvironmentVariable("OPENMODELICAHOME");
            baseDir = "c:\\temp";
            DirectoryInfo execDirInfo = Directory.CreateDirectory(baseDir + "\\executable");
            execDir = execDirInfo.FullName;
        }

        void createMos()
        {
            StreamWriter file = File.CreateText(Directory.GetCurrentDirectory() + "\\startScript.mos");
            file.WriteLine("loadModel(Modelica);");
            baseDir = Regex.Replace(baseDir, @"\\", @"\\");
            execDir = Regex.Replace(execDir, @"\\", @"\\");
            file.WriteLine("cd(\"" + baseDir + "\");");
            file.WriteLine("loadFile(\"DCMotorCircuit.mo\");");
            file.WriteLine("cd(\"" + execDir + "\");");
            file.WriteLine("simulate(DCMotorCircuit,outputFormat=\"csv\");");
            file.Close();
        }
       
        void setupExec()
        {
            // Start the child process.
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = OPENMODELICAHOME + "bin\\omc";
            p.StartInfo.Arguments = "startScript.mos";
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            Console.WriteLine(p.StandardOutput.ReadToEnd());

            p.WaitForExit();       
        }
    }
}

Re: Running OMC from C# gives access error

I'm running the nightly build from 20 December, it's the latest one.

Re: Running OMC from C# gives access error

I found the error. Apparently, it is not possible to use stdout redirecting for omc when it tries to call the mingw compiler. The first few calls from my script (load modelica library, model and cd into executable folder) ran flawlessly, but the simulate command did not work because omc wants to redirect the stdout from mingw. Maybe a "double redirect" (mingw -> omc -> my program) is not possible???

At least, when I removed the code related to stdout redirection ("p.StartInfo.RedirectStandardOutput = true;", etc) it succeeds in compiling a simulation executable, whereas before it only generated the c code but not the executable.

There are 0 guests and 0 other users also viewing this topic
You are here: