ThreadPool Error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

I am not sure whether this is the right newsgroup for this question but I am
posting it anyway. I am writing a Windows Service application using C# which
uses MS-CRM Web Services API to communicate with MS-CRM. It creates and
updates Account, SalesOrder, and Product entities.

For "Account" it uses following credentials:

string strServer = "2KSERV1-VPC";
string virtualDirectory = "MSCRMServices";
string strDir = "http://" + strServer + "/" + virtualDirectory + "/";

Microsoft.CRM.Proxy.BizUser bizUser = new Microsoft.CRM.Proxy.BizUser();
bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
bizUser.Url = strDir + "BizUser.srf";

Microsoft.CRM.Proxy.CRMAccount account = new Microsoft.CRM.Proxy.CRMAccount
();
account.Credentials = System.Net.CredentialCache.DefaultCredentials;
account.Url = strDir + "CRMAccount.srf";

So basically I am using http request and response. Everything works well for
some time (may be for 5-6 new account insertions) but then I get following
error:

"There were not enough free threads in the ThreadPool object to comelete the
operation."

I did some research on that. Are there only 25 threads available per
process? If yes, how to get around this problem? The service is running on
the continuous basis.

Any help is appreciated.

Thanks,
Prasad
 
I did some research on that. Are there only 25 threads available per
It's actually 50, the other 25 is for IO threads.

I believe your issues stems from a design flaw. You are firing webrequests
indiscriminately - i make that assumption on the BizUser object that takes a
URI. You will eventually run out of free threads to fire webrequests. One
approach is to not use the threadpool and implement a limited number of
threads (3) to fire webrequests. This caps the threads being spawned so you
won't unnecessarily tax the system resources.
--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
 
Thanks Alvin. I am not using ThreadPool class anywhere in my code. Can you
give me some pointers or examples for how to implement a limited number of
threads to fire webrequests. Also is there any way I can kill the thread
after one http request and response cycel is over. I may sound stupid but I
am trying to make anology with SQLConnection.Close().

Thanks,
Prasad
Alvin Bruney said:
I did some research on that. Are there only 25 threads available per
process?
It's actually 50, the other 25 is for IO threads.

I believe your issues stems from a design flaw. You are firing webrequests
indiscriminately - i make that assumption on the BizUser object that takes a
URI. You will eventually run out of free threads to fire webrequests. One
approach is to not use the threadpool and implement a limited number of
threads (3) to fire webrequests. This caps the threads being spawned so you
won't unnecessarily tax the system resources.
--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


Prasad said:
Hi All,

I am not sure whether this is the right newsgroup for this question but I
am
posting it anyway. I am writing a Windows Service application using C#
which
uses MS-CRM Web Services API to communicate with MS-CRM. It creates and
updates Account, SalesOrder, and Product entities.

For "Account" it uses following credentials:

string strServer = "2KSERV1-VPC";
string virtualDirectory = "MSCRMServices";
string strDir = "http://" + strServer + "/" + virtualDirectory + "/";

Microsoft.CRM.Proxy.BizUser bizUser = new Microsoft.CRM.Proxy.BizUser();
bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
bizUser.Url = strDir + "BizUser.srf";

Microsoft.CRM.Proxy.CRMAccount account = new
Microsoft.CRM.Proxy.CRMAccount
();
account.Credentials = System.Net.CredentialCache.DefaultCredentials;
account.Url = strDir + "CRMAccount.srf";

So basically I am using http request and response. Everything works well
for
some time (may be for 5-6 new account insertions) but then I get following
error:

"There were not enough free threads in the ThreadPool object to comelete
the
operation."

I did some research on that. Are there only 25 threads available per
process? If yes, how to get around this problem? The service is running on
the continuous basis.

Any help is appreciated.

Thanks,
Prasad
 
//Three threads will do our work to reduce time. Because each web request is
essentially

//a blocked thread, cpu utilization is low

ThreadWork.workerProcess fetcher = new ThreadWork.workerProcess
(arr1,path,appendFlag,flush);

Thread worker = new Thread (new ThreadStart (fetcher.Fetch));

ThreadWork.workerProcess fetcher2 = new ThreadWork.workerProcess
(arr2,path,appendFlag,flush);

Thread worker2 = new Thread (new ThreadStart (fetcher2.Fetch));

ThreadWork.workerProcess fetcher3 = new ThreadWork.workerProcess
(arr3,path,appendFlag,flush);

Thread worker3 = new Thread (new ThreadStart (fetcher3.Fetch));

//start work

worker.Start();

worker2.Start();

worker3.Start();

//wait for each other

worker .Join();

worker2.Join();

worker3.Join();



//thread body looks like this

fetch()

Uri myUri =new Uri("http://www.google.com/search?sa=X&oi=fwp&pb=f&q=" +
strNumber);

// Creates an System.Net.HttpWebRequest with the specified URL.

System.Net.HttpWebRequest myHttpWebRequest =
(System.Net.HttpWebRequest)WebRequest.Create(myUri);

myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; Q312461; .NET CLR 1.0.3705)";

HttpWebResponse res = (HttpWebResponse)myHttpWebRequest.GetResponse();

StreamReader sr = new StreamReader(res.GetResponseStream(),
System.Text.Encoding.UTF8);

string pageContent = sr.ReadToEnd();

res.Close();

sr.Close();


customize this code as you see fit

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


Prasad said:
Thanks Alvin. I am not using ThreadPool class anywhere in my code. Can you
give me some pointers or examples for how to implement a limited number of
threads to fire webrequests. Also is there any way I can kill the thread
after one http request and response cycel is over. I may sound stupid but
I
am trying to make anology with SQLConnection.Close().

Thanks,
Prasad
Alvin Bruney said:
I did some research on that. Are there only 25 threads available per
process?
It's actually 50, the other 25 is for IO threads.

I believe your issues stems from a design flaw. You are firing
webrequests
indiscriminately - i make that assumption on the BizUser object that
takes a
URI. You will eventually run out of free threads to fire webrequests. One
approach is to not use the threadpool and implement a limited number of
threads (3) to fire webrequests. This caps the threads being spawned so
you
won't unnecessarily tax the system resources.
--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------


Prasad said:
Hi All,

I am not sure whether this is the right newsgroup for this question but
I
am
posting it anyway. I am writing a Windows Service application using C#
which
uses MS-CRM Web Services API to communicate with MS-CRM. It creates and
updates Account, SalesOrder, and Product entities.

For "Account" it uses following credentials:

string strServer = "2KSERV1-VPC";
string virtualDirectory = "MSCRMServices";
string strDir = "http://" + strServer + "/" + virtualDirectory + "/";

Microsoft.CRM.Proxy.BizUser bizUser = new
Microsoft.CRM.Proxy.BizUser();
bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
bizUser.Url = strDir + "BizUser.srf";

Microsoft.CRM.Proxy.CRMAccount account = new
Microsoft.CRM.Proxy.CRMAccount
();
account.Credentials = System.Net.CredentialCache.DefaultCredentials;
account.Url = strDir + "CRMAccount.srf";

So basically I am using http request and response. Everything works
well
for
some time (may be for 5-6 new account insertions) but then I get
following
error:

"There were not enough free threads in the ThreadPool object to
comelete
the
operation."

I did some research on that. Are there only 25 threads available per
process? If yes, how to get around this problem? The service is running
on
the continuous basis.

Any help is appreciated.

Thanks,
Prasad
 
Back
Top