Hi there,

I'm working on a project which incorporates the following:

  • Reading data from XML
  • Writing data to XML
  • Interpreting XML and passing the information to various places

These are on different threads, and I've used a Mutex to make sure they don't try opening the main XML file at the same time.

My problem(s) are as follows:

  • Despite calling the Dispose() method on exit, I still receive 'Error: Abandoned Mutex' on one of the Mutexes
  • The data doesn't seem to be being read properly. I doubt it even enters the loop, as I put a debugging text box in the middle of the reader, and it never even comes up. And it's complaining of the abandoned mutex in that place when it should have already finished.

Here are snippets of my code:

// ########################## <INITIATE> #############################
public static Mutex xmlMutex = new Mutex(true); // Create a mutex for XML file editing
        private void mainStartBtn_Click(object sender, EventArgs e)
        {
            if (mainStartBtn.Text == "Begin")
            {
                mainStartBtn.Text = "Pause";

                xmlJob.makeXML(this.queue, "jobset");
                progressBarHandler.RunWorkerAsync();
                downloadThread.RunWorkerAsync();
            }   
        }
// ########################## <XML JOB WRITER> #############################
public static void makeXML(object list, string type)
    {
        ListBox jobs = list as ListBox;
        Mutex xmlMutex = AutoInstall.AutoInstallMenu.xmlMutex; // Make the mutex local so I don't have to type as much.

        xmlMutex.WaitOne();
        //############## XML Creation Codes #############
        xmlMutex.ReleaseMutex();
// ########################## </XML JOB WRITER> #############################
// ########################## <XML JOB READER> #############################
    // Create struct to hold all list attributes
    public struct downloadInfo
    {
        public string[] name;
        public string[] catagory;
        public string[] installer;
    }
    // Lists downloads
    public static downloadInfo downloads()
    {
        // Initialize object to store download list
        downloadInfo downloadList = new downloadInfo();

        // Make the mutex local so I don't have to type as much.
        Mutex xmlMutex = AutoInstall.AutoInstallMenu.xmlMutex;

        // Enter the mutex
        xmlMutex.WaitOne();

        //############## XML Reading Codes #############

        // For each non-downloaded item, add it to the list
        foreach (XmlNode item in xnList)
        {
            // Store app name
            temporary = item.SelectSingleNode("appName");
            downloadList.name[i] = temporary.InnerText;
            MessageBox.Show(temporary.InnerText); // THIS DOESN'T EVEN COME UP!

            // Store catagory
            temporary = item.SelectSingleNode("appCatagory");
            downloadList.catagory[i] = temporary.InnerText;

            // Store URI
            temporary = item.SelectSingleNode("installer");
            downloadList.installer[i] = temporary.InnerText;

            i++;
        }


        // Exit the mutex
        xmlMutex.ReleaseMutex();


        return downloadList;
    }

    // ########################## </XML JOB READER> #############################
// ########################## <CLOSE SAFELY> #############################
        private void AutoInstallMenu_FormClosed(Object sender, FormClosedEventArgs e)
        {
            xmlMutex.WaitOne();
            xmlMutex.Dispose();
        }

The INITIATE part and the CLOSE SAFELY part are on the same thread (I hope)
The XML READER and WRITER can be on two separate threads, depending on who called them. The progress bar is constantly on another thread though.

There is also a progress bar reader which opens the XML mutex every 500 milliseconds. This doesn't work either. I'll give that code if you need to.

What am I doing wrong here?

Hi there,

You have set your Mutex to be initially owned. This will cause it to wait on the first hit of xmlMutex.WaitOne(); As you never release it before you hit one, your code deadlocks.

Where you create your Mutex on line 2, the parameter passed to the constructor should be "false". This should fix it.

Genius, that's done it.

Now I have some more generic errors to debug, but those will be OK. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.