Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, Can I check on a lock without trying to acquire it?

I have a lock in my c# web app that prevents users from running the update script once it has started.

I was thinking I would put a notification in my master page to let the user know that the data isn't all there yet.

Currently I do my locking like so.

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

And I do not want to do

if(Monitor.TryEnter(myLock))
    Monitor.Exit(myLock);
else
    //show processing labal

As I imagine there is a slight possibility that it might display the notification when it isn't actually running.

Is there an alternative I can use?

Edit:
Hi Everyone, thanks a lot for your suggestions! Unfortunately I couldn't quite get them to work... However I combined the ideas on 2 answers and came up with my own solution. It seems to be working so far but I have to wait for the process to complete...

Ok this seems to be working, I broke out the Repopule Method into it's own class.

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

In master I do

protected void Page_Load(object sender, EventArgs e)
{
    if (DataPopulation.IsUpdating)
        lblRefresh.Visible = true;
    else
        lblRefresh.Visible = false;
}
like image 628
Biff MaGriff Avatar asked Feb 11 '11 22:02

Biff MaGriff


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


1 Answers

(Given that you are aware of the race condition for displaying this notification just after processing stopped.... )

You could switch to a CountdownEvent. This works similarly to a ManualResetEvent, but also provides CurrentCount and IsSet properies, which could be used to determine if something is being processed.

like image 125
Reed Copsey Avatar answered Sep 20 '22 15:09

Reed Copsey