【问题标题】:C# Sql Server "loading window"C# Sql Server“加载窗口”
【发布时间】:2011-08-20 19:30:21
【问题描述】:

这是我在这里的第一篇文章,但我经常使用这个网站来帮助我开发自己的应用程序,我应该说这个网站对我帮助很大,所以感谢大家。

现在我的问题是: 我正在开发我的第一个软件应用程序,它在 sql 服务器和应用程序本身之间交换数据。它是用 C# 开发的。从 sql server 数据库保存或检索数据没有问题。 我想要的是一种通知用户本地机器(安装应用程序的位置)和服务器之间的延迟的方法。我可以制作一些动画或简单地显示一些短信。我需要帮助的是如何创建在服务器通信时间运行时激活/触发/运行的代码。

如果您无法理解这个想法,请想象一个视频游戏。加载时(在某些游戏中),您可以在游戏开始前看到加载屏幕。当应用程序从/向服务器下载或上传数据时,我需要一些代码来显示“加载窗口”。

如果有任何代码示例或网站推荐,我将不胜感激。

PS:很抱歉文字内容太长,但我想确保每个人都能理解,所以我不必再重复一遍:P

【问题讨论】:

  • 您应该使用带有 ProgressBar 对象的新表单。您还将处理跨线程请求,我相信快速搜索会显示一些示例(例如if (this.InvokeRequired) { this.Invoke(aDelegateToMyFunction); } else { /*prossesing code here*/ })。

标签: c# sql


【解决方案1】:

【讨论】:

  • 那不……好?无论如何,我没有投票,因为香草链接不会削减它。请在答案中包含摘要和/或相关摘录(或在评论中添加链接)。
  • 实际上,我第一次通过只提供链接来投反对票。
【解决方案2】:

我在 2 年前开发了一个简单的 PleaseWait 类,但我没有更新这个类,它工作得很好,看看希望这会给你一个实现逻辑的想法。

public partial class frmWait : Form
{
    public frmWait()
    {
        InitializeComponent();
    }

    bool _isMoving = false;
    int _moveStart_x = 0;
    int _moveStart_y = 0;


    private void tmrProgress_Tick(object sender, EventArgs e)
    {
        if (barProgress.Value == barProgress.Maximum)
            barProgress.Value = barProgress.Minimum;
        else
            barProgress.Value += 1;
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        Close();
        PleaseWait.Abort();
    }
    protected override CreateParams CreateParams
    {
        get
        {
            System.Windows.Forms.CreateParams p = base.CreateParams;
            p.ClassStyle += 0x20000;
            p.ExStyle += 0x8000000;
            return p;
        }
    }

    protected override void WndProc(ref Message m)
    {
        const int WM_NCHITTEST = 132;

        base.WndProc(ref m);

        switch (m.Msg)
        {
            case WM_NCHITTEST:
                if (m.Result.ToInt32() == 1)
                    m.Result = new IntPtr(2);
                break;
        }
    }

    private void panelEx1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _isMoving = true;
            _moveStart_x = e.X;
            _moveStart_y = e.Y;
        }
    }

    private void panelEx1_MouseUp(object sender, MouseEventArgs e)
    {
        _isMoving = false;
    }

    private void pnlContainer_MouseMove(object sender, MouseEventArgs e)
    {
        if (_isMoving)
            this.Location = new Point(Location.X + e.X - _moveStart_x, Location.Y + e.Y - _moveStart_y);
    }
}

public class PleaseWait
{
    #region Static Operations
    private static Boolean _isAborted = false;
    private static Boolean _isVisible = false;
    private static frmWait _waitForm;
    private static String _waitingState = "";
    private static Boolean _autoClose = false;
    private static Boolean _cancelable = false;
    private static System.Threading.Thread _waiterThred;

    public delegate void CancelButtonPressed();

    public static event CancelButtonPressed OnCancel;

    public static Boolean AutoClose
    {
        get { return PleaseWait._autoClose; }
        set { PleaseWait._autoClose = value; }
    }
    public static string WaitingState
    {
        get { return PleaseWait._waitingState; }
        set { PleaseWait._waitingState = value; }
    }
    public static bool IsVisible
    {
        get { return _isVisible; }
        internal set { _isVisible = value; }
    }

    public static void ShowPleaseWait()
    {
        ShowPleaseWait("", _autoClose, false);
    }

    public static void ShowPleaseWait(string waitingState)
    {
        ShowPleaseWait(waitingState, _autoClose, false);
    }

    public static void ShowPleaseWait(bool autoClose)
    {
        ShowPleaseWait("", autoClose, false);
    }

    public static void ShowPleaseWait(string waitingState, bool autoClose, bool cancelable)
    {
        if (_waiterThred != null)
        {
            if (_isVisible)
            {
                // the please wait it woking, just continue and apply the changes
                _waitingState = waitingState;
                _autoClose = autoClose;
                _cancelable = cancelable;
                return;
            }
            else
            {
                _waiterThred.Abort();
                _waiterThred = null;
            }
        }

        _waitingState = waitingState;
        _autoClose = autoClose;
        _cancelable = cancelable;
        _isAborted = false;
        _isVisible = false;

        if (_autoClose)
            Application.Idle += new EventHandler(Application_Idle);



        _waiterThred = new System.Threading.Thread(DisplayWaitingForm);
        _waiterThred.IsBackground = true;
        _waiterThred.Name = "Please Wait....";
        _waiterThred.Start();
        Application.DoEvents();
    }

    public static void Abort()
    {
        _isAborted = true;
    }

    private static void Application_Idle(object sender, EventArgs e)
    {
        if (_autoClose)
            _isAborted = true;
    }


    private static void DisplayWaitingForm()
    {
        if (_waitForm != null)
        {
            if (!_waitForm.IsDisposed)
                _waitForm.Dispose();
            _waitForm = null;
            _isVisible = false;
        }
        try
        {
            if (_isAborted)
                return;

            _waitForm = new frmWait();
            if (_cancelable)
            {
                _waitForm.btnCancel.Enabled = true;
                _waitForm.btnCancel.Click += new EventHandler(btnCancel_Click);
            }
            try
            {
                _isVisible = true;
                _waitForm.Show();
                _waitForm.Focus();
                while (!_isAborted)
                {
                    System.Threading.Thread.Sleep(15);
                    _waitForm.lblMessage.Text = _waitingState;
                    Application.DoEvents();
                    _waitForm.lblMessage.Text = _waitingState;
                }
                _isVisible = false;
            }
            finally
            {
                FreeWaitingForm();
            }
        }
        finally
        {
            _isVisible = false;
        }
    }

    static void btnCancel_Click(object sender, EventArgs e)
    {
        if (_waitForm.InvokeRequired)
        {
            _waitForm.BeginInvoke(new EventHandler(btnCancel_Click), new object[] { e });
        }
        else
        {
            if (OnCancel != null)
                OnCancel.Invoke();
        }
    }

    private static void FreeWaitingForm()
    {
        _waitingState = "";
        _isVisible = false;
        if (_waitForm == null)
        {
            return;
        }
        _waitForm.Hide();
        if (!_waitForm.IsDisposed)
            _waitForm.Dispose();
        _waitForm = null;
    }

    #endregion
}

使用如下代码:

PleaseWait.ShowPleaseWait("Please wait", true, false);
// If second param is true then it will close the form automatically.
// If third param is true the it will expose a cancel button, so you can cancel your Asynchronous operations.

我没有插入设计代码,你看代码就明白了。

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    首先让我感谢您的回复。

    Toby 你的回答让我想到了线程监控我的 sql 连接,但这有点棘手和令人困惑,因为该应用仍在开发中并且将使用更多连接。

    S.Amani 回答这不是我想要的,但多亏了我找到了一种更简单的方法。我创建了一个表单(可以是其他任何形式),放置一个标签说:保存到数据库,取出顶部栏,定义位置并定义它的父级在显示时禁用并在关闭时启用。以下代码是我放入 DataBaseInteractionClass 中的代码

    private Wait myCustomWaitDialog = new Wait(); // My Waiting form
    
    private void SaveToDatabase(myObjectToSave obj) // Method called to save data do DB
    {
        // Create the connections and queries
        (...)
        // This is what I did
        // Show Waiting Form
        myCustomWaitDialog.Show();
        // Instanciate the command that will carry the query and to DB
        SqlCommand command = new SqlCommand(Queries.GetData(code), conn);
        // This is important
        //Create event that will fire when the command completes
        command.StatementCompleted += new StatementCompletedEventHandler(command_StatementCompleted);
        // Execute the transaction
        SqlDataReader reader = command.ExecuteReader();
        // Rest of the code (validations, close connections, try/catch, etc
        (...)
    }
    
    void command_StatementCompleted(object sender, StatementCompletedEventArgs e)
    {
        // This is the method that closes my Waiting Dialog 
        myCustomWaitDialog.CloseDialog();
        myCustomWaitDialog.Dispose();
    }
    

    这还不是我想要的,但它是迄今为止我找到的最佳解决方案。现在它会做:) 无论如何,感谢您的回复,我希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-03
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      相关资源
      最近更新 更多