How do I programmatically set MATLAB's priority in an engine application?

2 views (last 30 days)
How do I programmatically set MATLAB's priority in an engine application?
I am calling multiptle sessions of MATLAB from a C++ routine using the engSingleUser call to invoke each application having its own engine. I would like to have one of the engines run at a lower Windows Priority. I can do this externally from the CTL-ALT-DEL task list with no problem. I also know that there is a C language routine to cause the thread / process priority to be re-set which seems to imply that again resetting the priority should not be a problem.
However, the call to the engSingleUser passes back a handle to the engine, and not a Windows Process ID (maybe they are one and the same, but the documentation does not make it obvious). Is there any way that I can re-set the priority of a MATLAB session from a C language call? It is not obvious to me since the process ID seems to be hidden from me.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
engSingleUser and engOpen only establish an ActiveX channel between your application and an instance of MATLAB opened in automation server mode. It is not the same as creating a MATLAB process within your application. Thus you would need to make a system call in order to get the process id of MATLAB and then create a process handle from that. Once you have the process handle, you can use SetPriorityClass to set the priority to whatever you want.
For example:
1. Open MATLAB via engOpen
2. Find the MATLAB Window via FindWindow
3. Get the process id of the MATLAB Window via GetWindowThreadProcessId
4. Open the process and obtain a process handle
5. Check to make sure the value is not already set, and set it
6. Close the process handle.
PLEASE NOTE: FindWindow searched for the window title "Matlab Command Prompt" to get to the PID, so this example will only work if you have only one instance of MATLAB running.
Below is the code:
// priority.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "Windows.h"
#include "engine.h"
int main(int argc, char* argv[])
{
DWORD pid;
Engine *ep;
HANDLE hProcess;
BOOL bStatus;
// Open MATLAB
ep = engOpen(NULL);
// Find the Matlab Command Window and its pid
HWND hML = FindWindow(NULL, "Matlab Command Window");
GetWindowThreadProcessId(hML, &pid);
cout << "MATLAB PID: " << pid << endl;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION, false, pid);
if(hProcess) {
DWORD dwPriorityClass = GetPriorityClass(hProcess);
if(dwPriorityClass != BELOW_NORMAL_PRIORITY_CLASS) {
bStatus = SetPriorityClass(hProcess, BELOW_NORMAL_PRIORITY_CLASS);
}
}
CloseHandle(hProcess);
if(bStatus){
cout << "Successfully set MATLAB priority to below normal!" << endl;
} else {
cout << "MATLAB priority not set!" << endl;
}
return 0;
}

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!