Link to home
Start Free TrialLog in
Avatar of Alex E.
Alex E.

asked on

ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ...

Someone helped us here with this cod to ctach an exception and pop up a message to the user with this code:

        private void button1_Click(object sender, EventArgs e)
        {
            lineChanger("QWERTY", "MyTextFile.txt", 2);
        }

        static void lineChanger(string newText, string fileName, int line_to_edit)
        {
            int attemptCounter = 0;
            for (attemptCounter = 1; attemptCounter <= 10; attemptCounter++)
            {
                try
                {
                    System.Diagnostics.Debug.WriteLine("Attempt " + (attemptCounter));
                    string[] arrLine = File.ReadAllLines(fileName);
                    arrLine[line_to_edit - 1] = newText;
                    File.WriteAllLines(fileName, arrLine);

                    // No error - break
                    break;
                }
                catch (IOException ioex)
                {
                    System.Diagnostics.Debug.WriteLine("IOException happened: " + ioex.ToString());
                    if (attemptCounter >= 10)
                    {
                        MessageBox.Show("File is in use");
                    }
                }
            }
        }

Open in new window


However when the message MessageBox.Show("File is in use"); is launched the applciation gives this error:

"Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application"

How can be fixed the problem or what could be the problem?

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex E.
Alex E.

ASKER

Thank you your idea was excellent. In fact was impossible use msgbox however we used a session variable and we passed that variable session in a text label and that's it.

Thank you