Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8703285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:50:33+00:00 2026-06-13T02:50:33+00:00

I have found this : How to show a loading graphic/animation when wpf data

  • 0

I have found this : How to show a loading graphic/animation when wpf data binding is taking place

But don’t understand how to apply it for my work.

I have My MainWindow. It call all my user controls in it.

On a user control, I have a DataGrid. after press “Go” button, the datagrid load data from MySQL. It take a long time to do and I want to show a dialog window with “Please Wait” during the loading of the datagrig.

I have found the link below but don’t understant how to call it correctly.

Do I need to put this “loader” in a new class file like “Loader.cs” andthe button call it ? Ok but how to close it when datagrid is finished ?

I’m lost… Well if an other solution exist and simply to use…

Thanks by advance

EDIT TEST 1 :

Tried an simple test with slider to get a time to wait and a button.

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace TEST_LoadingPage
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnTester_Click(object sender, RoutedEventArgs e)
    {
        Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
        waitWindow.Content = new TextBlock { Text = "Veuillez patienter...", FontSize = 30, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };

        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += delegate
        {
            Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
            lbReussite.Visibility = Loop.Pause(slider.Value);
            Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
        };
        worker.RunWorkerAsync();
    }
}

public class Loop
{
    public static System.Windows.Visibility Pause(double duree)
    {
        try
        {
            DateTime begin = new DateTime();
            DateTime end = new DateTime();
            int i = 0;
            begin = DateTime.Now;
            end = DateTime.Now.AddSeconds(duree);

            while (DateTime.Now <= end)
                i++;

            return System.Windows.Visibility.Visible;
        }
        catch (Exception)
        {
            return System.Windows.Visibility.Hidden;
        }
    }
}
}

but don’t work with error :

The calling thread can not access this object because a different thread owns it

I know its a courant error but I don’t see “DispatcherTimer” or else that I’ve seen before in precedent project… So I’ll try the second code tomorrow BUT. I don’t understand where i put my method 🙁

EDIT 2

I tried with your code… I’m too stupide maybe.

I’ve write the class in a Loader.cs and in my MainWiondow (its a test)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnTester_Click(object sender, RoutedEventArgs e)
    {
        Loader<List<Donnees>> loader = new Loader<List<Donnees>>(GenerateList((int)slider.Value);
        loader.JobFinished += new Loader<Donnees>.OnJobFinished(loader_JobFinished);
        loader.Load();

    }

    private List<Donnees> GenerateList(int number)
    {
        List<Donnees> list = new List<Donnees>();
        for (int i = 1; i <= number; i++)
        {
            Donnees data = new Donnees();
            data.Id = i;
            data.Name = "Ligne " + i;
            list.Add(data);
        }
        return list;
    }

    void loader_JobFinished(object sender, List<Donnees> result)
    {
        result = GenerateList((int)slider.Value);
        dgDataGrid.ItemsSource = result;
    }

}

public class Donnees
{
    #region Properties

    private int id;
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    #endregion

    public Donnees()
    {
        id = -1;
        name = "";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:50:34+00:00Added an answer on June 13, 2026 at 2:50 am

    You put the following code in a method in the code-behind of your DataGrid-UserControl which gets called in your button-click-eventhandler:

    Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
    waitWindow.Content = new TextBlock { Text = "Please Wait", FontSize = 30, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
    

    Now you can decide…

    …if you want to create a DataLoader-class, which loads the data and fires an DataLoaded event when completed, than add:

    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += delegate
    {
        Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
        DataLoader dataLoader = new DataLoader();
        dataLoader.DataLoaded += delegate
        {
            Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
        };
    
        dataLoader.LoadData();
    };
    
    worker.RunWorkerAsync();
    

    … or if you just copy your data-loading-code into this method and add:

    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += delegate
    {
        Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
        //do dataloading here
        Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
    };
    worker.RunWorkerAsync();
    

    EDIT: I wrote a class, which should do what you want without much code in the code-behind:

    public class Loader<TFuncResult,TFirstArgType>:FrameworkElement
    {
        private Func<TFirstArgType,TFuncResult> _execute;
        public TFuncResult Result { get; private set; }
    
        public delegate void OnJobFinished(object sender, TFuncResult result);
        public event OnJobFinished JobFinished;
    
        public Loader(Func<TFirstArgType,TFuncResult> execute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
    
            _execute = execute;
        }
    
        private Window GetWaitWindow()
        {
            Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
            waitWindow.Content = new TextBlock { Text = "Please Wait", FontSize = 30, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
    
            return waitWindow;
        }
    
        public void Load(TFirstArgType firstarg, Window waitWindow = null)
        {
            if (waitWindow == null)
            {
                waitWindow = GetWaitWindow();
            }
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += delegate
            {
                Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));
                Result = _execute(firstarg);
                Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
            };
            worker.RunWorkerCompleted += delegate
            {
                worker.Dispose();
                if (JobFinished != null)
                {
                    JobFinished(this, Result);
                }
            };
            worker.RunWorkerAsync();
        }
    }
    

    EDIT 2: How to use it:

    Lets say your GenerateList() returns Data of Type List<Donnees> and the arguments Type is of Type int (works with all Types):

    Place this where you want to load the Data (e.g. in your Window_Loaded-Event):

    Loader<List<Donnees>, int> loader = new Loader<List<Donnees>, int>(GenerateList);
    loader.JobFinished += new Loader<List<Donnees>, int>.OnJobFinished(loader_JobFinished);
    loader.Load((int)slider.Value);
    

    Now call this EventHandler in the Code-Behind:

    void loader_JobFinished(object sender, List<Donnees> result)
    {
        YourDataGrid.DataSource = result
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[I have this working, but I don't understand why my fix to make it
like in this post here by exemple I have found this blog but in
This may have been answered but my search hasn't found what I was looking
Although I have research this topic I don't really seem to understand how to
I have found this link: http://www.jfree.org/forum/viewtopic.php?f=3&t=6314 Its back in 2007 where they agree that
I have found this line of MatLab code on the internet that displays a
I have found this answer really useful. It helps me plot network/graphs and select
I have found this project on Codeplex. http://www.codeplex.com/ProjNET I need to integrate this code
I have found this code for reverse geocoding: var point = new GLatLng (lat[1],long[1]);
I have found this line of code in a game that I study int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.