0

I am for the first time trying to use Thread in my windows service application.Now as per my condition i have to read data from database and if it matches with condition i have to execute a function in new thread.Now the main concern is that as my function which meant to execute in new Thread is lengthy and will take time so i have a query that, Will my program will reach to datareader code and read the new value from the database while my function keeps on executing in the background in thread.My application execution logic is time specific. Here is the code..

while (dr.Read())
{

    time = dr["SendingTime"].ToString();

    if ((str = DateTime.Now.ToString("HH:mm")).Equals(time))
    {

        //Execute Function and send reports based on the data from the database.

        Thread thread = new Thread(sendReports);
        thread.Start();
    }
}

Please help me..

user3924730
  • 163
  • 1
  • 3
  • 14
  • so what are you asking? how to fire a new thread (which you did), or how to avoid calling it again if it's still running, and you have new data come in? – Noctis Sep 01 '14 at 05:20
  • @Noctis I want to know that if my first thread is running the function.Will my Code will reach to Data reader again and execute the new thread.. – user3924730 Sep 01 '14 at 05:22
  • 1
    Yes... this will spawn a new thread for every row in the data reader. If the each thread takes longer to execute than it does for the data reader to read then your application will have all those threads running concurrently. Which I should add is pretty dangerous code... http://stackoverflow.com/questions/145312/maximum-number-of-threads-in-a-net-app – Mick Sep 01 '14 at 05:34
  • @Mick There will be a maximum of four to five threads .Will it be OK – user3924730 Sep 01 '14 at 05:37
  • 1
    @user3924730 You mean 4-5 rows? You'll end up with the same number of threads as rows, if you have a million rows in your table I'm pretty sure it would fail. If there's 4 or 5 rows, no, there'll be no problem with that – Mick Sep 01 '14 at 05:39
  • @Mick I am sure it will be 4 or 5 rows only .So it will be OK – user3924730 Sep 01 '14 at 05:40

1 Answers1

1

Yep, as the comments said, you will have one thread per row. if you have 4-5 rows, and you'll run that code, you'll get 4-5 threads working happily in the back.

You might be happy with it, and leave it, and in half a year, someone else will play with the DB, and you'll get 10K rows, and this will create 10K threads, and you'll be on a holiday and people will call you panicking because the program is broken ...

In other words, you don't want to do it, because it's a bad practice.
You should either use a queue with working units, and have a fixed number of threads reading from those queues (in which case you might have 10K units there, but lets say 10 threads that will pick them up and process them until they are done), or some other mechanism to make sure you don't create a thread per row.

Unless of course, you don't care ...

Noctis
  • 11,507
  • 3
  • 43
  • 82