D
D
Dima_Demichev2017-05-25 21:40:06
ASP.NET
Dima_Demichev, 2017-05-25 21:40:06

Why is C# ignoring validation?

I create a "bot" that changes the name of the conversation. But on the server, his roof is blown off and he doesn’t seem to pass the test, it turns out that he changes the name many times to the same one. And when I run the project in Visual Studio, everything is fine, and the name changes when it fails the check. Part of the code below.
Repeats every 2 seconds to "cycle" the bot.

TimerCallback a = new TimerCallback(Bot);
Timer timer = new Timer(a, 0, 0, 2000);

The code:
protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            // устанавливаем метод обратного вызова
            TimerCallback tm = new TimerCallback(Bot);
            // создаем таймер
            Timer timer = new Timer(tm, 0, 0, 2000);
        }

        public static void Bot(object obj)
        {
            string url = "https://api.vk.com/method/messages.getChat?access_token=TOKEN&chat_id=1";

            WebClient webClient = new WebClient();
            var response = webClient.DownloadString(url);
            var json = Encoding.UTF8.GetString(Encoding.GetEncoding(1251).GetBytes(response));
            JObject j = JObject.Parse(json);

            string title = (string)j["response"]["title"];
            int users = j["response"]["users"].Count();

            string name = "Тест, нас " + users;

            if (title != name)
            {
                url = "https://api.vk.com/method/messages.editChat?access_token=TOKEN&chat_id=1&title=" + name;
                webClient = new WebClient();
                response = webClient.DownloadString(url);
            }
        }

Accordingly title - Current title. name - new name.
while(true) has the same problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
John_Nash, 2017-05-26
@John_Nash

It's better to use Thread.Start(). Timers often fail for unknown reasons.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question