![](/img/trans.png)
[英]Reload web.config after updating connectionstring programmatically in C#
[英]IIS CPU goes bezerk after updating web.config in a C# application with a Singleton with a thread
我有一個Web應用程序執行以下操作:
單擊按鈕以實例化單例,從而創建一個Thread。 該Thread持續運行一些HTTP請求來收集一些數據。 您可以單擊在線程上調用Abort()方法的停止按鈕,並且應用程序停止發出HTTP請求。 當我手動啟動/停止它時,一切正常。
當我“觸摸”web.config時,我的問題就出現了。 CPU(w3wp.exe進程)激增,網站停止響應。 有人知道為什么會這樣嗎? 不應該更新web.config重置一切?
示例代碼如下:
private static MyProcessor mp = null;
private Thread theThread = null;
private string status = STOP;
public static string STOP = "Stopped";
public static string START = "Started";
private MyProcessor()
{}
public static MyProcessor getInstance()
{
if (mp == null)
{
mp = new MyProcessor();
}
return mp;
}
public void Start()
{
if (this.status == START)
return;
this.theThread = new Thread(new ThreadStart(this.StartThread));
this.theThread.Start();
this.status = START;
}
public void Stop()
{
if (this.theThread != null)
this.theThread.Abort();
this.status = STOP;
}
private void StartThread()
{
do
{
try
{
//do some work with HTTP requests
Thread.Sleep(1000 * 2);
}
catch (Exception e)
{
//retry - work forever
this.StartThread();
}
} while (this.status == START);
}
我懷疑這是問題所在:
private void StartThread()
{
do
{
try
{
//do some work with HTTP requests
Thread.Sleep(1000 * 2);
}
catch (Exception e)
{
//The recursive call here is suspect
//at the very least find a way to prevent infinite recursion
//--or rethink this strategy
this.StartThread();
}
} while (this.status == START);
}
當您的應用程序域重置時,您將獲得一個ThreadAbort異常,該異常將在此處捕獲並觸發遞歸調用,這將觸發另一個異常,以及另一個遞歸調用。 它一直是烏龜!
是的,在web .config中進行任何更改都會重置應用程序,並且asp.net會重新構建應用程序。
對於Bin和App_Code文件夾下的文件等其他文件也是如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.