Зависает интерфейс после нажатия на кнопку "включить", т.е. запуска процесса.
Пробовал асинхрон - не работает. Так же запускал в отдельном потоке - бесполезно
Код формы
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;
}
}
}
Код в 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);
}
}
}