Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by siddharthprodeveloper · May 07, 2021 at 10:04 PM · infinite

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();

``}

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eno-Khaon · May 11, 2021 at 10:01 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges