-4

我需要一个每秒检查数据库中的值是否为真的过程,这由 Windows 服务设置为真。

当值为 true 时更新图像。但我需要在值为 false 时,用户可以自由地在页面中进行其他活动。

我一直在寻找多线程通信,但我真的没有找到满足我特定需求的东西。

谢谢你的帮助

在这里,我添加了我拥有的代码:

private static class QuickUpdateCompletedCheck
{
    #region BEGIN Declares

    private static ProcessStatus quickUpdateCompletedStatus;
    private static Thread quickUpdateThread;
    private static ISynchronizeInvoke quickUpdateCompletedSynch;
    private static bool updateCompleted;
    private static QuickUpdateInfo quickUpdateInfo = null;

    public delegate void UpdateCompletedStatusCheck(string Message, int status);

    #endregion END Declares
    #region BEGIN Initialization

    public QuickUpdateCompletedCheck(ISynchronizeInvoke syn, ProcessStatus notify, Guid userIdLoc, int activityIdLoc, int fileIdLoc, int spreadIdLoc)
    {
        quickUpdateCompletedSynch = syn;
        quickUpdateCompletedStatus = notify;
        quickUpdateInfo = new QuickUpdateInfo(activityIdLoc, fileIdLoc, spreadIdLoc, userIdLoc);
    }

    #endregion END Initialization
    #region BEGIN Methods

    public void StartProcess()
    {
        quickUpdateThread = new System.Threading.Thread(new ParameterizedThreadStart(UpdateStatus));
        //set the thread to run in the background
        quickUpdateThread.IsBackground = true;
        //name our thread (optional)
        quickUpdateThread.Name = "Add List Items Thread";
        //start our thread
        quickUpdateThread.Start();
    }

    private static void UpdateStatus(object data)
    {
        QuickUpdateInfo quickUpdateInfo = (QuickUpdateInfo)data;

        object[] dataInfo = new object[4];

        dataInfo[0] = quickUpdateInfo.ActivityId;
        dataInfo[1] = quickUpdateInfo.FileId;
        dataInfo[2] = quickUpdateInfo.SpreadId;
        dataInfo[3] = quickUpdateInfo.UserId;

        quickUpdateCompletedSynch.Invoke(QuickUpdateCompletedataInfo); //Here I have an error need a delegate method in first parameter. i suppose is the QuickUpdateComplete method at the end of this description
    }

    #endregion END Methods
}

public class QuickUpdateInfo
{
    private int activityId;
    private int fileId;
    private int spreadId;
    private Guid userId;

    public int ActivityId
    {
        get { return activityId; }
    }

    public int FileId
    {
        get { return fileId; }
    }

    public int SpreadId
    {
        get { return spreadId; }
    }

    public Guid UserId
    {
        get { return userId; }
    }

    public QuickUpdateInfo(int activityId, int fileId, int spreadId, Guid userId)
    {
        this.activityId = activityId;
        this.fileId = fileId;
        this.spreadId = spreadId;
        this.userId = userId;
    }
}

此方法是在页面中最需要更新的图像

public partial class SpreadCorrection : BasePage
{
        protected void UpdatePostBack_OnClick(object sender, EventArgs e)
    {
                //how to start Thread here
    }

    private static void QuickUpdateComplete(int activityId, int fileId, int spreadId)
    {
        if (value from database is true)
        {   
                    UpdateImage();
                    //how to stop Thread here
        }
    }
}
4

2 回答 2

2

您可以使用 JavaScript 的setInterval()函数和 jQuery 的.ajax()函数在服务器端调用服务来检查值,如下所示:

function checkForDatabaseValue() {
    $.ajax({
        type: "POST",
        url: "YourPage.aspx/GetDatabaseValue",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: function (data) {
            // Do something with data returned here
        },
        error: function (errorMessage) {
            // Do something with error message here
        },
        complete: function() {
            // Reset the timer to a minute here
            setTimeout(function() { 
                checkForDatabaseValue(); 
            }, 60000);
        }
    });
}

YourPage.aspx可以托管一个 ASP.NET AJAX 页面方法来执行自动编码为 JSON 数据的简单页面托管服务,如下所示:

[WebMethod]
public static string GetDatabaseValue()
{
    // Put database retrieval logic here
}

注意:您需要引用 ASP.NET AJAX 库才能使用页面方法。上述setInterval.ajax()调用也适用于 ASP.NET XML Web 服务 (.asmx) 和 WCF 服务,但我展示了 ASP.NET AJAX 页面方法方法,因为它很简单。

于 2013-09-23T21:43:19.290 回答
1

在 ASP.Net 中有多种运行后台线程的方法。

这是 ASP.Net 的一个简单示例 - ASP.NET 中的简单后台任务

它将使用缓存在指定的时间间隔(当前为 30 秒)内调用您的代码。

void Application_Start(object sender, EventArgs e)
{
    AddTask("DoStuff", 30); // 30 seconds
}

private static CacheItemRemovedCallback OnCacheRemove;

private void AddTask(string name, int seconds)
{
    OnCacheRemove = CacheItemRemoved;

    HttpRuntime.Cache.Insert(name, seconds, null, 
        DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
        CacheItemPriority.NotRemovable, OnCacheRemove);
}

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
    // Checks if a value from database is true. 
    // If so, call to your method here ...

    AddTask(k, Convert.ToInt32(v));
}
于 2013-09-23T21:44:53.840 回答