Given the following code:
if (!string.IsNullOrEmpty(link1))
{
string[] link1_ar = link1.Split(sep, StringSplitOptions.None);
string page1 = link1_ar[1];
string filter1 = link1_ar[2];
string code2 = link2_ar[3];
MyMethod(summary, page1, filter1, code1);
}
if (!string.IsNullOrEmpty(link2))
{
string[] link2_ar = link2.Split(sep, StringSplitOptions.None);
string page2 = link2_ar[1];
string filter2 = link2_ar[2];
string code2 = link2_ar[3];
MyMethod(summary, page2, filter2, code2);
}
if (!string.IsNullOrEmpty(link3))
{
string[] link3_ar = link3.Split(sep, StringSplitOptions.None);
string page3 = link3_ar[1];
string filter3 = link3_ar[2];
string code3 = link3_ar[3];
MyMethod(summary, page3, filter3, code3);
}
private void MyMethod(ref string summary, string bid_page, string bid_filter, string bid_code, string bid_silver)
{
bla bla bla
}
i want to run three MyMethod together at a same time!
what should i change in these codes for getting that purpose?
Assuming your MyMethod is actually thread safe then the following should work using the Parallel.ForEach() method in .Net 4.0. I'm tracking the individual summaries in a ConcurrentDictionary so you can reference them by link later if you need to.
I've also assumed that you changed MyMethod() to return the summary.
List<string> links = new List<string>() { link1, link2, link3};
ConcurrentDictionary<string, string> summariesByLink = new ConcurrentDictionary<string, string>();
Parallel.ForEach(links, link => {
if (!string.IsNullOrEmpty(link))
{
string[] link_ar = link.Split(sep, StringSplitOptions.None);
string page = link_ar[1];
string filter = link_ar[2];
string code = link_ar[3];
string summary = MyMethod( page, filter, code);
summariesByLink.Add(link, summary);
}
}