Bif*_*iff 9 c# multithreading locking
我在我的c#web应用程序中有一个锁,它阻止用户在启动后运行更新脚本.
我以为我会在我的母版页中发出通知,让用户知道数据还不是全部.
目前我这样锁定.
protected void butRefreshData_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ParameterizedThreadStart(UpdateDatabase));
t.Start(this);
//sleep for a bit to ensure that javascript has a chance to get rendered
Thread.Sleep(100);
}
public static void UpdateDatabase(object con)
{
if (Monitor.TryEnter(myLock))
{
Updater.RepopulateDatabase();
Monitor.Exit(myLock);
}
else
{
Common.RegisterStartupScript(con, AlreadyLockedJavaScript);
}
}
Run Code Online (Sandbox Code Playgroud)
我不想这样做
if(Monitor.TryEnter(myLock))
Monitor.Exit(myLock);
else
//show processing labal
Run Code Online (Sandbox Code Playgroud)
正如我想象的那样,它可能会在实际运行时显示通知.
有没有我可以使用的替代方案?
编辑:
大家好,非常感谢您的建议!不幸的是我无法让他们工作......但是我将这两个想法结合起来并提出了我自己的解决方案.它似乎工作到目前为止,但我必须等待该过程完成...
好吧,这似乎有效,我将Repopule方法分解为它自己的类.
public static class DataPopulation
{
public static bool IsUpdating = false;
private static string myLock = "My Lock";
private static string LockMessage = @"Sorry, the data repopulation process is already running and cannot be stopped. Please try again later. If the graphs are not slowly filling with data please contact your IT support specialist.";
private static string LockJavaScript = @"alert('" + LockMessage + @"');";
public static void Repopulate(object con)
{
if (Monitor.TryEnter(myLock))
{
IsUpdating = true;
MyProjectRepopulate.MyProjectRepopulate.RepopulateDatabase();
IsUpdating = false;
Monitor.Exit(myLock);
}
else
{
Common.RegisterStartupScript(con, LockJavaScript);
}
}
}
Run Code Online (Sandbox Code Playgroud)
掌握我做的
protected void Page_Load(object sender, EventArgs e)
{
if (DataPopulation.IsUpdating)
lblRefresh.Visible = true;
else
lblRefresh.Visible = false;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3504 次 |
最近记录: |