简体   繁体   中英

How to use Threading.Timer for a loop on a public non-static method on ASP.NET C#?

I have the following method:

//BOLO stands for Be On The Lookout

public void LoadBolos()
{
    List<string> bolos;

    //If not loaded before
    if (ViewState["bolos"] == null)
    {
        List<string> bolos = GetBolos();

        ViewState["bolos"] = bolos;
        ViewState["index"] = 0;
    }

    else
    {
        bolos = (List<string>)ViewState["bolos"];
    }

    int index = ViewState["index"] != null ? (int)ViewState["index"] : 0;

    if (bolos.Count > 0)
    {
        imgBolo.ImageUrl = bolos[index];
        index++;
        index = index >= bolos.Count ? 0 : index;
    }

    ViewState["index"] = index;
}

I need to loop this method every 10 seconds, so I can refresh an image on the aspx page. However, I've noticed that some Thread.Timer functions don't allow non-static functions to be used.

If I convert this method to static, I can no longer access private variables that load on page load, nor can I use the Viewstate nor access the image.

So I was wondering which would be the most accurate way to refresh an image on the page every 10 seconds.

您可以使用无标题页面使用html内容刷新页面

<meta http-equiv="refresh" content="20"  />

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