Click here to Skip to main content
15,797,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Good Morning!

I'm building an application that Read the XML from SQL table and then processes it and write in to text file

I Have two method ReadXML() which read XML file & returning Business Entity object.
second method WriteXML() which takes Business Entity object and write into text file.

Here's how I want it to operate:

In main thread I want to create two thread one is for ReadXML() which will read XML & return Business Entity object which I have to store in Queue & second thread for WriteXML() which will read object from queue & write into text file.

How can I implement such a scenario? Queue will be shared by two threads?
How that queuing will be done?

- Dattatrya Moin
Updated 2-Apr-12 19:14pm
Sergey Alexandrovich Kryukov 2-Apr-12 23:36pm    
Well... of cause you can do it with threads, the questions are: why? is reading or writing of XML supposed to take so long time? and what is the problem?
Dattatrya Moin 11 3-Apr-12 0:17am    
Problem is How to share the queue between two threads? And What kind of care should I take. If you know best link to read about this please share..

Sergey Alexandrovich Kryukov 3-Apr-12 1:32am    
lock {/*...*/}


1 solution

Let's assume you are using System.Collections.Generic.Queue<T>. Please see:[^].

Read the section "Thread Safety". It explains what exactly you should do. You must implement your own synchronization. Practically, simply wrap the operations you need with lock, for example:
internal class QueueWrapper<T> {
    internal void void Enqueue(T item) {
    internal T Dequeue() {
            return Implementation.Dequeue();
    System.Collections.Generic.Queue<T> Implementation =
        new System.Collections.Generic.Queue<T>();
    object SyncObject = new Object(); // always lock with a private object, see below

Don't do lock(this); it would work but allow outside lock with the QueueWrapper instance. So, hiding the synchronization object SyncObject inside wrapper is a good fool-proof technique: no one will be able to create excessive synchronization with such object and compromise parallelism.

Share this answer
Prasad_Kulkarni 3-Apr-12 2:03am    
+5 good one.
Nice to see you sir,
Sergey Alexandrovich Kryukov 3-Apr-12 2:09am    
Thank you very much, Pashad.
Prasad_Kulkarni 3-Apr-12 2:11am    

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900