C# 在代码完成之前,控制台程序会无故关闭自身
我的控制台应用程序在代码完成之前退出(在执行/尝试执行“console.WriteLine”或“inti=0”等行之后) 我添加了一些断点,以查看在程序退出之前最后执行哪一行。在前几次尝试中,程序在尝试执行以下行后/时退出:C# 在代码完成之前,控制台程序会无故关闭自身,c#,console,console-application,C#,Console,Console Application,我的控制台应用程序在代码完成之前退出(在执行/尝试执行“console.WriteLine”或“inti=0”等行之后) 我添加了一些断点,以查看在程序退出之前最后执行哪一行。在前几次尝试中,程序在尝试执行以下行后/时退出: Console.WriteLine($"Online users to chat with ({users.Length} total):"); 然后,我尝试使用即时窗口在Console.WriteLine括号中获取字符串的值,这很有效。我按下F10继续下一行,它成功地打
Console.WriteLine($"Online users to chat with ({users.Length} total):");
然后,我尝试使用即时窗口在Console.WriteLine括号中获取字符串的值,这很有效。我按下F10继续下一行,它成功地打印了给定的参数。但是,按下F10后,当突出显示的部分为“int i=0”时,程序自动关闭:
for(int i=0;i
下面是代码,我已经在程序目前为止关闭的所有行上添加了注释:
public static void Main()
{
client = new WebClient();
messages = null;
try
{
LocalhostServer.Start("Chat");
// Starts a server with System.Net.HttpListener. The prefix added is: http://localhost:<port>/Chat/
}
catch { }
LocalhostServer.RecievedRequest += LocalhostServer_RecievedRequest;
Console.WriteLine("Enter your name:");
name = Console.ReadLine();
// closed here^^ after adding a new breakpoint to this line, makes no sense to me
try
{
client.DownloadString(new Uri(Url + "AddUser?name=" + name));
// Url is a static string which is set to: "http://localhost:<port>/Chat/"
}
catch { }
ShowOnlineUsers();
}
private static async void ShowOnlineUsers()
{
try
{
thread = new Thread(CheckIfChatStarted);
thread.Start();
Console.Clear();
string[] users = (await DownloadStringUntilSuccess(Url + "GetOnlineUsers")).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// closed here^^ after adding a new breakpoint to this line
Console.WriteLine($"Online users to chat with ({users.Length} total):"); // closed here
for (int i = 0; i < users.Length; i++) // closed here on "int i = 0;"
{
Console.WriteLine($"{i + 1}. {users[i]}");
}
Console.WriteLine("\nEnter user number to chat with:");
recipient = users[int.Parse(Console.ReadLine()) - 1];
}
catch (Exception ex)
{ // hasn't reached the breakpoint on that bracket
}
client.DownloadString(new Uri(Url + "StartChat?name=" + name + "&recipient=" + recipient));
}
publicstaticvoidmain()
{
客户端=新的WebClient();
消息=null;
尝试
{
LocalhostServer.Start(“聊天”);
//使用System.Net.HttpListener启动服务器。添加的前缀为:http://localhost:/Chat/
}
捕获{}
LocalhostServer.ReceivedRequest+=LocalhostServer\u ReceivedRequest;
Console.WriteLine(“输入您的姓名:”);
name=Console.ReadLine();
//在向此行添加新断点后在此处关闭^
尝试
{
client.DownloadString(新Uri(Url+“AddUser?name=“+name));
//Url是一个静态字符串,设置为:http://localhost:/Chat/"
}
捕获{}
ShowOnlineUsers();
}
私有静态异步void ShowOnlineUsers()
{
尝试
{
线程=新线程(CheckIfChatStarted);
thread.Start();
Console.Clear();
字符串[]用户=(等待下载StringUntillSuccess(Url+“GetOnlineUsers”)).Split(新字符[]{',},StringSplitOptions.RemoveEmptyEntries);
//在向此行添加新断点后在此处关闭^
Console.WriteLine($“与({users.Length}total)聊天的在线用户):”;//在此处关闭
for(inti=0;i
我不知道为什么会这样。。。非常感谢任何人的帮助
p.S-这是否是由于主线程等待用户输入(Console.ReadLine)时后台线程中执行Console.Clear/Console.WriteLine命令造成的?如果是,是否有解决方案/解决办法?尝试添加
Console.Readline()
要保持控制台打开…请尝试添加
Console.Readline()
要使控制台保持打开状态…请使用try/catch和output exception message环绕所有控制台。还要在catch子句中放置一个断点。我相信您正在接受一个异常。另外,您是否可以添加整个
main()
方法?这会有帮助,因为目前我不知道用户来自哪里。哦,欢迎来到SO@ZivB方法中是否有任何async
调用?users
是如何初始化和填充的?您没有显示任何会导致您描述的问题的代码。请发布一个小样本(不一定是你的全部代码)来重现这个问题。用try/catch和output异常消息来环绕所有这些。还要在catch子句中放置一个断点。我相信您正在接受一个异常。另外,您是否可以添加整个main()
方法?这会有帮助,因为目前我不知道用户来自哪里。哦,欢迎来到SO@ZivB方法中是否有任何async
调用?users
是如何初始化和填充的?您没有显示任何会导致您描述的问题的代码。请发布一个小样本(不一定是你所有的代码)来重现这个问题。我想他已经有了,这一行recipient=users[int.Parse(Console.ReadLine())-1]代码>我想他已经有了,这行代码recipient=users[int.Parse(Console.ReadLine())-1]代码>