Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Multithreading using StreamWriter
Message
From
02/04/2012 16:15:21
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
Multithreading using StreamWriter
Environment versions
Environment:
C# 4.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Application:
Desktop
Miscellaneous
Thread ID:
01540059
Message ID:
01540059
Views:
77
I'm attempting to write a desktop application that will build and post multiple files for a list of clients. There are thousands of clients and the data that will be written to these files can be large thus the desire to convert this process to write these files in parallel to see what speed increases can be made to this process. I currently have this process looping through the list of clients and building the reports for each client and posting them to the corresponding output directories. My main question would be are the variables, objects, references kept separate in each individual thread as not to cause a conflict with each other.

Would there be an issue with the StreamWriter variable being named the same for each thread?


Example:
 // Loop Through Site Profiles //
Parallel.ForEach(dt_sites.AsEnumerable(), sites_dr =>
{
              site_nbr = Convert.ToInt32(sites_dr["sitenbr"]);
              site_nm = ((string)(sites_dr["sitename"])).Trim();
              site_dir = ((string)(sites_dr["postpath"])).Trim();
              site_work_dir = Path.Combine("c:\work\", site_nbr.ToString().PadLeft(5, '0'));

           // dr_clms_data = Data to be written to the file

           cls_build_file(dr_clms_data, p_site_nbr, site_work_dir);
});


public static void cls_build_file(DataRow [] p_clms_dr, int p_site_nbr, string p_work_dir)
{
                StreamWriter sw = new StreamWriter(Path.Combine(p_work_dir, "test.txt"));
                sw.Write("test");
                sw.Close();
                sw.Dispose();
}
Next
Reply
Map
View

Click here to load this message in the networking platform