0

I have scripts that run on request and are compiled using CodeDomProvider. Here's an example of one:

var yes = SendYesNo();

if (yes)
    // Do something
else
    // Do something else

Now. SendYesNo displays a box with input from the user. I want to halt the script on that line, until a response is set. And then, take the response and apply it to the variable and continue the execution. So far I've used await/async, but I dislike this idea. Is it possible with something else?

Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42
user3265040
  • 305
  • 1
  • 4
  • 11
  • 1
    Is this WinForms, WPF, WebBased, Console App? – Erik Philips Mar 13 '14 at 21:25
  • @ErikPhilips WinForms. – user3265040 Mar 13 '14 at 21:25
  • What does SendYesNo do? Can't you just use ShowDialog? – aquinas Mar 13 '14 at 21:36
  • This doesn't work as is? When you call `SendYesNo()` the program cannot proceed until `SendYesNo()` finishes... – Brad Mar 13 '14 at 21:38
  • @Brad That wasn't my question. I want to know how do I make SendYesNo return a boolean, BUT only when I set that boolean. Like, make SendYesNo return a boolean that I set whenever I want. Only when it's set, it returns it. – user3265040 Mar 13 '14 at 22:10
  • Maybe I'm still confused. you have `bool someVal;` then `return someVal;` at the end of your `SendYesNo` method. Return cannot come back to the code that called `SendYesNo` unless there is something else at play here which you are not including. – Brad Mar 13 '14 at 22:31

1 Answers1

1

You could use a modal window that returns your parameter. You could either use a standard MessageBox or a customized Form if the input required is more complex.

Something like:

public class SomeForm : Form
{
    public bool yesNo
    {
        get
        {
            return yesNo;
        }
        set
        {
        //set value according to your logic
        }
    }
}

and in your main Form call it like:

using (var form = new SomeForm())
{
    if (form.ShowDialog() == DialogResult.OK)
    {
        var yesNo = form.yesNo;
        if (yes)
        // Do something
        else
        // Do something else
    }
}

in case it's just a Yes / No MessageBox you can refer to: How do I create a message box with "Yes", "No" choices and a DialogResult?

Community
  • 1
  • 1
Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42
  • You misunderstood my question. The input from the user is not even a message box, the input doesn't even matter. I just want to know how to halt the xeceution until a response is given. It doesn't really matter if it's WinForms or not, because I'm not using WinForms for this, anyhow. – user3265040 Mar 13 '14 at 21:43
  • If the input doesn't matter then why wait for it? – Brad Mar 13 '14 at 21:44
  • There are basically two scenarios: 1) You need to wait for the input and you don't want your computation to continue. Your method is synchronous, and it won't go past it till it returns. 2) You don't want to wait for the input but you want to process the result once you get it. In this case you might want to use a Thread and a callback / event once the input is available. Which one is your case? – Saverio Terracciano Mar 13 '14 at 21:46
  • "@ErikPhilips WinForms. – user3265040 19 mins ago" "I'm not using WinForms for this, anyhow. – user3265040 2 mins ago" Uhh ok. I give up. What are you asking??? – aquinas Mar 13 '14 at 21:46
  • @SaverioTerracciano The first one. I must wait for the result before continuing. About the WinForms -- I do use WinForms, but I don't think the question has to do with WinForms at all, it's asked in general. – user3265040 Mar 13 '14 at 22:00
  • 1
    In this case, since your code is synchronous, your code already works as expected. You won't reach the IF statement before the SendYesNo() method returns. – Saverio Terracciano Mar 13 '14 at 22:03
  • @SaverioTerracciano I know. But - that wasn't my question. My question is HOW do I make the SendYesNo WAIT for a response? Like, make it return a boolean, but only when a button sets that boolean when ever? – user3265040 Mar 13 '14 at 22:04
  • Refer to the code I wrote in my answer. You're creating a new class which inherits from Form. In that new class, you create a couple buttons (Yes / No) with associated actions / events that set the yesNo property of the form. If it's just that the interaction the user has to do, then you can use the basic MessageBox component, in which case, refer to the link I provided at the bottom of my answer. – Saverio Terracciano Mar 13 '14 at 22:10
  • It doesn't even have to inherit from `Form`. It can be anything at all. Control cannot return until a `return` statement is hit or an exception is thrown (if this is done synchronously which it sounds like it is) – Brad Mar 13 '14 at 22:34
  • I was suggesting Form since he wanted buttons to interact with. – Saverio Terracciano Mar 13 '14 at 22:36
  • @SaverioTerracciano I agree. I think it is good. I was jsut trying to emphasize that practically no matter what it is execution does not continue. – Brad Mar 13 '14 at 23:07