Click here to Skip to main content
16,022,054 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<pre>public static bool DisplayOriginalFiles(List<string> files)
{
	string fileName = string.Empty;
	string filePath = string.Empty;
	// Loop through Files and display data while building Observable Collection
	foreach (string file in files)
	{
					{// Get the original FileName
			fileName=Path.GetFileName(file);

			// Separate Path from FileName
			filePath=file.Replace(fileName,"");

			// Display and Load Rename Observable Collection with Original File Name
			Gbl.rO.Add(new RenameObservable(fileName,"","Orig Loaded",true,true,filePath,file));
		}	
	}
	return true;
}


I execute with the following:
C#
// Display Original Files to User
Common.DisplayOriginalFiles(selectedFiles);


The reason I want to make it async is because I want each renameobservable to come on the screen up one at a time rather than all at once.

Or is there another way to make this happen?

What I have tried:

Tried putting await in but will not allow me to do that.
Posted

There's nothing in your code that lends itself to being async. If you want the items to appear one at a time, then you'll need to add a delay between adding them - for example:
C#
public static async Task<bool> DisplayOriginalFiles(List<string> files)
{
    TimeSpan delayTime = TimeSpan.FromSeconds(0.5);
    
    foreach (string file in files)
    {
        string fileName = Path.GetFileName(file);
        string filePath = Path.GetDirectoryName(file);
        Gbl.rO.Add(new RenameObservable(fileName, "", "Orig Loaded", true, true, filePath, file));
        
        // Pause before adding the next item:
        await Task.Delay(delayTime);
    }
    
	return true;
}

NB: Your method of getting the file path is dangerously wrong. The file name could appear earlier in the path, which would result in you looking in the wrong folder. Eg: C:\passwords.txt\Windows\System32\passwords.txt would yield a path of C:\\Windows\System32\, which is just wrong.

Use the Path.GetDirectoryName to extract the path, rather than trying to roll your own.
 
Share this answer
 

The problem with Observables is that they push the data at the comsumer. If you want control the receipt of the data then a pull based system is probably better. AsyncStreams are ideal for this purpose as the async process is pull based. You have the ability to process an item from the stream before other items become available. Something along these lines.


C#
  internal class Program
 {
  public  static async IAsyncEnumerable <string? > ReadLines(string path)
  {
    using StreamReader stream = File.OpenText(path);
    while (stream.EndOfStream is false)
     {
      //return the line as soon as it is available
      yield return await stream.ReadLineAsync();
      Console.WriteLine("Next stream request");
      
     }
   }  
 

    static async Task Main()
  {
    await foreach (var line in ReadLines(@"C:\Temp\Test.txt"))
    {
      //do something with the line
      Console.WriteLine(line);
      Console.WriteLine("zzzzz");
      await Task.Delay(4000);

    }
    
    Console.ReadLine();
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900