• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

How to keep checking continuously at a particular time interval if a file is unlocked by another user?

Hi,

I have the attached code to lock the files before processing so they will be accessed by only one user at a time. It is working fine. But I would like to remove the DialogResult message box in the IOException catch blocks and do something to check at a particular time interval to see if the file can be accessed. If the file can be accessed, it should show a message saying "File is available. continue processing" so the user will not need to click "OK" to try again every time.
Can anybody please provide some suggestions?
Thanks for any ideas.
do
        {
            filelocked = false;
            try
            {
                JFileInfo = new FileInfo(JFile);

                using (FileStream JStream = JFileInfo.Open(FileMode.OpenOrCreA, FileAccess.ReadWrite, FileShare.None))
                {
                    try
                    {
                        AFileInfo = new FileInfo(AFile);
                        
                        using (FileStream AStream = AFileInfo.Open(FileMode.OpenOrCreA, FileAccess.ReadWrite, FileShare.None))
                        {
                            using (StreamReader reader = new StreamReader(JStream))
                            {
                                using (StreamWriter writer = new StreamWriter(JStream))
                                {
                                    if (AFileInfo.Exists && AFileInfo.Length == 0)
                                    {
                                        JStream.Seek(0, SeekOrigin.Begin);
                                        writer.WriteLine(Jdefault);
                                        writer.Flush();
                                    }

                                    string line = reader.ReadLine();
                                    int.TryParse(line, out PrevJNumber);

                                    try
                                    {
                                        fileContents = GenerATransactionsFile();
                                    }
                                    catch (Exception x)
                                    {
                                        throw new GenericException(string.Format("Error while processing the file. {0}", x.Message));
                                    }

                                    JStream.Seek(0, SeekOrigin.Begin);
                                    writer.WriteLine(PrevJNumber);
                                    writer.Flush();
                                }
                            }
                        }

                        if (!string.IsNullOrEmpty(fileContents))
                        {
                            try
                            {
                                System.IO.File.AppendAllText(AFileInfo.FullName, fileContents);
                            }
                            catch (Exception x)
                            {
                                throw new GenericException(string.Format("Error when appending transactions to the A file. {0}", x.Message));
                            }
                        }
                    }
                    catch (IOException ioex)
                    {
                        string message = string.Format("{0} \r\n{1}", ioex.Message, "The A file cannot be accessed since it is locked by another user. Please click OK to try again");
                        if (DialogResult.OK == MessageBox.Show(message, "File Locked", MessageBoxButtons.OKCancel))
                        {
                            filelocked = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new GenericException(string.Format("Error when processing the A file. {0}", ex.Message));
                    }
                }
            }
            catch (IOException ioex)
            {
                string message = string.Format("{0} \r\n{1}", ioex.Message, "The J file cannot be accessed since it is locked by another user. Please click OK to try again");
                if (DialogResult.OK == MessageBox.Show(message, "File Locked", MessageBoxButtons.OKCancel))
                {
                    filelocked = true;
                }
            }
            catch (Exception ex)
            {
                throw new GenericException(string.Format("Error when processing the J file. {0}", ex.Message));
            }

        } while (filelocked);

Open in new window

0
ipjyo
Asked:
ipjyo
2 Solutions
 
NavneetCommented:
Hi!

Check if this helps

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23680030.html

Regarding the keeping check for lock you may need to run background thread.
But question is till that time what will you show up to the user?

Thanks!
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
if (DialogResult.OK == MessageBox.Show(message, "File Locked", MessageBoxButtons.OKCancel))

Replace the system provided MessageBox with a form of your own design.  When that form is launched you could start a timer that will close the form automatically after a period of your choosing.
0
 
ipjyoAuthor Commented:
Thank you guys for the suggestions. I would like to try to add a custom form. I think I can show a label on the form and update it as soon as the files become available. In this case, I think I don't need a timer.

Thanks.

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now