简体   繁体   中英

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.)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM