user1577285
user1577285

Reputation: 3

Multiple windows application's bug in Mono

I'm developing a C# application with LinuxMint and MonoDevelop.
I wrote the next code,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace TestProgram
{
    public static class Program
    {
        public static int count = 0;
        static object locker = new object();

        const int limit = 10;

        [STAThread]
        static void Main()
        {
            for(int i = 0; i < Program.limit; ++i)
            {   
                Action item = () => Program.RunForm();
                item.BeginInvoke((a) => item.EndInvoke(a), null);
                Thread.Sleep(1000);
            }

            while(true)
            {
                Thread.Sleep(1000);
                if(Program.count == 0)
                    break;
            }

            return;
        }

        static void RunForm()
        {   
            lock(Program.locker) {
                Program.count += 1;
            }

            Application.EnableVisualStyles();
            Application.Run(new Form());

            lock(Program.locker) {
                Program.count -= 1;
            }
        }
    }
}

In DotNET, the program works fine. 10 windows appears correctly.
In Mono, the program crashes with no managed exception.
When 2nd, 3rd, or later window appears, it suddenly crashes.

Is this a mono's bug? Or the code is wrong?
Why are the behaviors different?

(Please excuse my terrible english.)

Upvotes: 0

Views: 330

Answers (1)

skolima
skolima

Reputation: 32684

That code should (and will, for some combinations of OS and .NET) break on Windows on Microsoft .NET as well. You should only create your forms on a single thread, and definitely shouldn't call Application.Run() multiple times!

Upvotes: 5

Related Questions