BusyIndi​​cator永远不会停止

时间:2013-06-20 14:17:03

标签: wpf busyindicator

我必须加载大数据,因此我决定在加载时使用busyIndicator。 问题是,我显示加载数据文本,但它永远不会停止。

  public mainWindow()
        {
            InitializeComponent();
            rbi.IsBusy = true;
            Task.Factory.StartNew(getData);
        }


void getData()
        {

//method which loading data and inserting them into dataGrid
            Metoda();
            this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send,
                                       (Action)delegate
                                       {
                                           rbi.IsBusy = false;
                                       });

        }

BusyIndicator包含网格,因此它看起来像

<BusyIndicator>
<Grid>
<GridView>
....

@EDIT: Metod“Metoda”:

         void Metoda()
            {
            using (SqlConnection conn = new SqlConnection(cString.c_String))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("...", conn))
                {
//when entering there, it break and doesnt continue.
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            needUpdate = rdr.GetBoolean(rdr.GetOrdinal("needUpdate"));
                        }
                    }
                }
            }
            if(needUpdate)
            {
    //loading data into static list of 'Product' type (read from DB using dataReader and add into static list - prodList)
                 FastSellSearchClass.GetProducts(wID);
            }
            GetProducts("", -1);
            }

            void GetProducts()
            {
            (...)
            //set itemssource = static list from previous method
            gridView.ItemsSource = FastSellSearchClass.prodList;
            }

@ EDIT2: XAML文件:

    <Grid>
            <my:BusyIndicator Name="rbi" IsIndeterminate="True">
                <my:GridView  IsReadOnly="True" Name="productsDG"  AutoGenerateColumns="False">
                    <my:GridView.Columns>
                       (...columns...)
                    </my:GridView.Columns>
                </my:GridView>
            </my:BusyIndicator>

        </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

下面的代码对我有用。

 public Window2()
        {
            InitializeComponent();
            busyindicator1.IsBusy = true;
            Task.Factory.StartNew(CountTime);
        }

        public void CountTime()
        {
            for (int i = 0; i < 50; i++) //waiting for somthing.......
            {
                Thread.Sleep(100);
            }
            this.Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() =>
            {
                this.busyindicator1.IsBusy = false;
            }));
        }


  <Grid>
        <tk:BusyIndicator x:Name="busyindicator1" IsBusy="False">
        <TextBlock  Name="textBlock1" Text="Sample Window"  VerticalAlignment="Center" HorizontalAlignment="Center" />
        </tk:BusyIndicator>
    </Grid>