How to wait in a custum dlls constructor for a button press of a form ?

c#chris

Member
Joined
Dec 10, 2024
Messages
8
Programming Experience
1-3
Hello,

lets assume we have a plugin dll based on the following class:

My Code:
internal Class CustomDll()
{
      
    private async Task<bool> Form()
    {
        Form1 _form = new();
        _form.textBoxString = "....";
        _form.Show();
        bool task = await _form.WaitAsync();

        if(task) _form.Close();

        return task;
    }
    
    public void CustomDll()
    {
        Form();
        
        //Here dll should wait for user reaction in form ?!
        
        //ToDo:
        //Proceed with dll initialization, if button 1 is pressed in Form, abort dll initialization if button 2 is pressen in form

        //Problem:
        //Form reakts on button press but dll is always initialized
        
    }
    
    
    //Other stuff
    
}

public partial class Form1 : Form
{

    public string textBoxString = string.Empty;
    private bool acceptflag = false;
    private bool declineflag = false;
    private readonly ManualResetEvent mre = new ManualResetEvent(false);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = textBoxString;
    }

    private void accept_Click(object sender, EventArgs e)
    {
        acceptflag = true;
    }

    private void decline_Click(object sender, EventArgs e)
    {
        declineflag = false;
    }

    public async Task<bool> WaitAsync()
    {
        var task = await Task.Run(() => WaitForButtonPressed());
        return task;
    }

    public bool WaitForButtonPressed()
    {
            while (true)
            {
                if (acceptflag) return true;
                if (declineflag) return false;
            }
    }
}


Question 1: How can I make the dll wait for any user interaction in form (without freezing) ?
Question 2: How can I interact bewteen form and dlls, e.g. exchanging data between both code parts and reacting on user inputs in form within dll ?
 
Calling a constructor is no different from calling a method. The only major difference is developers have expectations of constructors to be relatively quick and are just used to initialize a class, and most developers don't expect constructors to fail (depending on which school of programming you come from).

So your first question simply becomes: "How to you keep your method from freezing the UI?" which is the same problem most everybody hits upon when trying to do procedural code with in an event base UI system where the eventing system is dependent on the UI thread not being monopolized by a method.
 
As for exchanging data between a form and other code, the standard strategies apply:
- a form can expose properties that can be read or written to by other code
- a form can expose events so that other code can be notified when particular things happen
- a form can be passed a data object which both the form and the other code both access

For examples of the first two, see some of the Windows common dialogs wrapped by WinForms like the OpenFileDialog, SaveFileDialog, ColorChooser, etc.

For the latter, read about the various MVC, MVP, or MVVM design patterns. The first M stands for Model. It is the Model object that acted upon by the UI (eg. V for View) and other code (C for controller, P for Presenter, VM for View Model).
 
Back
Top Bottom