简体   繁体   中英

Delay windows phone 8 progress-bar appearance

I want to delay appearance of progressbar in Windows Phone 8 application for 2 sec.
So when I call webservice if I don't receive response after 2 sec progress-bar should appear.

I have implemented code with DispatcherTimer but it does not seams to work as expected.
This variable is binded to IsEnabled and IsVisible of ProgressBar control.
Problem is that this code works randomly and not after 2 sec.When I increase timer for 20 sec progress-bar is still appearing even every response is bellow 1 sec.

 private bool _isProgressBarLoading;
    public bool IsProgressBarLoading
    {
        get
        {
            return _isProgressBarLoading;
        }
        set
        {
            if (_isProgressBarLoading != value)
            {
                if (value)
                {
                    var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(2000) };
                    timer.Tick += delegate
                    {
                        timer.Stop();
                        _isProgressBarLoading = true;
                    };
                    timer.Start();
                }
                else
                {
                    _isProgressBarLoading = false;
                }
                NotifyOfPropertyChange(() => IsProgressBarLoading);
            }
        }
    }

How about using different Timer operating on separate thread:

System.Threading.Timer myTimer = null;
private bool _isProgressBarLoading = false;
public bool IsProgressBarLoading
{
    get { return _isProgressBarLoading; }
    set
    {
       if (_isProgressBarLoading != value)
       {
           if (value)
           {
                if (myTimer == null)
                {
                   myTimer = new System.Threading.Timer(Callback, null, 3000, Timeout.Infinite);
                }
                else myTimer.Change(3000, Timeout.Infinite);
                // it should also work if you create new timer every time, but I think it's
                // more suitable to use one
           }
           else
           {
                _isProgressBarLoading = false;
                NotifyOfPropertyChange(() => IsProgressBarLoading);
           }
       }
    }
}

private void Callback(object state)
{
   Deployment.Current.Dispatcher.BeginInvoke(() =>
   {
       _isProgressBarLoading = true;
        NotifyOfPropertyChange(() => IsProgressBarLoading);
    });
}

DispatcherTimer is working on the Main thread, I think it will be better to use other Thread.


And as for your code it should work if it looks like this - notify when you change the value:

if (value)
{
    var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(2000) };
    timer.Tick += delegate
    {
        timer.Stop();
        _isProgressBarLoading = true;
        NotifyOfPropertyChange(() => IsProgressBarLoading);
    };
    timer.Start();
}
else
{
    _isProgressBarLoading = false;
    NotifyOfPropertyChange(() => IsProgressBarLoading);
}

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