C#Void不';除了日志和返回true之外,您不做任何其他事情吗?
我对SteamBot代码有问题。所以,如果有人添加“机器人”作为好友,那么它将在日志中显示添加者。它做到了这一点,在那之后我添加了更多的代码,但那部分它就是做不到。它只会记录有人添加的内容,看起来它会立即返回true。问题是,为什么它不做代码的其他部分C#Void不';除了日志和返回true之外,您不做任何其他事情吗?,c#,steambot,C#,Steambot,我对SteamBot代码有问题。所以,如果有人添加“机器人”作为好友,那么它将在日志中显示添加者。它做到了这一点,在那之后我添加了更多的代码,但那部分它就是做不到。它只会记录有人添加的内容,看起来它会立即返回true。问题是,为什么它不做代码的其他部分 public override bool OnFriendAdd() { Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) +
public override bool OnFriendAdd()
{
Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") added me!");
HttpWebRequest check =
check.Method = "GET";
HttpWebResponse checkResp;
try
{
checkResp = check.GetResponse() as HttpWebResponse;
StreamReader createRead = new StreamReader(checkResp.GetResponseStream());
string resultCA = createRead.ReadToEnd();
if (resultCA.Contains("success"))
{
Regex SomeNameHere3 = new Regex("\"\" : \"([A-Za-z0-9]+)");
string swag3 = SomeNameHere3.Match(resultCA).Value;
swag3 = swag3.Replace("\"\" : \"", "");
currentSID = currentSID.ConvertToUInt64();
Log.Success("User: " + Bot.SteamFriends.GetFriendPersonaName(currentSID) + " : " + swag3);
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
}
}
catch (WebException ex)
{
checkResp = ex.Response as HttpWebResponse;
StreamReader createRead = new StreamReader(checkResp.GetResponseStream());
string resultCA = createRead.ReadToEnd();
if (resultCA.Contains("fail") && resultCA.Contains("Label does not exist"))
{
HttpWebRequest create =
create.Method = "GET";
HttpWebResponse createResp = (HttpWebResponse)create.GetResponse();
StreamReader createSR = new StreamReader(createResp.GetResponseStream());
string createSRRTE = createSR.ReadToEnd();
if (createSRRTE.Contains("success"))
{
Regex SomeNameHere3 = new Regex("\"\" : \"([A-Za-z0-9]+)");
string get = SomeNameHere3.Match(createSRRTE).Value;
get = get.Replace("\"\" : \"", "");
Log.Success("User: " + Bot.SteamFriends.GetFriendPersonaName(currentSID) + " : " + get);
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, ");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
Bot.SteamFriends.SendChatMessage(currentSID, type, "");
}
}
}
return true;
}
最好使用using语句,它将为您处理处置
using (StreamReader createRead = new StreamReader(createResp.GetResponseStream()))
{
///
}
要使
流
读取或写入,必须告知它正在读取或写入的非托管数据已完成,或者它不必继续读取或写入
除了关闭流之外,还需要处理它。处置流将自动关闭它,因此仅处置也足够:
var stream = new FileStream();
// stuff;
stream.Dispose();
正如另一个答案所建议的,您还可以选择在IDisposable
对象(例如流
)上使用using(){}
块。这将确保在使用
块结束时,正在“使用”的对象将被处置:
using(var stream = new FileStream()){
//stuff;
}
您的streamwriter等待它关闭。您需要显式地编写:createSR.Close();createSR.dispose();谢谢,它为我解决了这个问题。在那里写解决方案不是这个论坛的工作方式。你们应该让@Glubus写一个答案,然后选择正确的答案。@Glubus你们能写和答案一样的东西吗?所以我可以接受,我已经创建了一个答案。为流显式调用
Close
或Dispose
是一样的。“正确”(异常安全)的做法是使用using
块(或try finally),因此在该MSDN页面上会显示advice。此方法调用Dispose,指定true以释放所有资源。您不必专门调用Close方法。相反,请确保每个流对象都已正确处理。你说得对。