Click here to Skip to main content
14,391,232 members
Rate this:
Please Sign up or sign in to vote.
See more:
static    public void getSize(string pathLocation)

    DirectoryInfo dInfo = new DirectoryInfo(pathLocation);
    sizeOfDirKb = DirectorySize(dInfo, true) / (1024);

    adminProcess = new System.Diagnostics.ProcessStartInfo();

    TotalSizeOfFolderByte = sizeOfDirKb / (1024 * 1024);

    if (TotalSizeOfFolderByte < 100)
        TotalSizeOfFolder = Math.Floor(TotalSizeOfFolderByte * 100) / 100;

    else if (TotalSizeOfFolderByte >= 100)
        TotalSizeOfFolder = Math.Floor(TotalSizeOfFolderByte);

static int i=0;

static   double DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
    totalSize = dInfo.EnumerateFiles()//double
                .Sum(file => file.Length);//double
    Total = Total + totalSize / 1024 / 1024 / 1024;//double
    total1[i] = Total;//double

    if (includeSubDir)
        totalSize += dInfo.EnumerateDirectories()
                 .Sum(dir => DirectorySize(dir, true));


    return totalSize;

What I have tried:

Multi thread and multi task but still can not.
Updated 2-Dec-19 6:26am
CHill60 2-Dec-19 8:19am
You have not stated the question or a problem. Please use the "Improve question" link to add this detail to your post
Member 14675243 2-Dec-19 9:29am
Could I have your email or telegram for send it to u and easy to dicuss?
CHill60 2-Dec-19 10:11am
No, you may not.
Member 14675243 2-Dec-19 10:12am
So now u understand what I mean?
Member 14675243 2-Dec-19 10:20am
Where can I ask it? Thank beforehand that explain me.
CHill60 2-Dec-19 10:23am
Where can you ask what? Update your post here with an actual question or explanation of your problem
Member 14675243 2-Dec-19 10:28am
Yes my problem just add multi thread to my function like my caption
Gerry Schmitz 2-Dec-19 10:47am
The answer depends on what platform: UWP; WPF; Windows forms; Net Core; Net Framework...
Member 14675243 2-Dec-19 10:49am
Window form . Net framework
Rate this:
Please Sign up or sign in to vote.

Solution 2

Windows Forms has the fewest short-cuts to threading; this should help:

C# Corner: Multithreading in WinForms -- Visual Studio Magazine[^]
Member 14675243 2-Dec-19 11:07am
Yeah bro. But I try it already but when I apply it to my function it doesn't work. When I get thread to array and run it with function with loop. It doesn't work.
Gerry Schmitz 2-Dec-19 11:20am
Try harder.
CHill60 2-Dec-19 11:47am
"it doesn't work" does not help us to help you. You are really making this hard work for us
Member 14675243 2-Dec-19 19:40pm
CHill60 3-Dec-19 5:37am
Why? Because you are not being clear about your problem. You need to learn to describe problems accurately and fully. It's hard work for us to keep asking you questions and asking for clarification. Most people will just stop bothering.
Luckily, it looks like Ashutosh is helping you along now.
Member 14675243 5 days ago
Nothing to say
Rate this:
Please Sign up or sign in to vote.

Solution 3

getsize for multiple path location at same time, use c# inbuilt TPL(parallel task library)
            () =>
                //code for getsize1

            () =>
                //code for getsize2

            () =>
                //code for getsizeN

i hope this will help.
Member 14675243 2-Dec-19 18:14pm
Could we use loop on parallel. Invoke? Because the function getSize is alot and we can not know how much is it.
Member 14675243 2-Dec-19 18:25pm
Mean that the location path for get size store in arraylist
[no name] 3-Dec-19 0:34am
something like below
var collectionofpaths = new List<string>();
var actions = new List<action>();
foreach (var path in collectionofpaths)
actions.Add(() => getsize(path));

Member 14675243 3-Dec-19 5:13am
I will try it.And tell you about it can or not.Too much thanks bro.
Member 14675243 3-Dec-19 5:45am
The problems is that the function is not calculate one by one.But it's total all size of function.Example getSize1=100GB ,getSize2=1000Gb,getSize3=10000GB but it's not show like that.But it shows with 11100GB(Total all function)
[no name] 3-Dec-19 6:20am
if i remember well you are keeping global variable 'TotalSizeOfFolder ' that why you get total size of all. to solve this problem, you need to return size value for each getsize method, then TPL may not work, you can use another option Task, which start new thread for each operation.

dictionary with each path and value 0 can be defined in starting. then at the end you will get size for each path.
var collectionofpaths = new Dictionary<string, int="">();
List<task> tasks = new List<task>();
foreach (var f in collectionofpaths)
tasks.Add(System.Threading.Tasks.Task.Factory.StartNew(() => collectionofpaths[f.Key] = getsize(f.Key)));


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100