Loader animation for C# desktop based application

2019-09-12 15:22发布

问题:

Is there a way that similar in jquery ajax beforeSend and complete in C#?

Because in web I usually do when pressing like an add button i set beforendSend: a function to show an image and hide image function in complete:

And now I want to do in C# desktop application. Is there any similar to that? like some kind of using a progress bar

回答1:

Is this a winforms application? It has a ProgressBar control you can use. There is one for WPF as well. But you will want to do your processing on a background thread so your UI remains responsive and updates your progress bar.



回答2:

You will need to perform background processing and UI callbacks. Very simple example below:

private void button3_Click(object sender, EventArgs e)
    {
        ProcessingEvent += AnEventOccurred;

        ThreadStart threadStart = new ThreadStart(LongRunningProcess);
        Thread thread = new Thread(threadStart);
        thread.Start();
    }

    private void LongRunningProcess()
    {
        RaiseEvent("Start");

        for (int i = 0; i < 10; i++)
        {
            RaiseEvent("Processing " + i);
            Thread.Sleep(1000);
        }

        if (ProcessingEvent != null)
        {
            ProcessingEvent("Complete");
        }
    }

    private void RaiseEvent(string whatOccurred)
    {
        if (ProcessingEvent != null)
        {
            ProcessingEvent(whatOccurred);
        }
    }

    private void AnEventOccurred(string whatOccurred)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new Processing(AnEventOccurred), new object[] { whatOccurred });
        }
        else
        {
            this.label1.Text = whatOccurred;
        }
    }

    delegate void Processing(string whatOccurred);

    event Processing ProcessingEvent;


回答3:

You need to implement like below:

    FrmLoading f2 = new FrmLoading();   // Sample form whose Load event takes a long time
    using (new PleaseWait(this.Location, () => Fill("a")))  // Here you can pass method with parameters
          { f2.Show(); }

PleaseWait.cs

public class PleaseWait : IDisposable
{
    private FrmLoading mSplash;

    //public delegate double PrdMastSearch(string pMastType);
    public PleaseWait(Point location, Action methodWithParameters)
    {
        //mLocation = location;
        Thread t = new Thread(workerThread);
        t.IsBackground = true;
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
        methodWithParameters();
    }
    public void Dispose()
    {
        mSplash.Invoke(new MethodInvoker(stopThread));
    }
    private void stopThread()
    {
        mSplash.Close();
    }
    private void workerThread()
    {
        mSplash = new FrmLoading();   // Substitute this with your own
        mSplash.StartPosition = FormStartPosition.CenterScreen;
        //mSplash.Location = mLocation;
        mSplash.TopMost = true;
        Application.Run(mSplash);
    }
}

It works 100% correct ... now currently worked in my system.