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

  • SEARCH
  • Home
  • 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 8837379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:42:31+00:00 2026-06-14T09:42:31+00:00

I need to pass information from thread of scanning data to recording information thread(write

  • 0

I need to pass information from thread of scanning data to recording information thread(write to xml file).
It should looks something like this:

Application.Run() – complete

Scanning thread – complete

Writing to xlm thread – ???

UI update thread – I think I did it

And what i got now:

        private void StartButtonClick(object sender, EventArgs e)
    {
        if (FolderPathTextBox.Text == String.Empty || !Directory.Exists(FolderPathTextBox.Text)) return;
        {
            var nodeDrive = new TreeNode(FolderPathTextBox.Text);
            FolderCatalogTreeView.Nodes.Add(nodeDrive);
            nodeDrive.Expand();
            var t1 = new Thread(() => AddDirectories(nodeDrive));
            t1.Start();
        }
    }

     private void AddDirectories(TreeNode node)
    {
        string strPath = node.FullPath;
        var dirInfo = new DirectoryInfo(strPath);
        DirectoryInfo[] arrayDirInfo;
        FileInfo[] arrayFileInfo;

        try
        {
            arrayDirInfo = dirInfo.GetDirectories();
            arrayFileInfo = dirInfo.GetFiles();
        }
        catch
        {

            return;
        }

      //Write data to xml file
        foreach (FileInfo fileInfo in arrayFileInfo)
        {
            WriteXmlFolders(null, fileInfo);
        }
        foreach (DirectoryInfo directoryInfo in arrayDirInfo)
        {
            WriteXmlFolders(directoryInfo, null);
        }


        foreach (TreeNode nodeFil in arrayFileInfo.Select(file => new TreeNode(file.Name)))
        {
            FolderCatalogTreeView.Invoke(new ThreadStart(delegate { node.Nodes.Add(nodeFil); }));
        }

        foreach (TreeNode nodeDir in arrayDirInfo.Select(dir => new TreeNode(dir.Name)))
        {
            FolderCatalogTreeView.Invoke(new ThreadStart(delegate
                {node.Nodes.Add(nodeDir);
                }));

            StatusLabel.BeginInvoke(new MethodInvoker(delegate
                {
     //UI update...some code here
                }));
            AddDirectories(nodeDir);
        }
    }

      private void WriteXmlFolders(DirectoryInfo dir, FileInfo file)
    {//writing information into the file...some code here}

How to pass data from AddDirectories(recursive method) thread to WriteXmlFolders thread?

  • 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-06-14T09:42:32+00:00Added an answer on June 14, 2026 at 9:42 am

    Here is a generic mechanism how one thread generates data that another thread consumes. No matter what approach (read: ready made classes) you would use the internal principle stays the same. The main players are (note that there are many locking classes available in System.Threading namespace that could be used but these are the most appropriate for this scenario:

    AutoResetEvent – this allows a thread to go into sleep mode (without consuming resources) until another thread will wake it up. The ‘auto’ part means that once the thread wakes up, the class is reset so the next Wait() call will again put it in sleep, without the need to reset anything.

    ReaderWriterLock or ReaderWriterLockSlim (recommended to use the second if you are using .NET 4) – this allows just one thread to lock for writing data but multiple threads can read the data. In this particular case there is only one reading thread but the approach would not be different if there were many.

    // The mechanism for waking up the second thread once data is available
    AutoResetEvent _dataAvailable = new AutoResetEvent();
    
    // The mechanism for making sure that the data object is not overwritten while it is being read.
    ReaderWriterLockSlim _readWriteLock = new ReaderWriterLockSlim();
    
    // The object that contains the data (note that you might use a collection or something similar but anything works
    object _data = null;
    
    void FirstThread()
    {
        while (true)
        {
            // do something to calculate the data, but do not store it in _data
    
            // create a lock so that the _data field can be safely updated.
            _readWriteLock.EnterWriteLock();
            try
            {
                // assign the data (add into the collection etc.)
                _data = ...;
    
                // notify the other thread that data is available
                _dataAvailable.Set();
            }
            finally
            {
                // release the lock on data
                _readWriteLock.ExitWriteLock();
            }
        }
    }
    
    void SecondThread()
    {
        while (true)
        {
            object local; // this will hold the data received from the other thread
    
            // wait for the other thread to provide data
            _dataAvailable.Wait();
    
            // create a lock so that the _data field can be safely read
            _readWriteLock.EnterReadLock();
            try
            {
                // read the data (add into the collection etc.)
                local = _data.Read();
            }
            finally
            {
                // release the lock on data
                _readWriteLock.ExitReadLock();
            }
    
            // now do something with the data
        }
    }
    

    In .NET 4 it is possible to avoid using ReadWriteLock and use one of the concurrency-safe collections such as ConcurrentQueue which will internally make sure that reading/writing is thread safe. The AutoResetEvent is still needed though.

    .NET 4 provides a mechanism that could be used to avoid the need of even AutoResetEvent – BlockingCollection – this class provides methods for a thread to sleep until data is available. MSDN page contains example code on how to use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to pass information from one exe to another exe. Is it possible
When I need to pass some information from a form to another I usually
I have a viewmodel that implements IConfirmNavigationRequest and I need to pass information from
I need to be able to pass identifying information through to Authorize.net's server so
I need to pass an extra parameter :mobilejs => true from jQuery to a
I need to pass a variable which is captured from client side via jquery
I need to pass a parameter from an EditText and when I click the
I need to pass audio data into a 3rd party system as a 16bit
I need to pass data to a variable in my master page each time
How do I pass a row information from my class to a grid in

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.