Bilal Salas

Bilal Salas

  • NA
  • 21
  • 0

threading problem-alot of invoking function simultenusly

Aug 6 2010 6:57 PM

Hi,I want to use threads in my program
I have function that will return values according to input
so I want to repeat this function alot of times mybe 500 times
and I need the result of all functions to do the mission of the program
but my program is using the internet and request and response and every request will consume 2 second's approxemitly.
if I will invoke this function 500 time it will take 1000 second and this is not programming.
I want to invoke the 500 functions in the same time and use the result if all of them.
so If they will be inviked in the same time all of them will consume 2 second. and that will be efficient.
I will but the ipmortant blocks of my coede here:

 

int count = 0;
Mutex mx = new Mutex();
private void ckeckproxyvalidation(string url)
{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Timeout = 10000;
try
{
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
string tv = res.ResponseUri.ToString();
if(tv.Contains(url) && IsSiteNameExist(url))
{
//mx.WaitOne();
count++;
//mx.ReleaseMutex();
}
}
catch
{
//mx.WaitOne();
count = count - 1 + 1;
//mx.ReleaseMutex();
}
finally
{
MessageBox.Show(count.ToString());
}
}



the previous function that will be invoked mainly.regardless what is IsSiteNameExist() do it will retrun true or false.

and the int count will increase its value if condition is right.
and is the mutex "Mutex mx = new Mutex();"important to mantain the value of count from error when threads running

and the folloing is the code that will inkove the prevoius function:
and the sitefortest is string array contens :

 

string[] sitefortest =
{
"http://www.megaupload.com",
"http://www.google.com",
"http://www.hotmail.com",
"http://www.yahoo.com",
"http://www.bing.com",
"http://www.youtube.com",
"http://www.facebook.com",
"http://www.islamway.com",
"http://www.mediafire.com",
"http://www.videoproxy.tv",
};

foreach (string es in sitefortest/*above array*/)
{
new Thread((ThreadStart)delegate { ckeckproxyvalidation(es); }).Start();
//ckeckproxyvalidation(es);
}


ok. the foreach loop will not return the desierd result.

if I invoke the checkproxyvalidation() function without threading the count variable will have another value.
for example when I invoke checkproxyvalidation() in for loop without thread the count will be 7
and if I invoke it with threads count will be 4 or 3
I don't know what is the problem
please help me


Answers (1)