- Home /
why I cant able to run process at background in c# in Upadate ?
I was trying to get value from powershell by using standardoutput.readline() but if the power shell code was an infinite loop than unity will gets hang. Is there any way to get the power shell value without hang, NOTE: i want the value continuously in update. I had already tried to print in notepad and get in in unity but it much time to reload. I want the value form power shell while game is running . Is there any solution for me ? Thanks
public void mufunc(){
process1.StartInfo = new ProcessStartInfo( "powershell.exe","Get-item -path .\\siddharth.txt | Get-Content -Tail 1"){
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
}};
void Update () { if(Input.GetKeyDown("s")){ process1.start(); process1.WaintForExit();
``}
}
Answer by Llama_w_2Ls · May 08, 2021 at 08:14 AM
You can use async functions to run the code on a new thread, then contact the main thread again, once the process has completed. For example:
void Update ()
{
if (Input.GetKeyDown("s"))
{
StartMyProcess();
}
}
Then your 'StartMyProcess' function should look like this:
async void StartMyProcess()
{
await Task.Run(() =>
{
process1.StartInfo = new ProcessStartInfo( "powershell.exe","Get-item -path .\\siddharth.txt | Get-Content -Tail 1")
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
}
});
// Process has started. Do what you want with process1
}
Anything you want to run in a background thread, you should run inside of the await block, but any code you want to execute back on the main thread once the thread has finished executing, you can run outside the await block, underneath. Hope that helps. @siddharthprodeveloper
Answer by siddharthprodeveloper · May 11, 2021 at 05:44 AM
It says error cs0103: 'Task' does not exist in the the current context @Llama_w_2Ls
https://www.google.com/search?q=c%23+task+run
The first search result indicates that Task.Run is part of the namespace System.Threading.Tasks. Therefore, at the start of the script, you would include it:
using System.Threading.Tasks;
Your answer
Follow this Question
Related Questions
BigInteger/BigRational 2 Answers
Trouble spawning endless platform 0 Answers
How to use a texture to tile infinitely as a background. 0 Answers
Infinite Terrain Noise 1 Answer
how do i create a "randomized infinite grid"? details here.. 2 Answers