Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: If hosting sfml on a winform, do I need to account for an sfml crash?  (Read 4058 times)

0 Members and 2 Guests are viewing this topic.

cybersnap

  • Guest
I'm successfully hosting sfml in a windows form using visual basic.

On my application Form_Closing event I add

window.Close()

window is a sfml RenderWindow instance.

The program works fine, shuts down correctly, but I realized something. It's mentioned here that even though sfml can be hosted in a control, it still acts as it's own window. This means that while my application is running, if sfml crashes, my application won't shut down. ...and there is a reason for that ->

This is my loop. As you can see, I'm using window.IsOpen instead of something from my main form. The reason is, because sfml has a better and more consistent loop structure. It's much better and more optimized and not prone to .NET conflicts. So if sfml crashes, then this loop exits, but my form won't close.

What should I do, to make sure I handle any sfml crashes while using an sfml loop. I'm listing my complete program at the end of this post.


 While window.IsOpen
            Application.DoEvents()

            window.DispatchEvents()
            window.Clear()

            mySprite.Position = New Vector2f(250, 250)
            window.Draw(mySprite)

            If bSpin Then
                mySprite.Rotation = r

                If spinleft Then
                    r += TrackBar1.Value * -0.5F
                Else
                    r += TrackBar1.Value * Math.Abs(-0.5F)
                End If

            End If

            window.Display()
        End While
 


Complete program:

Imports SFML
Imports SFML.Window
Imports SFML.System
Imports SFML.Graphics
Imports SFML.Audio


Public Class Form1
    Dim rs As New Control

    Dim WithEvents window As RenderWindow

    Dim pth As String = "e:\data\pics\"

    Private bSpin As Boolean = False

    Dim r As New Single

    Dim spinleft As Boolean = False

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        window.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        rs.Size = New Size(500, 500)
        rs.BackColor = Drawing.Color.CornflowerBlue
        Me.Controls.Add(rs)
        rs.Location = New Point(0, 0)


        Dim contextSettings As New ContextSettings
        contextSettings.DepthBits = 32

        window = New RenderWindow(rs.Handle, contextSettings)
        window.SetVerticalSyncEnabled(True)

        Button2.Enabled = False
        Button3.Enabled = False

        Me.Show()
        Me.Focus()

        Dim myTexture As New Texture(pth & "spr1.gif")
        Dim mySprite As New Sprite
        mySprite.Texture = myTexture

        mySprite.Origin = New Vector2f(myTexture.Size.X / 2.0F, myTexture.Size.Y / 2.0F)

        r = 0.0F

        While window.IsOpen
            Application.DoEvents()

            window.DispatchEvents()
            window.Clear()

            mySprite.Position = New Vector2f(250, 250)
            window.Draw(mySprite)

            If bSpin Then
                mySprite.Rotation = r

                If spinleft Then
                    r += TrackBar1.Value * -0.5F
                Else
                    r += TrackBar1.Value * Math.Abs(-0.5F)
                End If

            End If

            window.Display()
        End While
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If bSpin = False Then
            bSpin = True
            Button1.Text = "Stop spin"
            Button2.Enabled = True
            Button3.Enabled = True
        Else
            bSpin = False
            Button1.Text = "Start spin"
            Button2.Enabled = False
            Button3.Enabled = False
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        spinleft = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        spinleft = False
    End Sub
End Class
 

cybersnap

  • Guest
Re: If hosting sfml on a winform, do I need to account for an sfml crash?
« Reply #1 on: April 04, 2015, 07:40:51 pm »
Is it good enough if I add this?


    Sub mySFML Window_Closed(ByVal sender As Object, ByVal e As EventArgs) Handles window.Closed
        Me.Close()
    End Sub
 

cybersnap

  • Guest
Re: If hosting sfml on a winform, do I need to account for an sfml crash?
« Reply #2 on: April 04, 2015, 07:50:22 pm »
I'm still thinking about this and I'm not an expert with events.

Would it be safer to do this and not worry about .NET/COM relations?

While Me.Visible

 ...

End While

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: If hosting sfml on a winform, do I need to account for an sfml crash?
« Reply #3 on: April 04, 2015, 08:16:22 pm »
This forum has an edit function for a reason. Use it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: If hosting sfml on a winform, do I need to account for an sfml crash?
« Reply #4 on: April 05, 2015, 02:59:47 pm »
Quote
though sfml can be hosted in a control, it still acts as it's own window

You do realize with WinForms every single control is actually its own window?

Quote
if sfml crashes, my application won't shut down
Quote
So if sfml crashes, then this loop exits, but my form won't close.

Ehhh... I really don't get what you mean by "crashes". Because if SFML throws an exception (like any other piece of code can) either you handle that exception and your program keeps on running... or you don't handle that exception and your program will die. SFML's render window is nothing special and it won't cause this case you thought of.

Quote
So if sfml crashes, then this loop exits, but my form won't close.

This doesn't make any sense and won't ever happen.

Would it be safer to do this and not worry about .NET/COM relations?

While Me.Visible

 ...

End While

This doesn't make any sense either... SFML doesn't even use COM.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

cybersnap

  • Guest
Re: If hosting sfml on a winform, do I need to account for an sfml crash?
« Reply #5 on: April 05, 2015, 08:55:30 pm »
Sorry about the no-edits, I'll be more careful.

Ok, I read the last post. You have to understand, that dot net is a little confusing. I simplified the code to get a better understanding and to better my question. Here is my complete application.

Please note that in the _closing procedure, I had to use window.close. If I do not, then after closing the form, the application remains in memory, as I checked the windows task manager. So it's important to explicitly shutdown the sfml window control.

My problem in understanding this is in relation to a standard windows forms application. From what I understood, in a net application, if the last form closes, then everything shuts down with it. Since the program remained in memory, i was not sure if I had to have a specific way to handle all this, including any special exception handling.

So now that I have this. Is it necessary to make any other considerations for initialization, shutdown, or the possibility of exceptions?

Imports SFML
Imports SFML.Window
Imports SFML.Graphics

Public Class Form1
    Private rh As New Control ' rs = renderhost

    Private WithEvents window As RenderWindow

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        window.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        rh.Size = New Size(640, 360)
        Me.Controls.Add(rh)
        rh.Location = New Point(0, 0)

        window = New RenderWindow(rh.Handle)
        window.SetVerticalSyncEnabled(True)

        Me.Show()

        While window.IsOpen
            Application.DoEvents()

            window.DispatchEvents()
            window.Clear()

            window.Display()
        End While
    End Sub
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: If hosting sfml on a winform, do I need to account for an sfml crash?
« Reply #6 on: April 06, 2015, 02:16:03 pm »
Your problem stems from the fact that you are running the render loop from inside the Form_Load() method and are only looping while the render window is open. You should instead do your render loop from outside the load method and instead loop while your hosting window is still open. I posted a full example of how to do that here.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything