Click here to Skip to main content
14,456,727 members
Rate this:
Please Sign up or sign in to vote.
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?
Posted
Comments
Mehdi Gholam 7-Nov-11 9:46am
   
What have you done so far?
   
It's not so easy for a beginner. This is actually a good question (I voted 5); I provided solution which is not that trivial, please see, pretty interesting.
--SA
Danyvaral 7-Nov-11 9:51am
   
i am not getting exactly how to do it?Somewhat confusing.So i want someones help.please help me
   
Done. Please read carefully, including two referenced solutions (and answer and an article) and ask if you have further questions.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

It's not a bad idea to reuse threads. You have two options: 1) using thread pool, 2) having "permanent" thread (with lifetime maybe close to the lifetime of your process) pushing different tasks on it when required and throttling it between tasks the way it spend not CPU time and waits to be waken up by a new task.

First way is simple. Get an idea of thread pool: http://en.wikipedia.org/wiki/Thread_pool_pattern[^]. With .NET, use the class System.Threading.ThreadPool, see http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^].

The thread pool method is good when you cannot precisely pre-determine the number of threads working in parallel. Nevertheless, having too many threads should be avoided.

If you really want to write to no more than three files (or so) at the same time, you can use the schema with "permanent" threads and throttling. Create such threads using regular thread constructor, System.Threading.Thread, http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^].

First of all, to make it all accurate, make your the thread body an instance (non-static) method of some class, a thread wrapper. In this way, you can encapsulate the thread in this class and make all the data of the wrapper class accessible to the thread at once, via "this" reference to the instance of the wrapper class passed implicitly. See my past solutions with detailed explanation here:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

The body of the thread should repeat in cycle waiting for the instance of System.Threading.EventWaitHandle, System.Threading.AutoResetEvent, System.Threading.ManualResetEvent, see:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx[^].

When you thread calls the method WaitOne of the event wait handle, it is put to the special wait state; switched off but the OS and never scheduled back to execution until waken up by the expiration time (if any), Thread.Abort or by the call to the method Set of the same instance of the event wait handle called by other thread. In this way, this other thread can "put" tasks to you thread and thread wrapper and then let your thread to go using Set.

More advanced (and maybe easier in usage) approach using this technique is using a blocking queue. You can find the implementation in my article: "Simple Blocking Queue for Thread Communication and Inter-thread Invocation". For .NET v. 4.0, such queue already exists, but I explain the usage patterns and provide detail usage code, including another interesting and useful case when the elements of the queue are delegate instances; in this way, you can push very different tasks to the queue and sequentially execute them in the same thread, in parallel with several threads.

—SA
   
Comments
Mehdi Gholam 7-Nov-11 10:29am
   
5'ed
   
Thank you, Mehdi.
--SA
Danyvaral 8-Nov-11 2:22am
   
Thanks for response........!
Rate this:
Please Sign up or sign in to vote.

Solution 1

Create a parameterized thread function that saves a file, and pass the filename to be saved, along with the data to be saved to that file. If you're getting data from multiple ports, each port receiving data should also be opened on itsa own thread, and those port threads should create their own file saving thread.


I would create a class that opens and listens on a given port (in a thread of course) and handles the saving of its data, and then instantiate as many of these port objects as is necessary.
   
v2
Comments
Danyvaral 7-Nov-11 10:15am
   
I dont want to create thread again and again.I want to reuse these threads.and i am getting data continuously on these ports.Please tell me how to do it?
#realJSOP 7-Nov-11 10:57am
   
You can't reuse threads. Once they complete, or if they are otherwise aborted, you MUST recreate them. There's no getting around that, even with thread pools. You could engineer the thread in such a way as to sit/spin waiting for a buffer to fill up before dumping the text to an opened file.

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