Idea to implement Cslalight BusinessBase.WaitBusy

Idea to implement Cslalight BusinessBase.WaitBusy

Old forum URL: forums.lhotka.net/forums/t/7149.aspx


Cuong posted on Monday, June 22, 2009

I am developing a Silverlight app. The SaveButton's Click event handler which will save business object is like bellow:

var obj = this.DataObject;
if (obj.IsBusy == false)
{
    if (obj.IsDirty)
    {
        if (obj.IsValid)
        {
            obj.Save();
        }
        else
        {
            MessageBox.Prompt(obj.BrokenRulesCollection.First().ToString());
        }
    }
    else
    {
        OnCancel();
    }
}

So, if a TextBox control is focused, the user have to press the Enter key (that will invoke the SaveButton' Click event handler) twice to save business object because of the checking code "if (obj.IsBusy == false) { ... }". The first invoking is ignored because the business object is still busy.

I want to implement an BusinessBase.WaitBusy to lock the application thread until the business object is not busy. So the code will be changed like bellow and the user can press Enter key once to save business object. Now I cannot find any good solution to implement this method.

Does anyone meet this senario? Do you have an idea to implement this method or have a trick to workaround my issue? I just tried to use Thread.Sleep inside BusinessBase.WaitBusy with no success.

var obj = this.DataObject;
if (obj.WaitBusy(1000)) // wait 1 second
{
    if (obj.IsDirty)
    {
        if (obj.IsValid)
        {
            obj.Save();
        }
        else
        {
            MessageBox.Prompt(obj.BrokenRulesCollection.First().ToString());
        }
    }
    else
    {
        OnCancel();
    }
}




 

RockfordLhotka replied on Tuesday, June 23, 2009

Anything that blocks the main Silverlight thread (which is the browser's main thread too) is bad.

The reason is that the browser is then locked up, so the user can't switch to another tab, close the browser or otherwise interact with the browser. There is no way in which this is a good thing.

So any time you think you want to block this thread, you need to reimagine a solution to the problem.

Copyright (c) Marimer LLC