Когда я сворачиваю, чтобы уведомить, а после этого максимизирую его 2-3 раза, форма зависает и иногда выдает ошибку - PullRequest
0 голосов
/ 03 декабря 2018

Когда я минимизирую свое приложение формы и максимизирую его из уведомлений, Когда я повторяю минимизировать и максимизировать действие снова и снова, через 2-3 раза, My froms замораживается.Нет проблем, пока я не добавлю блок регистра переключателей, но теперь, когда я удаляю блок, он продолжает выдавать ошибку. Почему?Вы не могли бы мне помочь?Ошибка здесь:

System.ArgumentException HResult = 0x80070057 Сообщение = Döngüsel denetim başvurusu yapıldı.Денетим кенди кендисинин сахиби оламаз вея кендиси тарафиндан юстю оларак атанамаз.Source = System.Windows.Forms StackTrace: в System.Windows.Forms.Control.CheckParentingCycle (элемент управления, Control toFind) в System.Windows.Forms.Form.set_Owner (значение формы) в MetroFramework.Forms.MetroForm.RemoveShadow ()в MetroFramework.Forms.MetroForm.OnClosed (EventArgs e) в System.Windows.Forms.Form.WmClose (Message & m) в System.Windows.Forms.Form.WndProc (Message & m) в MetroFramework.Forms.MetroForm.WndProc (Сообщение &m) в System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m) в System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m) в System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Security.Cryptography;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Timers;

namespace webService_Synchronization
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        domainService.controlService nsService = new domainService.controlService();
        DataTable dt = new DataTable();
        cryptography cryptography = new cryptography();
        static SemaphoreSlim semaphore = new SemaphoreSlim(1);
        System.Timers.Timer aTimer = new System.Timers.Timer();
        int succeed, error, attention;



        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            double timerIntervali = calcInterval();
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = timerIntervali;
            aTimer.Enabled = true;



            controlService.demodata demodata = new controlService.demodata();

            dt = demodata.domains("test.com");
        }

        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string Domain = dt.Rows[i][0].ToString();
                string Folder = dt.Rows[i][1].ToString();

                MD5 md5Hash = MD5.Create();
                string md5hashDomain = cryptography.GetMd5Hash(md5Hash, Domain);
                string sha1hashDomain = cryptography.GetSHA1(Domain);

                syncIT(i, Domain, Folder, md5hashDomain, sha1hashDomain);
                if (i==2) { break; }
            }
            aTimer.Interval = calcInterval();


        }

        public double calcInterval()
        {
            DateTime nowTime = Convert.ToDateTime(DateTime.Now.ToLongTimeString());
            if (0 > (DateTime.Compare(nowTime, Convert.ToDateTime("12:00"))))
            {
                TimeSpan differenceMillisecond = Convert.ToDateTime("12:00:00") - nowTime;
                return differenceMillisecond.TotalMilliseconds;
            }
            else
            {
                TimeSpan differenceMillisecond = Convert.ToDateTime("23:59:59") - nowTime;
                return differenceMillisecond.TotalMilliseconds;
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
            {
                ntfytestSenkronizasyon.Visible = true;
                this.ShowInTaskbar = false;
                this.Visible = false;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ntfytestSenkronizasyon != null)
            {
                if (ntfytestSenkronizasyon.Visible)
                {
                    ntfytestSenkronizasyon.Visible = false;
                }
            }
        }

        private void ntfytestSenkronizasyon_MouseClick(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Right:
                    testSenk.Show();
                    break;
                case MouseButtons.Left:
                    ShowInTaskbar = true;
                    Show();
                    WindowState = FormWindowState.Normal;
                    break;
            }
        }

        private void show_Click(object sender, EventArgs e)
        {
            ShowInTaskbar = true;
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void close_Click(object sender, EventArgs e)
        {
            ntfytestSenkronizasyon.Visible = false;
            Environment.Exit(0);
        }

        private void btnsyncIT_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string Domain = dt.Rows[i][0].ToString();
                string Folder = dt.Rows[i][1].ToString();

                MD5 md5Hash = MD5.Create();
                string md5hashDomain = cryptography.GetMd5Hash(md5Hash, Domain);
                string sha1hashDomain = cryptography.GetSHA1(Domain);

                syncIT(i, Domain, Folder, md5hashDomain, sha1hashDomain);
                if (i == 2) { break; }
            }

        }

        public async Task syncIT(int a, string _domain, string _filePath, string _md5Domain, string _sha1Domain)
        {
            lblattention.Invoke(new Action(() => lblattention.Text = "attention: "));
            lblerror.Invoke(new Action(() => lblerror.Text = "error: "));
            lblsucceed.Invoke(new Action(() => lblsucceed.Text = "succeeded: "));
            attention = 0; error = 0; succeed = 0;
            dgwHistory.Rows.Clear();
            dgwHistory.Refresh();
            if (!(_domain.ToLower()).Contains("localhost"))
            {

                await semaphore.WaitAsync();
                btnsyncIT.Invoke(new Action(() => btnsyncIT.Enabled = false));

                try
                {
                    metroLabel1.Invoke(new Action(() => metroLabel1.Text = _domain + " Your process is continuing"));
                    nsService.Url = "http://www." + _domain + "/Api/controlService.asmx";
                    nsService.Timeout = -1;

                    domainService.ServiceResult senk = await Task.Run(() => nsService.Senkronizasyon(_domain, _filePath, _md5Domain, _sha1Domain));
                    metroLabel1.Invoke(new Action(() => metroLabel1.Text = "Completed..."));
                    bool status = senk.Status;

                    string durum = "Attention";
                    if (status) { durum = "succeeded"; }
                    string statusMessage = senk.StatusMsg;

                    if (durum == "succeeded")
                    {
                        dgwHistory.Invoke(new Action(() => dgwHistory.Rows.Add(_domain, durum, statusMessage, DateTime.Now)));
                        dgwHistory.Invoke(new Action(() => dgwHistory.Rows[dgwHistory.Rows.Count - 2].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#baffc9")));
                        ++succeed;
                        lblsucceed.Invoke(new Action(() => lblsucceed.Text = "succeeded: " + succeed));
                    }
                    else
                    {
                        dgwHistory.Invoke(new Action(() => dgwHistory.Rows.Add(_domain, durum, statusMessage, DateTime.Now)));
                        dgwHistory.Invoke(new Action(() => dgwHistory.Rows[dgwHistory.Rows.Count - 2].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#ffffba")));
                        ++attention;
                        lblattention.Invoke(new Action(() => lblattention.Text = "attention: " + attention));
                    }
                }
                catch (Exception e)
                {
                    dgwHistory.Invoke(new Action(() => dgwHistory.Rows.Add(_domain, "Attention", "We cannot access this domain!", DateTime.Now)));
                    dgwHistory.Invoke(new Action(() => dgwHistory.Rows[dgwHistory.Rows.Count - 2].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#ffb3ba")));
                    ++error;
                    lblerror.Invoke(new Action(() => lblerror.Text = "error: " + error));

                }
                finally
                {
                    btnsyncIT.Invoke(new Action(() => btnsyncIT.Enabled = true));
                    ntfytestSenkronizasyon.Visible = true;
                    ntfytestSenkronizasyon.ShowBalloonTip(360000000, "Program is Running", "succeeded: " + succeed + "      attention: " + attention + "    error: " + error, ToolTipIcon.Info);
                    semaphore.Release();
                }

            }
        }

    }
}
...