public async Task WinForms_Parent_Child_Switch()
        {
            await UnitTestHelper.InitializeWithGrahicsAsync();

            Panel hostPanel1 = null;
            Panel hostPanel2 = null;
            SeeingSharpRendererControl renderControl = null;
            int       stepID = 0;
            Exception fakeUIThreadException = null;

            ObjectThread fakeUIThread = new ObjectThread("Fake-UI", 100);

            fakeUIThread.ThreadException += (sender, eArgs) =>
            {
                fakeUIThreadException = eArgs.Exception;
            };
            fakeUIThread.Starting += (sender, eArgs) =>
            {
                hostPanel1      = new System.Windows.Forms.Panel();
                hostPanel1.Size = new Size(500, 500);
                hostPanel2      = new System.Windows.Forms.Panel();
                hostPanel2.Size = new Size(500, 500);

                renderControl      = new SeeingSharpRendererControl();
                renderControl.Dock = System.Windows.Forms.DockStyle.Fill;

                hostPanel1.CreateControl();
                hostPanel2.CreateControl();
                hostPanel1.Controls.Add(renderControl);
            };
            fakeUIThread.Tick += (sender, eArgs) =>
            {
                Application.DoEvents();
                stepID++;

                switch (stepID)
                {
                case 2:
                    hostPanel1.Controls.Remove(renderControl);
                    break;

                case 4:
                    hostPanel2.Controls.Add(renderControl);
                    break;

                case 8:
                    hostPanel2.Controls.Remove(renderControl);
                    break;

                case 10:
                    renderControl.Dispose();
                    hostPanel2.Dispose();
                    hostPanel1.Dispose();
                    break;

                case 11:
                    fakeUIThread.Stop();
                    break;
                }
            };
            fakeUIThread.Start();

            // Wait until the Fake-UI thread stopped
            await fakeUIThread.WaitUntilSoppedAsync();

            // Some checks after rendering
            Assert.True(GraphicsCore.Current.MainLoop.IsRunning);
            Assert.True(GraphicsCore.Current.MainLoop.RegisteredRenderLoopCount == 0);
            Assert.Null(fakeUIThreadException);
            Assert.True(renderControl.IsDisposed);
        }
Example #2
0
        public async Task WinForms_Parent_Child_Switch()
        {
            await TestUtilities.InitializeWithGraphicsAsync();

            // Ensure that any async disposal is  done before we create a new GraphicsCore
            await GraphicsCore.Current.MainLoop !.WaitForNextPassedLoopAsync();
            await GraphicsCore.Current.MainLoop !.WaitForNextPassedLoopAsync();

            Panel?hostPanel1 = null;
            Panel?hostPanel2 = null;
            SeeingSharpRendererControl?renderControl = null;
            var       stepId = 0;
            Exception?fakeUIThreadException = null;

            var fakeUIThread = new ObjectThread("Fake-UI", 100);

            fakeUIThread.ThreadException += (_, eArgs) =>
            {
                fakeUIThreadException = eArgs.Exception;
            };
            fakeUIThread.Starting += (_, _) =>
            {
                hostPanel1 = new Panel
                {
                    Size = new GDI.Size(500, 500)
                };

                hostPanel2 = new Panel
                {
                    Size = new GDI.Size(500, 500)
                };

                renderControl = new SeeingSharpRendererControl
                {
                    Dock = DockStyle.Fill
                };

                hostPanel1.CreateControl();
                hostPanel2.CreateControl();
                hostPanel1.Controls.Add(renderControl);
            };
            fakeUIThread.Tick += (_, _) =>
            {
                Application.DoEvents();
                stepId++;

                switch (stepId)
                {
                case 2:
                    hostPanel1 !.Controls.Remove(renderControl !);
                    break;

                case 4:
                    hostPanel2 !.Controls.Add(renderControl !);
                    break;

                case 8:
                    hostPanel2 !.Controls.Remove(renderControl !);
                    break;

                case 10:
                    renderControl !.Dispose();
                    hostPanel2 !.Dispose();
                    hostPanel1 !.Dispose();
                    break;

                case 20:
                    fakeUIThread.Stop();
                    break;
                }
            };
            fakeUIThread.Start();

            // Wait until the Fake-UI thread stopped
            await fakeUIThread.WaitUntilSoppedAsync();

            // Some checks after rendering
            Assert.IsTrue(GraphicsCore.Current.MainLoop.IsRunning);
            Assert.IsTrue(GraphicsCore.Current.MainLoop.RegisteredRenderLoopCount == 0);
            Assert.IsNull(fakeUIThreadException);
            Assert.IsTrue(renderControl !.IsDisposed);
        }