running matlab m-file from c# console application no longer working - c#

I have a c# console application.
The program is very simple. It simply opens a Matlab application & specifies the directory of the m-file I wish to run, supplying one parameter (code is below). The code in the m-file basically uploads some data to a datbase. After this an excel report is created from the data uploaded to the database.
I have this task scheduled to run at 4am. The code worked fine for the first two months. However (without any changes I'm aware of) the code is not working. I come in the morning to see that it has opened an instance of matlab & it has changed the matlab directory but it does not appeared to have run the matlab function. Its strange as the c# code then runs the excel code afterwards which reprots there is no data in the database. When I come in and run the code manually though the code works fine. I don't know what is happening or how to find out what is going on?
In windows scheduled the task has the message "the operation completed successfully. (0x0)
public void CalculateFundDrift()
{
try
{
// create matlab instance
_matlab = new MLApp.MLApp();
// change to the directory where the function is located
_matlab.Execute(#"cd c:\my_directory\");
// define the output
object result = null;
// call the matlab function upload_data
_matlab.Feval("my_func_name", 0, out result, "my_para");
// quit matlab
Console.WriteLine(Environment.NewLine + "Closing Matlab");
_matlab.Quit();
}
catch(Exception ex)
{
_matlab.Quit();
throw;
}
}
Update
One thing I do no notice when running the code is that it opens a matlab command window (looks more like a text file). In the morning when I come in its an actual matlab application that has been left open.

Related

Error With XmlDocument.load causing ntdll.dll error on exit of aplication

So I have a problem with the xmldocument.load function MSND Link . Specifically, it runs fine in the code but upon closing the program it causes an error in ntdll.dll.
Picture of Error
However, if I remove this function from the program I’m writing and run it separately it works without error. Also if I run the rest of my program with some dummy input it runs and closes without error as well.
To be clear this is running as a windows forms application with these functions being called from a google API library I’m putting together.
public static string Test()
{
XmlDocument XML = new XmlDocument();
XML.Load(#"https://maps.googleapis.com/maps/api/distancematrix/xml?units=imperial&origins=Vancouver+BC|Seattle&destinations=San+Francisco|Vancouver+BC&key=AIzaSyA_j0g3zKKhosZz9uEXwCWd2fuv3WCwHPk");
string S = XML.ChildNodes[1].ChildNodes[3].ChildNodes[0].ChildNodes[0].InnerText;
return S;
}
How would I Resolve this error message?
Sense it dose not appear to be causing issues with the program I would also accept a way to suppress the error message from popping up after the program closes. Though obviously I would prefer to fix the issue than just cover it up.
Edit: 4/18/2016
I just made one of my first Alpha testing builds of the program and when the program is being run from outside of Visual studio after using WIX to create an installer and install it it is not throwing the error. still acting up from within visual studio though very odd.

How to quit MATLAB process using C# and COM?

I have a this code that calls a MATLAB function (displayFigure.m) from a C# client. Each time I update the .m file, the changes are not reflected when the C# program is rerun.
public void CallMLfunc(){
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Feval("displayFigure", 0, out result, input);
matlab.Quit();
}
Tried to terminate the MATLAB.exe process using
matlab.execute("quit;");
and will get the following an unhandled exception: System.Runtime.InteropServices.COMException.
After force quitting the program I rerun it again an displayFigure.m (version 2) is reflected. Is there another to quit the MATLAB process?
There's a MATLAB.exe in the process list for the COM object. Sometimes there's a visible window you can close. Sometimes there isn't. You may have more than one MATLAB.exe, especially if you are debugging and the MATLAB editor is open, you'll have that instance running as well. The matlab.Quit(); should terminate the MATLAB.exe associated with the COM object, but if there is an Exception during the matlab.Feval(), or breakpoint, etc., it might not execute. When I have that problem, I terminate the MATLAB.exe with the highest PID.

terminal command not working in c# progra

I call a console program in a c# application but it's not working.
I'm trying to call pdf toolkit VIA c#.
When I run pdftk using command prompt, the output file (new.pdf) is created. When c# calls pdftk it doesn't error, but it doesn't work.
Am I calling it wrong in C# or something?
C# Call: (Not working)
System.Diagnostics.Process.Start(pdfTKPath, pdftkArgs);
Console.Write(pdfTKPath + pdftkArgs);
Console.ReadLine();
// Here's the string that the console outputs.
// pdftk.exe C:\Users\name\Desktop\assignment1\assignment1\data\temp\test_cropped.pdf stamp C:\Users\name\Desktop\assignment1\assignment1\data\temp\test_footer.pdf output C:\Users\name\Desktop\assignment1\assignment1\data\temp\new.pdf
CMD Prompt Call: (Works Fine)
C:\Users\name>pdftk.exe c:\Users\name\Desktop\assignment1\assignment1\data\temp\test_cropped.pdf stamp c:\Users\name\Desktop\assignment1\assignment1\data\temp\test_footer.pdf output c:\Users\name\Desktop\assignment1\assignment1\data\temp\new.pdf
C:\Users\name>pdftk.exe
is wrong
Use the following one:
C:\Users\name\pdftk.exe
Also, did you escape
\
symbol? It is required if you are hardcoding path in the app
Maybe pdftk.exe is not in the path. Try to call the exe specifying it's full path.
firstly I would check if aprocess is created and if it has an error
var processo=System.Diagnostics.Process.Start(pdfTKPath, pdftkArgs);
Console.Write("process created: "+processo.ExitCode);
The reason that this wasn't working is that the processing that was happening with the prior method wasn't complete.
The method before this one uses ghost script to create a cropped PDF file. This method was evoked without ghost script completing the work. Therefore this method didn't think the files existed yet (because they didn't). I set this method to wait 2 second before starting and it works fine. This allowed the previous method time to complete.

C#.net Windows Service project - Same project different location not working

I have written a windows service code in C#.NET containing 2 threads running simultaneously which is working as per expected.
Now, I copied the same project to different location, built the solution and copied the service to the destination VM machine (Windows 7). But one of the threads stuck up at a function call i.e. once the execution reaches the function call, it gets stuck up and this thread just wont respond.. no exception nothing. Now, try to stop this service, it will take huge time, wont stop sometimes and will leave a stray process in the task manager which will be vanished after few minutes.
Same thing happened when I copied the solution to some another location.
Now, again, I use my original location, copy this service to destination VM machine and voila.. everything is working perfectly.
We tried calling some other function from the same class, it is getting called. But not the one which we want to.
And this happens only on my laptop. On my colleagues laptop, it works fine.
Anybody came across this issue??
Please help.
The code is as below -
The calling function is a normal function from a class.
The function which is getting called is inside a class which is as follows -
class A {
// Some variables
public A(parameter a) // constructor
{ }
void function1(object A)
{ }
bool function2(B b) // this is the calling class
{
Log("some message"); // this is 1st line of the function.
... ...
}
}
calling code is -
A a = new A(param);
a.function1(obj); // Works
a.function2(this); // stuck up at this call.. do not even print log message which is at the entry point. even if we change or remove parameter altogether.. just have some issue with function
Just reinstalling Visual Studio worked for me.
Thanks!

How can I launch a program from memory in C#?

I have some UI application that lives in the user's task bar that is written in C#. The EXE for the tool is checked in to our source control system on a number of projects that use it so we are able to update the version they run with by checking in updated EXE.
The problem is that when the users get the latest revision of the exe, the program is often running, and the sync fails on their machine. I want to fix it so the program doesn't lock the exe and any dependent DLL's when it runs so they can sync without having to shut down the program.
Currently, I have a program that takes an executable as a parameter and will launch it from memory by reading the assembly contents into memory ahead of time. Unfortunetly, this totally fails when it comes to the DLL's that the program requires.
The code I have right now looks something like this:
public class ExecuteFromMemory
{
public static void Main(string[] args)
{
//Figure out the name of the EXE to launch and the arguments to forward to it
string fileName = args[0];
string[] realArgs = new string[args.Length - 1];
Array.Copy(args, 1, realArgs, 0, args.Length - 1);
//Read the assembly from the disk
byte[] binary = File.ReadAllBytes(fileName);
//Execute the loaded assembly using reflection
Assembly memoryAssembly = null;
try
{
memoryAssembly = Assembly.Load(binary);
}
catch (Exception ex)
{
//Print error message and exit
}
MethodInfo method = memoryAssembly.EntryPoint;
if (method != null && method.IsStatic)
{
try
{
method.Invoke(null, new object[] { realArgs });
}
catch(Exception ex)
{
//Print error message and exit
}
}
else
{
//Print error message and exit
}
}
}
My question is, am I doing something totally stupid? Is there a better way to handle this? If not, what should I do to support handling external dependencies?
For example, the above code fails to load any dependent files if you try to run 'Foo.exe' that uses functions from 'Bar.dll', the 'Foo.exe' will be overwriteable, but 'Bar.dll' is still locked and can't be overwritten.
I tried getting the list of referenced assemblies from the 'GetReferencedAssemblies()' method on the loaded assmebly, but that doesn't seem to give any indication where the assemblies should be loaded from... Do I need to search for them myself? If so, what's the best way to do this?
It seems like other people might have come across this before, and I don't want to re-invent the wheel.
-
Update:
The EXE is checked in because thats how we distribute our in-house tools to the teams that use them. Its not optimal for this use-case, but I don't have the opportunity to change that policy.
Disclaimer: I don't use Windows, though I am familiar with its strange way of locking things.
In order to update your application while it is running, you'll likely need to have two processes: The executable itself, and an update “helper” application that will finish the update process. Let's say that your application is ProcessA.exe and your update helper is Updater.exe. Your main program will download a new copy of the executable, saving it with a random name. Then you run your updater program, which watches for the termination of your current process. When your process terminates, it displays a quick window showing the status of the update, moving the new executable into the place of the old one, and then restarting that program.
It'd be more elegant to be able to emulate POSIX filesystem semantics and be able to delete the currently-running process disk image and replace it with a new file, but I don't know if that is even possible on Windows. On a POSIX system, you can delete an in-use file and it won't actually be deleted until any remaining file handles are closed, though you can then reuse the filename.
You might want to check out an article written at CodeProject that talks about this. It also has a follow-up article.
Good luck!
Does the program need to keep running while updating?
Typically to update a program which is running you would copy over any of the files that are to be replaced to a temporary folder. Then shut down the old instance, delete it and move the new files over to the correct locations then re-launch it.
This allows for minimal down time of the application since the longest part is usually the copy and the file move is very fast if the temporary folder is on the same logical drive.
Although Michael's answer is one way of doing this, there are tools out there that are explicitly for managing what's installed on the desktop.
What you are doing with the exe being checked into source control is not normal. If you have a windows domain controller, you can use Group Policy to push programs down to the client. Alternatively, you could use something like Altiris to handle it.
If you must continue the way you are going then you have two options. One, using a helper / loader app which does a version check on launch. This is similar to how firefox works.
The second way is to build a helper service that sits in memory and polls every so often for updates. This is how Google Chrome, Adobe, etc work.

Categories

Resources