0

When I minimize my form application and maximize it from notifyicon, When I repeat minimize and maximize action over and over again, after 2-3 times , My froms is freezing. There is no problem till I add switch case block but now when I remove the block, it goes on giving error.Why ? Can You Help me? Error is Here :

System.ArgumentException HResult=0x80070057 Message=Döngüsel denetim başvurusu yapıldı. Denetim kendi kendisinin sahibi olamaz veya kendisi tarafından üstü olarak atanamaz. Source=System.Windows.Forms StackTrace: at System.Windows.Forms.Control.CheckParentingCycle(Control bottom, Control toFind) at System.Windows.Forms.Form.set_Owner(Form value) at MetroFramework.Forms.MetroForm.RemoveShadow() at MetroFramework.Forms.MetroForm.OnClosed(EventArgs e) at System.Windows.Forms.Form.WmClose(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at MetroFramework.Forms.MetroForm.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at 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();
                }

            }
        }

    }
}
jazb
  • 5,498
  • 6
  • 37
  • 44
  • wow a lot of code here. i wonder if you might get a better response from the community if you used a [mcve] – jazb Dec 03 '18 at 10:38
  • You can't get help with code that needs so many Invoke() calls. Very risky, likely to cause deadlock. dwgHistory.Rows.Clear() is a threading race bug. Best to throw this away and work on version 2, strongly segregate code that needs to run on a worker thread from code that needs to run on the UI thread. – Hans Passant Dec 03 '18 at 10:46
  • Also delete the ShowInTaskbar assignments. They are very risky because they cause the window to be re-created. You don't need them, when you use Show/Hide then the taskbar button automatically gets shown/hidden as well. – Hans Passant Dec 03 '18 at 10:52
  • Please translate the error to English. – mjwills Dec 03 '18 at 10:53
  • It keels over because the form tries to set its Owner property to itself. MetroForm is not the kind of class that can deal well with threading bugs and spurious RecreateHandle() calls. – Hans Passant Dec 03 '18 at 10:58
  • When I delete ShowInTaskbar, It is solved, Thank You So Much @HansPassant, I will apply another advice. Thank you so much again. – setniparis Dec 03 '18 at 10:58

0 Answers0