question

VoyTec avatar image
0 Votes"
VoyTec asked NicoZhu-MSFT commented

UWP C# Faster checking.

Greeting dear M Comunnity.

I wanted to ask if there is faster way to check if a file contais anything else except "" just checking the file kb
I use this code for now:

 for (int x = 1; x < 32; x++)
             {
                 for (int l = 1; l < 321; l++)
                 {
                     string data = "";
                     if (x > 9) { data = $"{x}.01.2022"; }
                     if (x < 10) { data = $"0{x}.01.2022"; }
    
                     StorageFolder folderSP = await KnownFolders.DocumentsLibrary.GetFolderAsync("Św. Puszcza App");
                     try { StorageFolder folderDATA = await folderSP.GetFolderAsync(data); }
                     catch { Windows.Storage.StorageFolder folderDATA = await folderSP.CreateFolderAsync(data, CreationCollisionOption.ReplaceExisting); }
                     StorageFolder nowyfolderDATA = await folderSP.GetFolderAsync(data);
                     try { StorageFile plik = await nowyfolderDATA.GetFileAsync(l + ".dat"); }
                     catch { Windows.Storage.StorageFile plik = await nowyfolderDATA.CreateFileAsync(l + ".dat", CreationCollisionOption.ReplaceExisting); }
                     Windows.Storage.StorageFile plikPOKOJ = await nowyfolderDATA.GetFileAsync(l + ".dat");
                     string text = await Windows.Storage.FileIO.ReadTextAsync(plikPOKOJ);
    
                     pasek_ladowania.Value = pasek_ladowania.Value + 0.0493;
                     // 01
                     if (x == 1)
                     {
                         if ((l == 1) && (text == "")) { _2_os.Text = _2_os.Text + " " + l + ","; 
 ...
 }




windows-uwp
· 12
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

We suggest you use multiple tasks to process above, for more please refer to this document.


0 Votes 0 ·

@NicoZhu-MSFT
Thanks Nico but my brain overheats at this and I can't get it working.
I need to spend a lot of time on this then... :-)

0 Votes 0 ·

Ok, I could explain it detail, you used two levels foreach to query, for this scenario, we could make 32 tasks list to query at same time.

 private void Button_Click(object sender, RoutedEventArgs e)
 {
    
     int failed = 0;
     var tasks = new List<Task>();
     for (int x = 1; x < 32; x++)
     {
         tasks.Add(Task.Run(async () =>
         {
             for (int l = 1; l < 10; l++)
             {
                 try
                 {
                     // Simulate time-consuming operations
                     await Task.Delay(TimeSpan.FromSeconds(1));
                     Debug.WriteLine(l.ToString());
                 }
                 catch (Exception)
                 {
                     Interlocked.Increment(ref failed);
                 }
             }
    
         }));
     }
    
     Task t = Task.WhenAll(tasks);
    
     if (t.Status == TaskStatus.RanToCompletion)
         Debug.WriteLine("All task succeeded.");
     else if (t.Status == TaskStatus.Faulted)
         Debug.WriteLine("task attempts failed", failed);
 }


1 Vote 1 ·
Show more comments

1 Answer

VoyTec avatar image
0 Votes"
VoyTec answered NicoZhu-MSFT commented

@NicoZhu-MSFT

 private async void check()
 {
 loading_bar.Value = 0;
 var tasks = new List<Task>();
 for (int days = 1; days < 32; days++)
 {
 tasks.Add(Task.Run(async () =>
 {
 await Task.Delay(TimeSpan.FromSeconds(0.01));
 for (int room = 1; room < 321; room++)
 {
 loading_bar.Value = loading_bar.Value + 0.0493;
 string date = "";
 if (x > 9) { date = $"{x}.01.2022"; }
 if (x < 10) { date = $"0{x}.01.2022"; }
 StorageFolder folderApp = await KnownFolders.DocumentsLibrary.GetFolderAsync("My Application");
 try { StorageFolder folderDATE = await folderApp.GetFolderAsync(data); }
 catch { Windows.Storage.StorageFolder folderDATE = await folderApp.CreateFolderAsync(data, CreationCollisionOption.ReplaceExisting); }
 StorageFolder newfolderDATE = await folderApp.GetFolderAsync(data);
 try { StorageFile file = await newfolderDATE.GetFileAsync(room + ".dat"); }
 catch { Windows.Storage.StorageFile file = await newfolderDATE.CreateFileAsync(room + ".dat", CreationCollisionOption.ReplaceExisting); }
 Windows.Storage.StorageFile fileROOM = await newfolderDATE.GetFileAsync(room + ".dat");
 string text = await Windows.Storage.FileIO.ReadTextAsync(fileROOM);
 if (days == 1)
 {
 if ((room == 1) && (text == "")) { room4two.Text = room4two.Text + " " + room + ","; }
 if ((room == 2) && (text == "")) { room4two.Text = room4two.Text + " " + room + ","; }
 etc...blah blah blah.,..
 }
 }
 }));
 }  
 Task t = Task.WhenAll(tasks);
 loading_bar.Visibility = Visibility.Collapsed;
 }

App is for calculating and showing available rooms from checking all room files from saved data.
Problem: not calculating or not showing anything.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I checked your code and I found that you wrote a lot of hard code to update the ui interface. But the process of updating the ui should be done in the UI thread, so the above multiple tasks are not suitable for your scenario.

1 Vote 1 ·

@NicoZhu-MSFT many thanks. My excuse is just: I am a beginner, I learn on my own - I studied diffrent things.

I don't know what is UI thread but I'll check the internet... but my question is: is there a way out to show so many files in just few seconds. I mean inside: "Rozpiska..." button on the second page.

Is there a way I can call you my angel and you blew my problems away? Please!

0 Votes 0 ·

Io speed was limited with your physical device and file access api. And for UI thread problem you could refer to Keep the UI thread responsive document


0 Votes 0 ·