简体   繁体   中英

How to disable textbox while another thread is running

I want to disable the textbox while a thread is running. After the execution of thread has finished the textbox on the form should be enabled.

CODE

Thread thread = new Thread(new ThreadStart(ScannerThreadFunction));
thread.Start();

    public void ScannerThreadFunction()
    {            
        try
        {
            Scan();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
        }
    }

TextBox should be disabled until Scan() is running. After the scan() is completed, i want to enable the TextBox.

In WPF, you can do this during the scan method. You just need to push the enabling and disabling of the TextBox onto the UI thread, and you do this with the dispatcher like so:

private void button1_Click(object sender, RoutedEventArgs e)
{
    var thread = new Thread(new ThreadStart(ScannerThreadFunction));
    thread.Start();
}

public void ScannerThreadFunction()
{
    try
    {
        Scan();
    }
    catch (Exception ex)
    {
        //Writing to the console won't be so useful on a desktop app
        //Console.WriteLine(ex.Message);
    }
    finally
    {
    }
}

private void Scan()
{
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                                          new Action(() => MyTextbox.IsEnabled = false));

    //do the scan

    Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                              new Action(() => MyTextbox.IsEnabled = true));
}

In WinForms, you can also do this during the scan method, but it is done a little differently. You need to check if the InvokeRequired boolean on the form itself is true, and if so use a MethodInvoker, something like this:

private void button1_Click(object sender, EventArgs e)
{
    var thread = new Thread(new ThreadStart(ScannerThreadFunction));
    thread.Start();
}

public void ScannerThreadFunction()
{
    try
    {
        Scan();
    }
    catch (Exception ex)
    {
        //Writing to the console won't be so useful on a desktop app
        //Console.WriteLine(ex.Message);
    }
    finally
    {
    }
}

private void Scan()
{
    ChangeTextBoxIsEnabled(false);

    //do scan

    ChangeTextBoxIsEnabled(true);
}

private void ChangeTextBoxIsEnabled(bool isEnabled)
{
    if (InvokeRequired)
    {
        Invoke((MethodInvoker)(() => MyTextbox.Enabled = isEnabled));
    }
    else
    {
        MyTextbox.Enabled = isEnabled;
    }
}

Before you start the background thread - disable your textbox. In the background thread, notify the main thread when processing is done (if you're using WPF, use the main thread's Dispatcher ), and in the main thread enable the textbox again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM