如何修复X次工作后c#应用程序停止
我正在使用c#创建控制台应用程序,它读取一些文件并在其中进行循环,在每个文件中我调用一个方法,在应用程序停止工作或服务器上的internet连接停止工作数小时后:如何修复X次工作后c#应用程序停止,c#,garbage-collection,C#,Garbage Collection,我正在使用c#创建控制台应用程序,它读取一些文件并在其中进行循环,在每个文件中我调用一个方法,在应用程序停止工作或服务器上的internet连接停止工作数小时后: namespace Sample { class Program { public static void Main(string[] args) { string contents = File.ReadAllText(@"threads.txt");
namespace Sample
{
class Program
{
public static void Main(string[] args)
{
string contents = File.ReadAllText(@"threads.txt");
if(contents != null) {
ThreadPool.SetMaxThreads(Convert.ToInt32(contents), Convert.ToInt32(contents));
}
foreach (string user in File.ReadLines("x.txt"))
{
foreach (string pass in File.ReadLines("y.txt"))
{
foreach (string line in File.ReadLines("z.txt"))
{
ThreadPool.QueueUserWorkItem(tesTConn, new object[] { a, b, c, d });
Thread.Sleep(50);
}
///////////////////////////////////////////////////////////////////
}
}
Console.WriteLine("Press ENTER to exit.");
Console.Read();
}
//string domain, int port, string username, string password
public static void tesTConn(object state)
{
try
{
object[] array = state as object[];
string domain = Convert.ToString(array[0]);
int port = Convert.ToInt32(array[1]);
string username = Convert.ToString(array[2]);
string password = Convert.ToString(array[3]);
Console.WriteLine("Working with IP:" + domain + " | User :" + username + " | Pass:" + password);
// SipClient client = new SipClient(domain, port, username, password);
SipClient client = new SipClient(domain, port, "0", "0");
client.Connect();
// Contact[] contacts = client.GetRegisteredContacts("sip:" + domain, "sip:" + username + "@" + domain);
Contact[] contacts = client.GetRegisteredContacts("sip:" + domain, "sip:0@" + domain);
if (!(contacts != null && contacts.Length > 0))
{
SipClient client1 = new SipClient(domain, port, username, password);
client1.Connect();
Contact[] contacts1 = client1.GetRegisteredContacts("sip:" + domain, "sip:" + username + "@" + domain);
if ( contacts1.Length > 0)
{
string path = @domain + ".txt";
if (!File.Exists(path))
{
using (var tw = new StreamWriter(path, true))
{
tw.WriteLine(domain);
tw.WriteLine(domain + " : " + username + " : " + password);
}
}
else if (File.Exists(path))
{
using (var tw = new StreamWriter(path, true))
{
tw.WriteLine(domain + " : " + username + " : " + password);
}
}
}
client1.Disconnect();
}
client.Disconnect();
return;
}
catch { }
finally { }
}
}
}
我想知道如何通过使用垃圾收集或任何其他技术来解决这个问题?不确定这是否是您的问题,但您不应该这样设置线程池线程,也不应该让足够的工作排队以最大化线程池。相反,设置MaxDegreeOfParallelism并让CLR管理线程池。乙二醇
public static void Main(string[] args)
{
int threads = Convert.ToInt32(File.ReadAllText(@"threads.txt"));
var workItems = new List<object>();
foreach (string user in File.ReadLines("x.txt"))
{
foreach (string pass in File.ReadLines("y.txt"))
{
foreach (string line in File.ReadLines("z.txt"))
{
workItems.Add(new object[] { user, pass, line });
}
}
}
var opts = new ParallelOptions() { MaxDegreeOfParallelism = threads };
var results = Parallel.ForEach(workItems, opts, DoWork);
Console.WriteLine("Press ENTER to exit.");
Console.Read();
}
private static void DoWork(object state)
{
//whatever
}
publicstaticvoidmain(字符串[]args)
{
int threads=Convert.ToInt32(File.ReadAllText(@“threads.txt”);
var workItems=新列表();
foreach(File.ReadLines(“x.txt”)中的字符串用户)
{
foreach(字符串传入File.ReadLines(“y.txt”))
{
foreach(File.ReadLines(“z.txt”)中的字符串行)
{
Add(新对象[]{user,pass,line});
}
}
}
var opts=new ParallelOptions(){MaxDegreeOfParallelism=threads};
var结果=Parallel.ForEach(工作项、选项、工作项);
控制台。WriteLine(“按ENTER键退出”);
Console.Read();
}
私有静态无效工作(对象状态)
{
//随便
}
这里是龙。你可能在排队等候大量的工作,顺便说一句,你很方便地没有发布这些工作。可能是您忘记了处理部分中的内容,//这里是我的代码,它向其他服务器发送请求并获取响应代码>?在这些文件中,我们可以期望有多少行?而内容的典型值是什么呢
?我想问一下这个想法,这个函数中的代码对我来说很好,我会用函数codeWell更新帖子,显然代码不是最优的,否则它不会停止工作,是吗?循环很好。添加到帖子中的函数代码>>x.txt、y.txt和z.txt中有多少字符串?谢谢,主要问题通过您的修改解决了,但还有一个问题似乎没有结果,因此我认为我需要给每个项目足够的时间,我尝试thread.sleap,但它会停止代码,请提供有关此问题的任何建议!!