阅读背景:

【winform】主窗体多线程给子窗体传值

来源:互联网 

1.主窗体多线程给子窗体传值

解决方案:主要使用委托,因为会出现跨线程错误
主窗体

        public FormMain()
        {
            InitializeComponent();

            //background thread running
            Action sAction = async () => await CallbackWorkItem();
            System.Threading.Tasks.Task task = System.Threading.Tasks.Task.Run(sAction);
            System.Threading.Tasks.Task.WaitAll(task);
        }




        private async Task CallbackWorkItem()
        {
            List<string> filesGuidList = new List<string>(); //the GUID to download
           

            while (true)
            {
                try
                {
                     int count = int.Parse(COUNT);

                    if (string.IsNullOrWhiteSpace(DOWNPATH))
                    {
                        MessageBox.Show("the config's key for DOWNPATH is empty.");
                        return;
                    }

                    if (!string.IsNullOrWhiteSpace(PROJECTID) && !string.IsNullOrWhiteSpace(HOID))
                    {
                        filesGuidList.Clear();

                        //1.Check
                        bool checkTask = await CheckIds(Guid.Parse(PROJECTID), Guid.Parse(HOID));

                        if (checkTask)
                        {
                            deliveryFile.SetMonitor(deliveryFile,"download begins.", COUNT);
                            //2.Load
                            var files = await LoadDeliveryFiles(Guid.Parse(PROJECTID), Guid.Parse(HOID), TEMPKEY, count);

                            if (count == 1)
                            {
                                deliveryFile.SetMonitor(deliveryFile,$"download the data: {files[0].Name}", COUNT);
                            }
                            else
                            {
                                deliveryFile.SetMonitor(deliveryFile,$"download the {COUNT} data.", COUNT);
                            }


                            foreach (var file in files)
                            {
                                filesGuidList.Add(file.Guid.ToString());
                            }


                            string fileZipName = string.Empty;
                            //3.download
                            if (DownloadFile(Guid.Parse(PROJECTID), filesGuidList, out fileZipName))
                            {
                                DeleteRedis(Guid.Parse(PROJECTID), filesGuidList, fileZipName, TEMPKEY);
                                deliveryFile.SetMonitor(deliveryFile,"end of download.", COUNT);
                            }
                        }
                        else
                        {
                            deliveryFile.SetMonitor(deliveryFile,"no download data.", COUNT);
                           await Task.Delay(1000);
                        }
                    }
                    deliveryFile.SetMonitor(deliveryFile,"rest 7 seconds...", COUNT);

                    await Task.Delay(7000);

                }
                catch (FormatException e)
                {
                    MessageBox.Show("the config's key for PROJECTID or HOID must be GUID,and COUNT must be number.");
                    await Task.Delay(60000); //if throw exception,re-execute after 60 seconds
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    await Task.Delay(60000); //if throw exception,re-execute after 60 seconds
                }
            }
        } 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: