How to display a processing component in a window form

I am using the download circle component when I want to read some data from RSS. I want to render this circle component to wait for the user.

here is my code:

private void btnUpdateRSS_Click(object sender, EventArgs e)
        {
            if (txtRSSGroup_Address.Text.Trim() == string.Empty)
                return;

            DialogResult dr = MsgBox.Show("اطلاعات قبلی از این RSS حذف شده و با اطلاعات جدید جایگزین می شود. \n \n آیا مطمئن به انجام عملیات به روز رسانی هستید؟", "هشدار", Mode.YesNo);

            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    string[] RSSNews;

                    loadingCircleFF.Visible = true;
                    string address = txtRSSGroup_Address.Text.Trim();
                    System.Threading.ThreadPool.QueueUserWorkItem((o) =>
                    {
                        RSSNews = Utility.RSSNews_Read(address);

                        for (int i = 0; i < RSSNews.Length; i++)
                        {
                            if (RSSNews[i] != null && RSSNews[i] != string.Empty)
                            {
                                RSS.RSSGroup_ID = RSSGroupID;
                                RSS.RSS_Content = RSSNews[i];
                                RSS.RSS_PersianDate = FreeControls.PersianDate.Now.ToString("YYYY/MM/dd");
                                RSS.User_FirstName = GlobalVariable.User_FirstName;
                                RSS.User_LastName = GlobalVariable.User_LastName;

                                RSS.Insert();
                            }
                            else
                                break;
                        }

                        this.BeginInvoke(new Action(() => { loadingCircleFF.Visible = false; }));
                    });

                    RSS.DeleteByGroup(RSSGroupID);

                    MsgBox.Show("عملیات به روز رسانی با موفقیت انجام شد.", "موفقیت", Mode.Information);
                    DTcancel_RSS(null, null);
                }
                catch
                {
                    MsgBox.Show("خطا در دریافت اطلاعات از RSS", "خطا", Mode.Information);
                }
            }

        }

      

but the problem is this:

before showing the loading circle, it will start reading from the RSS and my app will be blocked! so the download circle is never displayed.

How to solve this problem?

+3


source to share


1 answer


The problem is that your UI won't be able to redraw itself after you set loadingCircleFF.Visible = true;

, after queuing a work item into a workflow, your UI thread is busy processing RSS.DeleteByGroup(RSSGroupID);

and DTcancel_RSS(null, null);

so it won't render right away.

In my updated code, I placed them inside the body of the QueueUserWorkItem, so they will not block your UI thread.



Note that the work item runs on a worker thread to keep your application from hanging. And when you hide the CircleFF load after the RSSNews is read successfully, you have to use BeginInvoke to marshal the call to the UI thread, as you cannot access the CircleFF load from the worker thread;

try
{
    string[] RSSNews;

    loadingCircleFF.Visible = true;
    string address = txtRSSGroup_Address.Text.Trim();
    System.Threading.ThreadPool.QueueUserWorkItem((o) =>
    {
        RSSNews = Utility.RSSNews_Read(address);

        for (int i = 0; i < RSSNews.Length; i++)
        {
            if (RSSNews[i] != null && RSSNews[i] != string.Empty)
            {
                RSS.RSSGroup_ID = RSSGroupID;
                RSS.RSS_Content = RSSNews[i];
                RSS.RSS_PersianDate = FreeControls.PersianDate.Now.ToString("YYYY/MM/dd");
                RSS.User_FirstName = GlobalVariable.User_FirstName;
                RSS.User_LastName = GlobalVariable.User_LastName;

                RSS.Insert();
            }
            else
                break;
        }

        this.BeginInvoke(new Action(() => { 
            loadingCircleFF.Visible = false;
            //MessageBox.Show("عملیات به روز رسانی با موفقیت انجام شد.", "موفقیت");                            
            RSS.DeleteByGroup(RSSGroupID);

            //MessageBox.Show("عملیات به روز رسانی با موفقیت انجام شد.", "موفقیت");
            DTcancel_RSS(null, null);
        }));
        //If these methods don't access UI, call them normally, else wrap them with BeginInvoke.
        //RSS.DeleteByGroup(RSSGroupID);
        //DTcancel_RSS(null, null);
    });
}
catch
{
    MessageBox.Show("خطا در دریافت اطلاعات از RSS", "خطا");
}

      

+1


source







All Articles