Search code examples
c#multithreadingdatareader

Passing data to another class without using ODBC.DataReader in C#


I am trying to make a multithreaded program that pulls numerous threads of data from a server held by another company. Currently once a thread has pulled the data it then uses OdbcDataReader to read that data. But ultimately, what I would like to do is pass that data off to another different thread, which will use the reader to read and parse the data, so that the current thread can continue to pull more data from the database.

Currently here is the code for the thread up until the point of the reader.

public class plexThread
    {
        OdbcConnection connection;
        OdbcCommand command;
        string sqlStatement;
        OdbcDataReader reader;
        string threadNumber;
        DateTime startDate;
        DateTime endDate;

        public plexThread(string threadNumber)
        {
            connection = new OdbcConnection("XXXX");
            this.threadNumber = threadNumber;
        }

        public void plexthread()
        {            
            try
            {
                connection.Open();
                startDate = ThreadInfo.useStartDate();
                endDate = ThreadInfo.getEndDate();
                DateTime timerStart = new DateTime();
                DateTime now = DateTime.Now;
                int timeout = 0;
                bool dataCaptured;

                while (startDate <= endDate)
                {
                    dataCaptured = false;
                    DateTime startDatePlusDay = startDate.AddDays(+1);
                    Console.WriteLine("Thread" + threadNumber + " is picking up day: " + startDate);
                    string startDateString = startDate.ToString("yyyy-MM-dd");
                    string startDatePlusDayString = startDatePlusDay.ToString("yyyy-MM-dd");
                    sqlStatement = "call sprocXXXX(null, null, '" + startDateString + "', '" + startDatePlusDayString + "', null, null,null,null,'',null,null,null,null,null,null,'', 164407);";
                    command = new OdbcCommand(sqlStatement, connection);

                    while (dataCaptured == false)
                    {
                        if (timeout <= 5)
                        {
                            try
                            {
                                reader = command.ExecuteReader();
                                int fieldCount = reader.VisibleFieldCount;
                                while (reader.Read())
                                {
                                    for (int i = 0; i < fieldCount; i++)
                                    {                                       
                                        string data = reader[i].ToString();
                                    }

Not sure what is the best way to do this?


Solution

  • It looks like you're implementing the Producer/Consumer pattern.

    One solid solution is to use a BlockingCollection. Your current thread can post the data read from the reader to the blocking collection, and a separate thread can read from the blocking collection until there is no more data to consume.

    There's an excellent example at the bottom of the linked MSDN page.