爲什麼我得到這個「的System.OutOfMemoryException」的System.OutOfMemoryException而執行了一個小時
這個代碼是提供的服務來讀取智能卡讀卡器的智能卡,並把結果文本框,這是焦點中的任何後應用。當它運行(超過60分鐘)我得到錯誤拋出。我的情況發生了什麼?
請指教
class Program
{
static bool infinity = true;
static WinSCard sCard;
public static void Main(string[] args)
{
try
{
sCard = new WinSCard();
while (infinity) {
sCard.EstablishContext(); //establis smart card reader resourete manager
sCard.ListReaders(); //get list of smart card reader
string readerName = sCard.ReaderNames[0]; //the first smart card reader in the list
sCard.WaitForCardPresent(readerName); //block execution until smart card was attached to the reader
sCard.Connect(readerName); //connect to a smart card
// Console.WriteLine("ATR: 0x" + sCard.AtrString);
byte[] cmdApdu = { 0xFF, 0xCA, 0x00, 0x00, 00 }; // get card UID ...
byte[] respApdu = new byte[6];
int respLength = respApdu.Length;
//submit command to smart card and get a return result
sCard.Transmit(cmdApdu, cmdApdu.Length, respApdu, ref respLength);
//Convert Bute[] to HexString and finally Integer
int num = Int32.Parse(ByteArrayToString(respApdu, respLength), System.Globalization.NumberStyles.HexNumber);
Console.WriteLine("GET CARD UID ==> " + num);
//Send Result to any input that currently got focus
SendKeys.SendWait(num.ToString());
sCard.WaitForCardRemoval(readerName); //block execution until smart card was removed to the reader
sCard.Disconnect(); //terminated connection to a smart card
sCard.ReleaseContext(); //freely any resource allocated under the context
}
}
catch (WinSCardException ex)
{
Console.WriteLine(ex.WinSCardFunctionName + " 0x" + ex.Status.ToString("X08") + " " + ex.Message);
}
finally
{
Console.WriteLine("Please press any key...");
Console.ReadLine();
}
}
public static string ByteArrayToString(byte[] ba, int length)
{
StringBuilder hex = new StringBuilder(length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString().Replace("9000","");
}
}
這可能是非常有益的,如果你能提供到底是哪行拋出異常,和完整的stacktrace和這樣的東西! – AFract 2014-10-11 12:29:33
我也想知道它,但sharpdevelop並沒有顯示哪個導致這個錯誤(對於這個錯誤) – 2014-10-11 12:33:18
,你可以從發表的「評論」看到的答案 - 這個問題真的很難回答,因爲它是現在 - 沒有[方法]'東西確實不可能回答 - 只是嘗試一些隨機內存分析器,看看內存中的東西 - 最有可能的是一些事件處理程序,它可以避免GCed – Carsten 2014-10-11 12:43:19