Start new console application in new thread - c#

I am trying to start a .exe program from the build runner, but I don't want it to run in the teamcity console, I want a brand new console that runs independently from the current build. So far I have tried a few things without any success.
Tried with a powershell script:
$ErrorMessage = "POWERSHELL ERROR"
try {
Write-Output "About to start backend"
Start-Process Program.exe
Write-Output "Started backend"
} Catch {
Write-Output $ErrorMessage
exit(1)
}
Tried starting a new console with this command:
Start Program.exe
I'm starting to wonder if it's even possible.

Start-Process powershell -ArgumentList #("-NoExit", "-Command Start-Process cmd")

I got the answer from the teamcity forum, thought I might share it here though. My problem was caused to an issue with the build agent caused by windows.
Check out the full explanation here.
To solve the problem, you need to run the agent by running the command "agent.bat run" in the agent's bin folder under your installation directory.

Related

how to have a windows service self update?

All the solutions I can find on this topic are very old and none of them appear to answer my question...
I am trying to create a windows service that can self update (or auto update by some external trigger). In the past, I had created a windows service that was installed with InstallShield and we were able to update auto update the service in a hacky way by making the service write a batch script to the local machine and then run the batch script, which would stop the service, overwrite the service executable and other files with the new ones, and restart the service. This surprisingly worked.
However, I have updated the service to use InstallUtil.exe and this auto update script no longer works... I assume it's something to do with the way InstallShield handles the service install vs how InstallUtil does it... but I can only make guesses as I don't fully understand what each is doing to the registry.
Since I can't just overwrite the files and restart the service with the InstallUtil method, I thought I'd write a batch script that runs sc.exe to stop the service, uninstall it entirely, write the new files, install the new service files, and then start it... unfortunately, I can't seem to get sc.exe to run from a windows service automatically because it requires admin permissions... I tried to force it to self-elevate to admin using this snippet, but it doesn't appear to work as a service (it works fine if I run it from command line not as a service)
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
Does anyone know how I can cause a windows service to self update? I can look into updating to a .NET Core Worker service if there is some method of self update in .NET Core that I'm unaware of... Any ideas are much appreciated... it really shouldn't be this hard to accomplish...
For reference, here is the batch script I am currently using (ignore odd variables and such as I am dynamically replacing some of them, it works great when launched manually, just doesn't work when the service tries to run it):
#echo off
setlocal enableextensions enabledelayedexpansion
::make sure to run whole script as admin (this restarts scripts as admin if not already in admin mode)
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
pushd %networkDirectory%
::stop running service
for /F "tokens=3 delims=: " %%H in ('sc query %serviceName% ^| findstr " STATE"') do (
if /I "%%H" NEQ "STOPPED" (
net stop %serviceName%
if errorlevel 1 goto :stop
)
::delete existing service after stopping
sc delete %serviceName%
)
:: install updated service files
set "releaseDir=%networkDirectory%\Release"
set "programFilesCopyDir=%ProgramFiles%\{_companyDirectory}\%serviceName%\Release"
:: copy service Release dir to local system program files
xcopy "%releaseDir%" "%programFilesCopyDir%" /S /Y /Q
::execute the install
pushd "%programFilesCopyDir%"
CALL %serviceName%.exe --install
::start service
sc start %serviceName%
For anyone else trying to accomplish this that stumbles on this... I ended up finding a solution. I use the same script posted in my question above, but I wrote code to set up a scheduled task with Windows Task Scheduler. The scheduled task runs the above script as a one time scheduled task. This works like a charm.
I used this NuGet package to write the Task Scheduler code I needed:
https://www.nuget.org/packages/TaskScheduler/2.8.20?_src=template

Problem - 'dispatch' is not recognized as an internal or external command

When I tried to create dispatch-Model for my bot., I am getting following error - 'dispatch' is not recognized as an internal or external command, operable program or batch file.Before using dispatch command, I installed botdispatch using npm install -g botdispatch. But still I am getting this error.
Can you check your NPM folder to see if there is anything with dispatch there? Assuming you're on Windows, this is typically in AppData\Roaming\npm.
PowerShell:
dir $home\AppData\Roaming\npm
CMD:
dir %homepath%\AppData\Roaming\npm.
If dispatch (dispatch.cmd) is there, then it might be a pathing issue. Check your path variables to make sure that that path is there:
CMD:
echo %path%
PowerShell:
($env:path).Split(';')
So I was having this same issue, it turns out I needed to add the path of the NPM folder C:\Users\xxxxxx\AppData\Roaming\npm to my path for Powershell7.
$env:Path += ";C:\Users\xxxxxx\AppData\Roaming\npm"
Where xxxxxx is your userid. After adding this command to Powershell the npm modules began to work.
If you do not see this path by running this command below, then you need to add it with the one above.
($env:path).Split(';')
All of these should be ran inside of a PS7 window, I ran them with Administrator rights when I launched the Powershell 7 window.

VisualStudio 2017 runs .exe when building on external console

I'm trying to learn some C# and am currently using the internal console for outputs, but when it comes to keyboard inputs, I've read that it can not be done in the internal console on VS 2017 for Mac.
So I try to do it on external console, but all I get is this :
bash -c 'clear; cd "/Users/gb/Projects/reTest/reTest/bin/Debug";
"/Library/Frameworks/Mono.framework/Versions/5.8.1/bin/mono32"
--debug --debugger-agent=transport=dt_socket,address=127.0.0.1:56795 "/Users/gb/Projects/reTest/reTest/bin/Debug/reTest.exe" ; echo $? >
/var/folders/s_/sljf42_d01bdxlb5s_rwgsj80000gn/T/tmp563f24ea.tmp;
echo; read -p "Press any key to continue..." -n1; exit'; exit
I guess the console tries to execute a .exe application which is not possible in this case !
It's a console project by the way... I haven't found any solution for that so far.
Thks.
After further researches I found out that my bash was kind of corrupted somehow so I modified the .bash_profile file which had some extra inconvenient text..

C# command prompt commands not found

I am trying to call tpmvscmgr.exe located in C:\Windows\System32
from my c# application.
I found this code:
string strCmdText;
strCmdText = #"/C Robocopy C:\Users\Johannes\test1 C:\Users\Johannes\test2";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
here Run Command Prompt Commands
and it works good.
However when I try to change it to
strCmdText = "/C Tpmvscmgr.exe create /name tpmvsc /pin default /adminkey random /generate ";
This does not work.
In fact when I debug and look around in the command prompt opened by the code I can not find the tpmvscmgr.exe in windows/system32.
Im guessing it is opened as a different user or with other priviledges or something but can this be fixed?
I really would need to run a tpmvscmgr.exe command from code.
OK I figured something out.
In my visual studio c# project I had "Any CPU" marked for the project. Apparently this means the console opens as win32.
When I changed the c# project to x86 I could run tpmvscmgr.exe
Pretty tricky stuff.
C# cmd prompt cannot see telnet.exe

PowerShell Not Returning to command line after running "dotnet run ..."

I'm currently trying to build a PowerShell script that need to start my own C# application as Service and then after I need to check back the event log for error or other entry. But as soon as the dotnet run task is done, the process won't let me run another command, it seam to wait for the started process to stop.
Here a example of PowerShell script
CD C:\MyCSharpProject\Project\
dotnet run --debug <# script stop after this command is run, and the next command is not run r#>
notepad <# This line is never hit #>
How can I start my service and then run another command?
You can use Start-Process to run something without waiting.
Example:
Start-Process -FilePath 'dotnet' -WorkingDirectory 'C:\MyCSharpProject\Project' -ArgumentList 'run --debug'
notepad

Categories

Resources