C# 内存不足$\u得到了什么?
我正在创建一个多线程的C# 内存不足$\u得到了什么?,c#,webclient,C#,Webclient,我正在创建一个多线程的random.orgnumber getter来实现我的c#IRC机器人。我的问题是,它使用了相当大的内存占用。我认为这是WebClient类。我不喜欢它使用~5000k内存来连接url,读取第一行并输出数字 有没有更轻松的方法 class Program { static void Main(string[] args) { for (int i = 0; i < 4; i++)
random.org
number getter来实现我的c#IRC机器人。我的问题是,它使用了相当大的内存占用。我认为这是WebClient
类。我不喜欢它使用~5000k
内存来连接url,读取第一行并输出数字
有没有更轻松的方法
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 4; i++)
{
Thread More = new Thread(GetRandomNum);
More.Start();
}
}
public static void GetRandomNum()
{
string number;
for (int i = 0; i < 100; i++)
{
using (WebClient client = new WebClient())
{
number = client.DownloadString("http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new");
}
Console.WriteLine(number.Trim());
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
对于(int i=0;i<4;i++)
{
线程更多=新线程(GetRandomNum);
More.Start();
}
}
公共静态void GetRandomNum()
{
字符串编号;
对于(int i=0;i<100;i++)
{
使用(WebClient=newWebClient())
{
number=客户端。下载字符串(“http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new");
}
Console.WriteLine(number.Trim());
}
}
}
就可以了
WebRequest request = WebRequest.Create ("http://www.random.org/integers/?num=1&min=1&max=100&col=1&base=10&format=plain&rnd=new");
// If required by the server, set the credentials.
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Display the status.
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();
这看起来非常复杂,但代码相当简单和直接。
内存消耗必须比WebClient低得多。为什么要使用Random.org而不是一个好的加密PRNG?我从来没有找到一个令人信服的技术理由来选择RNG Web服务而不是加密PRNG,还有很多理由来避免它。好吧,我试图为我的IRC机器人创建一个真正的随机1-100骰子掷骰子。PRNG还行,我想,我只是希望它完美:(