Answers for "Multi threaded file reading"

C#
0

Multi threaded file reading

using System.Threading.Tasks; 
static async Task<string> ReadTextFileInBackgroundThread ( string filePath ) 
{     
  if( !System.IO.File.Exists(filePath) )      
  {             
    Debug.LogWarning($"This file path is invalid tho: '{filePath}'");
    return string.Empty;     
  }     // we're still on unity thread here
  return await Task.Run(         
    () =>         
                        
    {             // we're on worker thread, so exciting!
      return System.IO.File.ReadAllText(filePath);         
    }     );     // back on unity thread } 
  async void Start () 
  {     
    txtText.text = 
      await ReadTextFileInBackgroundThread( @"c:\hentai\wishlist.txt" ); 
  }
Posted by: Guest on August-11-2021

C# Answers by Framework

Browse Popular Code Answers by Language