C# - 在按下按钮之前使UI功能像无限循环一样

时间:2017-08-07 12:56:14

标签: c# winforms oledb

我在C#中创建一个代码,我从Access数据库中提取一些记录,但我需要在下一次迭代时依赖于单击按钮。我试过一些线程或任务,但它没有工作,因为它阻止了我需要它看到和可点击的用户界面。

以下是代码:

    bool nextClick = false ;

                while (readerSelect.Read())
                {

                    // show the correct panel
                    if (string.Compare(readerSelect[2].ToString(), "P1") == 0)
                    {
                        // panel with type 1
                        textBoxP1Text1.Text = readerSelect[3].ToString();
                        textBoxP1Text2.Text = readerSelect[4].ToString();
                        pictureBoxP1Image.ImageLocation = readerSelect[6].ToString();

                    }
                    else
                    {
                        // panel with type 2
                        textBoxP1Text2.Text = readerSelect[5].ToString();
                    }

    //this while need to be kind of infinite so the interation can't be processed and 
   //so when i need to change iteration i click the buttonNext 
                    while (!nextClick) {
                    startWhile:; 
                       MethodInvoker mi = delegate () {
                           if (nextClick)
                           {
                               Application.DoEvents(); 
                              // System.Windows.Forms.Application.Run();
                           }

                        };
                        this.Invoke(mi);
                        //break;
                        goto startWhile; 
                    }

 private void buttonNext_Click(object sender, EventArgs e)
    {
        // click on the next button
        nextClick = true; 
    }

2 个答案:

答案 0 :(得分:3)

您可以在异步任务中使用信号量,在每次点击期间按下Release按钮,并且每次都有等待它的while循环。这是一个简单的示例,使用添加了button1label1的表单:

public partial class Form1 : Form
{
    private readonly SemaphoreSlim signal = new SemaphoreSlim(0, int.MaxValue);

    public Form1()
    {
        this.InitializeComponent();
        this.RunLoop();
    }

    private async void RunLoop()
    {
        var i = 0;
        while (true)
        {
            this.label2.Text = $"Enqueued: {this.signal.CurrentCount}";
            await this.signal.WaitAsync(); // Wait button click async
            await Task.Delay(1000); // Simulate work
            this.label1.Text = $"Completed: {++i}";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.signal.Release();
        this.label2.Text = $"Enqueued: {this.signal.CurrentCount + 1}";
        // Or if you want to limit the # people can queue up, then put this whole
        // thing in an `if (signal.CurrentCount < myLimit)` block, and optionally
        // disable the button once limit has been reached, and re-enable it right
        // before the `WaitAsync` call above.
    }
}

答案 1 :(得分:2)

虽然Dax Fohl的回答有效,但您的设计中似乎遇到了问题。我认为你在Form课程中有太多的商业逻辑违反了Single Responsibility Principle

我建议将业务逻辑分解为自己的类。然后,您只需按下按钮单击事件处理下一条记录并显示结果,而不是遍历循环中的所有内容。这是我的意思的一个例子:

public partial class Form1 : Form
{
    private readonly DataProcessor dataProcessor = new DataProcessor();

    public Form1()
    {
        this.InitializeComponent();
    }

    private void button1Next_Click(object sender, EventArgs e)
    {
        this.buttonNext.Enabled = false;
        this.ProcessNext();
    }

    private async void ProcessNext()
    {
        string s = await this.dataProcessor.ProcessNext();
        this.textBoxP1Text1.Text = s;
        this.buttonNext.Enabled = true;
    }
}

public class DataProcessor
{
    private readonly Random r = new Random(); // Or reader or whatever.

    public async Task<string> ProcessNext() // Just using `string` as an example.
    {
        await Task.Delay(1000);
        return this.r.Next().ToString();
    }
}

我认为将来会更容易理解和更易于维护。当一个新的团队成员看到信号量(或你未来的自我)时,它很难理解/记住所有这一切的重点。在这里,你只需要一个本地函数,它可以完成一件事并且很容易理解。