开发者

c# condition checking

i need to check condition constantly. Not in each loop iteration but also during time of function execution.

wth like this is n开发者_JAVA百科ot acceptable:

 while(condition)
 {
      ...
 }

I need simply to abort function even 'in the middle' of progress when condition appears true. Any ideas?


I need simply to abort function even 'in the middle' of progress when condition appears true. Any ideas?

There is no "simple" way to abort a function in the middle of progress[*], and that's a good thing. Aborting a function in the middle could leave your system in an inconsistent state (imagine aborting in the middle of writing a file).

The correct way to do this is to identify "safe points" in your function where interruption is possible and do your check there:

... // some statement

if (condition) return;

... // some more statements

if (condition) return;

...

Footnote: [*] I was lying when I said that there's no simple way to abort a function. There is one, and it's called Thread.Abort (you'd need to run your function in a separate thread for that and monitor condition in another thread, usually the main UI thread). However, using Thread.Abort is highly discouraged for the reasons mentioned above. Don't do it.


I think you need to reconsider what you're wanting to. The most direct answer to your question is that you can check within the loop, and break if the condition is true, you can do this more than once.

But it really seems more likely that what you have/need is a background process, and that an extra pass through the loop isn't that important, you just need a way to stop it when an ALTERNATE condition is true. For that, a background thread is probably the answer.


Assuming this is some kind of error handling assert condition - Take a look at .NET Code Contracts and the ContractInvariantMethod, there's a good example here.

It allows you to define conditions that are not allowed to occur, and causes assert if it ever does at runtime.

The following is taken from the link. It enforces that balance can never be negative, otherwise an exception will occur.

class Account
{
    private int balance

    [ContractInvariantMethod]
    private void BalanceHasToAlwaysBeInTheBlack()
    {
        // Note to reader: only conditions can be in here
        Contract.Invariant(balance >= 0)
    }

    public void Deposit(int amount)
    {
        Contract.Requires(amount > 0)
        Contract.Ensures(balance > amount)   // Note to reader: you can also ensure on the returned object e.g. Contract.Ensures(Contract.Result<int>() > 0)

        balance += amount
    }

    public void Withdraw(int amount)
    {
        Contract.Requires(amount > 0)

        balance -= amount
    }
}


Use the break statement to exit out of a loop.

If you need to exit from a function, use the return statement.

It is not entirely clear from your question what exactly you are trying to achieve, hence the general answer.


You could investigate yield as documented here http://msdn.microsoft.com/en-us/library/9k7k7cf0(v=vs.80).aspx


There are no predefined constructs in C# that autoamatically do what you are thinking of. Not that I'm aware of actually. You have to implement it by yourself.

Check condition in some function with bool return value. And call that function in places where you want to ckeck for it and check if call of that function returns false (for example), after you can use break, return whatever execution breaking instructions you want (based on call context).

Hope this helps.


The break statement can get you out of the loop. As for a mechanism to listen to your condition changing, relying on the while loop would not be completely as efficient as the compiler would need to reach the end of the scope for it to register that the condition is no longer valid.

Maybe you could create your own event handler?


One answer to your question is: start a separate Thread to perform work in, and check the condition in the main thread. When the condition changes, you can Abort() the work thread, immediately shutting it down.

However you would leave the system in an unknown state, as the Abort() shuts down the thread instantly, so generally that's a bad idea.

A better solution, unfortunately, is check the conditions on multiple places from within the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜