1

I have an issue with SteamBot code. So, if someone adds "Bot" as friend then it will show in log who added. It does that, after that I added more code, but that part it just doesn't do. It will just log that someone added and seems like it returns true right away. Question is, why it isn't doing other part of code?

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;
       }
MicroVirus
  • 5,324
  • 2
  • 28
  • 53

2 Answers2

1

In order for a Stream to read or write it must be told that the unmanaged data it is reading from or writing to is finished, or that it does not have to keep on reading or writing.

Read on why closing / disposing streams is necessary.

Besides closing a stream you also need to dispose it. Disposing a Stream will automatically close it, so only a dispose is enough as well:

var stream = new FileStream();
// stuff;
stream.Dispose();

As the other answer suggests, you can also choose to use using() {} blocks on IDisposable objects (such as a Stream). This will ensure that at the end of the using block the object being 'used' will be disposed:

using(var stream = new FileStream()){
    //stuff;
}
Glubus
  • 2,819
  • 1
  • 12
  • 26
  • 1
    An explicit call to `Close` or `Dispose` for the streams are the same thing. The 'correct' (exception safe) thing to do is use a `using` block (or try-finally), hence the the advise on that MSDN page. – MicroVirus Mar 16 '16 at 13:28
  • *This method calls Dispose, specifying true to release all resources. You do not have to specifically call the Close method. Instead, ensure that every Stream object is properly disposed*. You're right. – Glubus Mar 16 '16 at 14:19
0

It's better to use using statements, it will handle the disposal for you

using (StreamReader createRead = new StreamReader(createResp.GetResponseStream()))
{
   ///
}
D.Kempkes
  • 345
  • 3
  • 5