I am new to C# programming and I have a task to make a process from single thread to multithreaded. I am using C#3.5 version and implementing threadpool in the code. I have searched about threadpool and did some changes but it is not working. When I again searched in the internet I think I wrote partial code upto only queueing user workitems, I am not understanding how to execute the threads.
Shown here is the code I wrote, please don't hesitate to correct me if the code is wrong, I am very new to C# coding.
ThreadPool.SetMaxThreads(6, 6);
try
{
// Assign the values to the report parameters
for (int i = 0; i < aq.Count; i++)
{
object j = aq[i];
ThreadPool.QueueUserWorkItem(new WaitCallback(process), j);
}
}
private void process(object i)
{
List<Report> aq = new List<Report>();
ReportEnv env = null;
ParameterValue[] paramval;
List<Report> list = new List<Report>();
Report al = null;
using (OleDbDataAdapter oleDA = new OleDbDataAdapter())
using (DataTable dt = new DataTable())
{
oleDA.Fill(dt, i);
foreach (DataRow _row in dt.Rows)
{
al = new Report();
al.EmailAttachmentMsg = _row["AttachmentMsg"].ToString();
al.reportName = _row["Repo"].ToString();
al.AccountNumber = _row["Number"].ToString();
al.AccountGroupCode = _row["GroupCode"].ToString();
al.EmailTo = _row["To"].ToString().Split(';');
al.ReportScheduleId = _row["ReportScheduleId"].ToString();
al.Frequency = _row["Frequency"].ToString();
al.ColcoContactTelephone = _row["ColcoContactTelephone"].ToString();
list.Add(al);
}
}
// aq = Populatereport(Dts.Variables["vnSource_SQL_Result"].Value);
env = PopulateEnvironment(Dts.Variables["vnEnvironment"].Value);
aq = list;
paramval = new ParameterValue[2];
paramval[0] = new ParameterValue();
paramval[0].Name = "PRM_CustomerDetails";
paramval[0].Value = aq[0].AccountNumber;
paramval[1] = new ParameterValue();
paramval[1].Name = "PRM_Startdate";
paramval[1].Value = aq[0].StartDate;
//Rendering the report begins
ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = env.SSRSServerUrl.ToString();
//Load the report options
rs.LoadReport(aq[0].ReportPath, null);
rs.SetExecutionParameters(paramval, aq[0].CultureCode);
// Set the filename
String filename = aq[0]. Number + "_" + env.Code + "_" + "_" + aq[0].Name +
DateTime.UtcNow.ToString("_dd-MM-yyyy_hh-mm-ss.fff");
//Render the report and generate pdf
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
Warning[] warnings = null;
string[] streamIDs = null;
string deviceInfo = null;
results = rs.Render(aq[0].ReportFormat, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
//Write the file into the directory
using (FileStream stream = File.OpenWrite(@env.wipPath + filename))
{
stream.Write(results, 0, results.Length);
}
if (SendEmail(env.From, aq[0].To, env.Subject, aq[0].Attachment, env.Server, false, filename, env.TimeOut) == true)
{
// Move report file from WIP to Processed
File.Move(@env.oldPath + filename, @env.newPath + filename);
}
}