cdub
cdub

Reputation: 25701

Multi threading of process in C#

I have the following code that runs to start my table calculations (the table calculations fire off a few queries returning thousands of rows). When my app just runs one instance, things are fine, but 2 or more then the server slows done and I start to get errors.

Should I turn this code into threads? How is that done?

private static object _lock = new object();

private void RunTable(string outputType, string _outputDataType) {

        Server.ScriptTimeout = 300;

        string returnCode = string.Empty;
        lock (_lock)
        {
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MainDll"].ToString()))
            {
                connection.Open();
                using (SqlCommand command = new SqlCommand(sql.ToString(), connection))
                {
                    command.CommandType = CommandType.Text;
                    command.CommandTimeout = 300;
                    returnCode = (string)command.ExecuteScalar();
                    Dispose();
                }
                Dispose();
            }
        }

Upvotes: 0

Views: 292

Answers (1)

Kiril
Kiril

Reputation: 40345

First: if you're seeing errors, then tell us what error you're seeing.

Second: how much data are you reading (i.e. can you load all the data in RAM)?

Third: if you can't load all the data at once, then try using the SqlDataReader to continuously read from the database.

Regarding the multithreading: it really depends on where is your bottleneck. If your bottleneck is in reading from the database, then you won't gain much by multithreading it (especially if your database does not allow concurrent access). You can use threads to process the data once it has been obtained from the database and that works particularly well when you have to use an SqlDataReader since you're reading record by record.

Upvotes: 1

Related Questions