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 6787167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:20:00+00:00 2026-05-26T17:20:00+00:00

i am creating windows application in C# in which i want to write in

  • 0

i am creating windows application in C# in which i want to write in a multiple file with multiple threads.I am getting data from different ports and there is one file associated with every port.Is it possible that creation of thread for every port and use the same thread again and again for writing data to respective file? Suppose i am getting data from ports 10000,10001,10002 and there are three files as 10000.txt,10001.txt and 10002.txt. I have to create three threads for writing data to these three files respectively and i want to use these threads again and again.Is it possible?Please can you give a small sample of code if possible?

  • 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-26T17:20:01+00:00Added an answer on May 26, 2026 at 5:20 pm

    If you let each thread handle its own file it’ll do?

    So when a port reader thread is created, you open the file that belongs to that port, and store the handle to that file. Whenever you receive data, you write it to the file:

    class PortReader
    {
        StreamWriter _writer;
        int _port;
        YourPort _myPort;
    
        public PortReader(int port)
        {
            // Do your port initialization stuff
    
            _writer = new StreamWriter(port + ".txt");
        }
    
        private void DataReceived(String data)
        {
            _writer.WriteLine(data);
        }
    
        public void StartReading()
        {
            while (_myPort.Connected)
            {
                // Do your port reading stuff
                String data = _myPort.ReadData();
    
                // Do some processing with the data
    
                // And now let it be written to the file
                DataReceived(data);
            }
        }
    }
    

    And then start them like such:

    List<PortReader> readerList = new List<PortReader>();
    
    readerList.Add(new PortReader(10000));
    readerList.Add(new PortReader(10001));
    readerList.Add(new PortReader(10002));
    // etc...
    
    foreach (PortReader reader in readerList)
    {
        new Thread(new ThreadStart(reader.StartReading)).Start();
    }
    

    Ideally you want to separate the file writing from the port reading, but I’ll leave that up to you for the sake of simplicity.

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

Sidebar

Related Questions

I am creating Windows Application in C# in which I want to write in
I'm creating a Firefox extension, in which I want to iterate through the Application.windows
I am creating a C# windows application to create an Excel 2003 sheet. There
I am currently creating a console application which is reading from a folder which
I am creating a Windows form application in C# which gets a set of
I want to Change default installation path which is Program Files in windows application
I'm creating an console application which needs to run several threads in order to
I'm currently in the process of creating a Windows service application which will monitor
I am creating a C# Windows application which is based on a medical inventory.In
I'm creating a Windows application which use FileSystemWatcher. FileSystemWatcher watches some catalog for changes.

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.