how to start only one 3dsmax instance pertime - instance

Whenever a command like "3dsmax -silent -U PythonHost F:/code/3dmax/dsmax_snapshot.py" is executed, a new instance is started。I want to execute the script using only the opened instance each time

Simplest way is to call registerOLEInterface to expose python.ExecuteFile (and/or python.Execute) from the script you pass on the commandline, then you can use win32com to execute python commands in that running max instance. For this to work, you have to first register the OLE server. Once the keys are added to the registry, you can expose the needed functions and call them from outside of max.

Related

CodeFirst migrations: How to run a database script (C# or SQL) after completion of "update-database" automatically?

I am using EF.Core with code-first migrations to update a SQL database.
Whenever I added migrations (Package Manager Console: add-migration), I am updating the database with the well-known update-database command. Is there a way to run a SQL batch script after completion of this command automatically (like you can do with post-build events in Visual Studio) ?
That script could backup the database or do other tasks (like setting up user roles etc).
I don't want to modify existing migrations to do this, I know you can add something like
protected override void Up(MigrationBuilder migrationBuilder)
{
var sqlFile = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
#"Migrations\20200701103006_MySQLBatch_Up.sql");
var sqlCommands = System.IO.File.ReadAllText(sqlFile);
migrationBuilder.Sql(sqlCommands);
// ...
}
But I don't want to do that, because this way you would have to do it every time you're adding a new migration.
Is there an event or a method that one can override to achieve it? Or something that can be triggered, like a script?
Effectively, what I want to achieve is having a script or method invoking:
update-database
pg_dump -h localhost -U postgres -p 5432 myDatabase > C:\Temp\myDatabase.sql
Note: update-database runs in the Package Manager Context, pg_dump runs in the command shell (cmd.exe) - hence, you cannot run update-database in a .cmd or .bat script directly.
Two options I can think of
Simpler: Why not have a simple batch file that executes both commands in sequence, and then you can run the batch file instead of Update-Database command? You could even get the most parameters from a standard project config file and that way you could use the same script in multiple projects without changing anything except the project config file. This way you make sure your additional scripts runs in response to Update-Database command when it actually is intended
If this is for a Powershell session that may involve multiple "update-commands" in a dynamic manner and you don't want the above approach, then you can try subscribing to Powershell engine Exiting event (i.e. [System.Management.Automation.PsEngineEvent]::Exiting) and execute a script in response automatically via its -Action parameter anytime Update-Database is executed (but only if in the same Powershell session).
See Register-EngineEvent command for details and examples
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/register-engineevent?view=powershell-7
Inside your action script, you can get the event details (like $Events[0].MessageData) and search for the text " Database-Update ", then execute your your desired commands in response. This can get erroneous if " Database-Update " text appears in any unintended context in the session.
You can review Get-Event command details and examples here
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-event?view=powershell-7
You can setup a persistent session with local or remote computer via New-PSSession session command so that commans executed in multiple files can be considered for the event subscriber.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-pssession?view=powershell-7
More about different type of Powershell sessions
https://www.sconstantinou.com/windows-powershell-sessions-pssessions/
I can't seem to find anything in the documentation to do what you want in the way that you want.
However, an alternative solution I thought of is to, instead of running the scripts from the commandline or as part of the migration class, you run them on Startup as part of your automatic migration.
So you can do something like this:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext dataContext)
{
// migrate changes on startup
dataContext.Database.Migrate();
foreach(var file in GetMigrationFiles())//you can write the code that searches a folder for SQL scripts to execute
{
dataContext.Database.ExecuteSqlRaw(File.ReadAllText(file));
}
}
Make sure to order your GetMigrationFiles() properly.
The problem here is that you'll need to also write SQL to roll back your custom SQL in the event that you'd want to roll back. Put those in a different folder though.

How allow only one python code process to run if same is executed at the same time

if I have two or more running python console applications at the same time of same application, but executed several times by hand or any other way.
Is there any method from python code itself to stop all extra processes, close console window and keep running only one
The solution I would use would be to have a lockfile created in the tmp directory.
The first instance would start, check for the existence of the file, create the file since it is not there, then run; the following instances will start, check for the existence of the file, then quit since it's there. The original instance would remove the lockfile as its last instruction. NOTE: If the app runs into an error and does not execute the instruction to remove the lockfile, you would need to manually remove it else the app will always see the file.
I've seen on other threads that some suggest using the ps command and look for your app's name, which would work; however, if your app will ever run on Windows, you would need to use tasklist.

Get new command line args while running

i'm running a vb.net app that uses command line args and store them in variables, and, for example, put them in a textbox. I want another external app to pass data every minute to my app by calling my app with the data as argument.
I know I can get the command line arguments using GetCommandLineArgs. But can I get 'new' args while running, whithout restarting the app?
Example:
- I start the app, using "myapp.exe argument1". This shows "argument1" in the textbox
- Next, I run "myapp.exe argument2" (while myapp.exe is still running), and so myapp should just keep on running, but now display "argument2"
Is this possible using command line args, or do I need to use another approach?
Thanks!
But can I get 'new' args while running, whithout restarting the app?
No, command line arguments are only set once during the lifetime of a running application. You will need to use another approach to pass the data to your application (WCF, sockets, database, files, remoting, named pipes, ...).

how to call variables from a running script to a different script powershell

I want a script to get a variable from a different script which are both running on the system. Is that possible?
I have two scripts running on the system, and I want one script to pull a user-defined variable instead of asking the user to input the data twice
Assuming both scripts are running concurrently, MSMQ would be one option:
What's the best way to pass values to a running/background script block?
Global Variables are only "Global" in the context of scopes in the current session. They aren't visible to a script running is a different session.
Write it to a text file and have the other script read it. You can load up the text into a variable, then execute it as code using iex (Invoke-Expression).

Is it possible to set Powershell profile code to run after every script?

I have set up a Powershell profile to run at Powershell startup.
Is it possible to configure powershell to call the profile after every .ps1 script so I don't have to call .$profile at the end of each script?
I don't know of a way to do what you're asking without hacking at PowerShell internals (not even sure then). I would just do as you suggest, put this stuff in a separate script and then create a simple alias for it like a or s e.g.:
New-Alias s c:\users\john\bin\reset.ps1
You could put it in your prompt function to reload after every command:
function prompt
{
. $profile
}
I don't know of any event you can hook into that would fire after a script is run.