基于C#桌面的应用程序的加载器动画

时间:2012-11-15 02:33:56

标签: c# .net desktop-application loader

有没有一种类似于jquery ajax beforeSend并在C#中完成的方式?

因为在网络中我通常按下添加按钮时按下我设置beforendSend:功能以显示图像并隐藏complete:中的图像功能

现在我想在C#桌面应用程序中做。有没有类似的东西?喜欢某种使用进度条

3 个答案:

答案 0 :(得分:1)

这是一个winforms应用程序吗?它有一个你可以使用的ProgressBar控件。 WPF也有一个。但是您需要在后台线程上进行处理,以便您的UI保持响应并更新进度条。

答案 1 :(得分:1)

您需要执行后台处理和UI回调。下面是一个非常简单的例子:

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;

答案 2 :(得分:0)

你需要实现如下:

    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);
    }
}

它100%正确...现在目前在我的系统中工作。