Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6563237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:48:21+00:00 2026-05-25T13:48:21+00:00

My problem is passing the this.folderFolder instance method to ThreadStart ctor. I step through

  • 0

My problem is passing the this.folderFolder instance method to ThreadStart ctor. I step through it with dirAssThread and watch it update the instance data member correctly and complete, then I trap back to

if (dirAssThread.IsAlive) completeThread(-1); //***ie abort

and find that the data member of the same this instance that I passed with the method to the ThreadStart ctor has miraculously reset itself to 0!

Here are the other functions

using System;
using System.IO;
using System.Threading;

namespace MonitorService
{
    struct myStruct
    {
        long bytesSzMember;
        Thread dirAssThread;
        private Object thisLock;

        private void completeThread(long bytesSzNew)
        {
            lock (thisLock)
            {
                if (bytesSzNew == -1)
                {
                    dirAssThread.Abort();
                    Console.WriteLine("A thread timed out.");
                }
                else
                {
                    bytesSzMember = bytesSzNew;
                    Console.WriteLine("A thread update size.");
                }
            }
        }

        private void folderFolder()
        {
            long bytesSzNew = 0;
            DirectoryInfo di = new DirectoryInfo("C:\\SomeDir");
            DirectoryInfo[] directories = di.GetDirectories("*",SearchOption.AllDirectories);
            FileInfo[] files = di.GetFiles("*",SearchOption.AllDirectories);
            foreach (FileInfo file in files)
            {
                bytesSzNew += file.Length;
            }
            completeThread(bytesSzNew);
        }

        private void updateSize()
        {
            thisLock = new Object();
            dirAssThread = new Thread(new ThreadStart(this.folderFolder));
            dirAssThread.Start();
            Thread.Sleep(5000);
            if (dirAssThread.IsAlive) completeThread(-1);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:48:22+00:00Added an answer on May 25, 2026 at 1:48 pm

    Update

    After the question title update, the problem you are seeing is that structs are copied on reference. You are passing a copy of your struct when assigning the delegate to the thread, and it is this copy that will be updated by the thread. When you do your check in completeThread it is against the original which has not been updated.

    Use a class instead of a struct.

    Alternate Solution

    I would suggest using wait handles instead of sleeps and thread aborts, as Thread.Abort is considered a dangerous practice and should be avoided (quite easily in this case). I propose the following solution which is a recursive version that will not follow circular references (so there is no need to abort in reality, the code can be removed if you do not want a timeout facility).

    public class WaitForFileSizes
    {
        private readonly object _syncObj = new object();
        private readonly HashSet<string> _seenDirectories = new HashSet<string>();
        private readonly ManualResetEvent _pEvent = new ManualResetEvent(false);
        private long _totalFileSize;
        private Thread _thread;
        private volatile bool _abort;
    
        private void UpdateSize()
        {
            _thread = new Thread(GetDirectoryFileSize);
            _thread.Start();
    
            bool timedout = !_pEvent.WaitOne(5000);
    
            if (timedout)
            {
                _abort = true;
                _pEvent.WaitOne();
                Console.WriteLine("A thread timed out.");
            }
            else
            {
                Console.WriteLine("Total size {0}b.", _totalFileSize);
            }
        }
    
        private void GetDirectoryFileSize()
        {
            GetDirectoryFileSizesRecursively(new DirectoryInfo("C:\\temp"));
    
            _pEvent.Set();
        }
    
        private void GetDirectoryFileSizesRecursively(DirectoryInfo dir)
        {
            Parallel.ForEach(dir.EnumerateFiles(), f =>
            {
                if (_abort)
                {
                    _pEvent.Set();
                    return;
                }
    
                Interlocked.Add(ref _totalFileSize, f.Length);
            });
    
            Parallel.ForEach(dir.EnumerateDirectories(), d =>
            {
                if (!IsSeen(d))
                {
                    GetDirectoryFileSizesRecursively(d);
                }
            });
        }
    
        private bool IsSeen(DirectoryInfo dir)
        {
            lock (_syncObj)
            {
                if (!_seenDirectories.Contains(dir.FullName))
                {
                    _seenDirectories.Add(dir.FullName);
    
                    return false;
                }
    
                return true;
            }
        }
    }
    

    Update

    As we now have circular reference detection, the threading and abort code can be removed as that was previously there to abort the thread if it was in an endless loop – no need for that now:

    public class WaitForFileSizes
    {
        private readonly object _syncObj = new object();
        private readonly HashSet<string> _seenDirectories = new HashSet<string>();
        private long _t;
    
        public void UpdateSize()
        {
            GetSize(new DirectoryInfo("C:\\temp"));
    
            Console.WriteLine("Total size {0}b.", _t);
        }
    
        private void GetSize(DirectoryInfo dir)
        {
            Parallel
            .ForEach(dir.EnumerateFiles(), f => Interlocked.Add(ref _t, f.Length));
    
            Parallel
            .ForEach(dir.EnumerateDirectories().Where(IsNewDir), GetSize);
        }
    
        private bool IsNewDir(FileSystemInfo dir)
        {
            lock (_syncObj)
            {
                if (!_seenDirectories.Contains(dir.FullName))
                {
                    _seenDirectories.Add(dir.FullName);
                    return true;
                }
                return false;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should there be any problem passing this kind of a collection in WCF? class
This is a follow up to my previous question: Problem passing parameters via Iframe
This stored procedure does not save the data, it seems to be a problem
The Dropdownlist data binding using throw common function. In this have a problem in
problem with passing variables (php, sql, jquery draggable) Hi, this is my problem: updating
I'm having a problem passing strings that exceed 80 characters in JSON. When I
I'm trying to solve the problem of passing a 2-dimensional table into JavaScript AJAX
I'm experimenting with WCF Services, and have come across a problem with passing Interfaces.
I am having a problem passing a json string back to a php script
I have a problem passing in a complex type into a controller. Here is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.