C# 每秒检查一次数据库值,同时允许用户执行其他操作

C# 每秒检查一次数据库值,同时允许用户执行其他操作,c#,asp.net,multithreading,web-applications,C#,Asp.net,Multithreading,Web Applications,我需要一个每秒检查数据库中的值是否为true的进程,windows服务将该值设置为true 当该值为true时,更新图像。 但我需要的是,当值为false时,用户可以自由地在页面中执行其他活动 我一直在寻找多线程通信,但我真的没有找到适合我的特定需求的东西 谢谢你的帮助 在这里,我添加了我拥有的代码: private static class QuickUpdateCompletedCheck { #region BEGIN Declares private static Pr

我需要一个每秒检查数据库中的值是否为true的进程,windows服务将该值设置为true

当该值为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
        }
    }
}

您可以使用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);
        }
    });
}
[WebMethod]
public static string GetDatabaseValue()
{
    // Put database retrieval logic here
}
YourPage.aspx
可以托管一个ASP.NET AJAX页面方法来执行简单的页面托管服务,该服务自动编码为JSON数据,如下所示:

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);
        }
    });
}
[WebMethod]
public static string GetDatabaseValue()
{
    // Put database retrieval logic here
}

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

在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));
}

为什么多线程不适合你?你真的认为你是第一个在互联网上遇到这种问题的人吗?我把这个问题解释为:我懒得读所有这些东西,给我发一个有效的解决方案好吧,多线程我的东西是answear,但我试过几个例子。无论如何,谢谢你的评论。谢谢你的回答。这是我第一次使用线程。我已经在问题中添加了我的代码,这样你就可以看到我是否妨碍了你,或者我做了什么不好。再次感谢