F
F
Farid Jennetov2018-01-10 18:49:13
C++ / C#
Farid Jennetov, 2018-01-10 18:49:13

C# interface freezes, what should I do?

The interface hangs after pressing the "enable" button, i.e. process start.
Tried async - doesn't work. I also ran it in a separate thread - it's useless
Form code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VkNet;
using VkNet.Enums.Filters;
using System.IO;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;

namespace КикМашина
{
    public partial class beseda : Form
    {
        VkApi vk = new VkApi();
        long chatid;
        long userid;
        bool _reg = false;
        
        
        //String 

        ulong appID = 1234562;
        string login = "login";
        string pass = "pass";
        Settings scope = Settings.All;
        static string directory = AppDomain.CurrentDomain.BaseDirectory + "\\Chats.txt";
        static string[] chats = File.ReadAllLines(directory);
        bool end = false;
        

        public beseda()
        {
            if (!File.Exists(directory)) File.Create(directory);
            //Program.Registry();
            vk.Authorize(new ApiAuthParams
            {
                ApplicationId = appID,
                Login = login,
                Password = pass,
                Settings = scope,

            });
            InitializeComponent();

            for (int i = 0; i < chats.Length; i++)
            {
                comboBox1.Items.Add(vk.Messages.GetChat(Convert.ToInt64(chats[i])).Title);
            }
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            Form f2 = new Form2();
            f2.Show();
            Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int selectedIndex = comboBox1.SelectedIndex;
            
            
            if (end == false)
            {
                Program.chat_one(Convert.ToInt32(chats[selectedIndex]), textBox1.Text, end, richTextBox1.Text);
                end = true;
            }
            else
            {
                Program.chat_one(Convert.ToInt32(chats[selectedIndex]), textBox1.Text, end, richTextBox1.Text);
                end = false;
            }
        }

        public void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            string json1 = richTextBox1.Text;
        }
    }
}

Code in Program.cs
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using VkNet;
using VkNet.Enums.Filters;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;

namespace КикМашина
{
    static class Program
    {
        static VkApi vk = new VkApi();
        static ulong appID = 543263786;
        static string login = "login";
        static string pass = "passk";
        static Settings scope = Settings.All;

        static bool _reg = false;
        /// <summary>
        /// Главная точка входа для приложения.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        public static void Registry()
        {
            if (_reg == true) return;
            vk.Authorize(new ApiAuthParams
            {
                ApplicationId = appID,
                Login = login,
                Password = pass,
                Settings = scope
            });
            _reg = true;
        }
        public async static Task Longpoll(int chat_id, string chat_name, string json_text)
        {
            var updates = (JContainer)JObject.Parse(json_text)["updates"];
            dynamic results = updates.Descendants()
                 .OfType<JObject>()
                 .Where(x => x["source_text"] != null).FirstOrDefault();
            try
            {
                if (!((results.source_text) == chat_name))
                {
                    await vk.Messages.EditChatAsync(Convert.ToInt64(chat_id), chat_name);
                }
            }
            catch { }
        }
        public async static void Httpkek(int chat_id, string chat_name)
        {
            string server = vk.Messages.GetLongPollServer(false, 2).Server;
            string key = vk.Messages.GetLongPollServer(false, 2).Key;
            int ts = Convert.ToInt32(vk.Messages.GetLongPollServer(false, 2).Ts);
            string str = "https://" + server + "?act=a_check&key=" + key + "&ts=" + ts + "&wait=25&mode=2&version=2";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(str);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            StringBuilder builder = new StringBuilder();
            string json_text = builder.Append(reader.ReadToEnd()).ToString();
            await Longpoll(chat_id, chat_name, json_text);
        }
        public static void on_thread_first(int chat_id, string chat_name, bool end, string json)
        {
            var chatone = new Thread(() =>
            {
                cycle(chat_id, chat_name, end, json);
            });
            if (end == false)
            {
                chatone.Start();
            }
            if(end == true)
            {
                chatone.Abort();
            }
        }
        public static Task cycle(int chat_id, string chat_name, bool end, string json)
        {
            while (true)
            {
                Httpkek(chat_id, chat_name);
            }
        }
        public static void chat_one(int chat_id, string chat_name, bool end, string json)
        {
            if (_reg == false) Registry();
            cycle(chat_id, chat_name, end, json);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question