Aug 13, 2017 12:08 PM
(This post was last modified: Aug 20, 2017 11:41 PM by Barret.)
At some point in time, if it didn't happen already, when you create forms and threads and it happens to be
you close the form without closing the thread, any calls from within that thread that is making changes
to let say, a textbox or some concept of a log view, it will flip you off with an exception.
Example exception
https://msdn.microsoft.com/en-us/library....110).aspx
Controls and forms have properties such as "IsDisposed" and "IsAccessible" but neither of those do fuck all.
What we need is a proper locking mechanism that locks and block calls of such disposed controls.
I saw a few methods, some even very elaborate using the OnClose procedures but yet they do not do want i want.
This might be an solution yet i did not fully test it but seems to work.
Quick setup:
Add the following to your form class
And initialize them on OnFormLoad event
And we add this to the OnFormClosing event
Add this subroutine, this will be our enabler when we are closing so both parties are knowing "whats going on"
And in your thread worker, you make the following construction.
(Assuming you have setup a loop where a boolean determines the status of that loop)
And for the procedures that interact between the thread and form, you add the following.
(This is just an example of a output logger for some debug info coming from another thread)
This will, in my idea, avoid having these exceptions. But i'm not entirely sure if this would be the best, so i'll leave this here and see who is going to add his post.
Ps. Yes i know of 'ManualResetEvent' but thats not what im asking here.
- B.
you close the form without closing the thread, any calls from within that thread that is making changes
to let say, a textbox or some concept of a log view, it will flip you off with an exception.
Example exception
https://msdn.microsoft.com/en-us/library....110).aspx
Controls and forms have properties such as "IsDisposed" and "IsAccessible" but neither of those do fuck all.
What we need is a proper locking mechanism that locks and block calls of such disposed controls.
I saw a few methods, some even very elaborate using the OnClose procedures but yet they do not do want i want.
This might be an solution yet i did not fully test it but seems to work.
Quick setup:
Add the following to your form class
PHP Code
<?php
Private Property ExitLock As Object
Private Property ExitFlag As Boolean
And initialize them on OnFormLoad event
PHP Code
<?php
Me.ExitFlag = False
Me.ExitLock = New Object
And we add this to the OnFormClosing event
PHP Code
<?php
Me.FlagForShutdown()
Add this subroutine, this will be our enabler when we are closing so both parties are knowing "whats going on"
PHP Code
<?php
Private Sub FlagForShutdown()
SyncLock Me.ExitLock
Me.ExitFlag = True
End SyncLock
End Sub
And in your thread worker, you make the following construction.
(Assuming you have setup a loop where a boolean determines the status of that loop)
PHP Code
<?php
Private Sub FlagForShutdown()
Synclock Me.ExitLock
Do
.......thread worker code....
If (Me.ExitFlag) Then
...perform safe and clean exit code....
End If
While Bool
End Synclock
End Sub
And for the procedures that interact between the thread and form, you add the following.
(This is just an example of a output logger for some debug info coming from another thread)
PHP Code
<?php
Private Sub DebugLog(Message As String, Optional Clear As Boolean = False)
If (Not Me.ExitFlag) Then
If (Me.InvokeRequired) Then
Me.Invoke(Sub() Me.DebugLog(Message, Clear))
Else
If (Clear) Then Me.Textbox.Clear()
Me.Textbox.AppendText(String.Format("{0}{1}", Message, Environment.NewLine))
Me.Textbox.SelectionLength = Me.Textbox.Text.Length
Me.Textbox.ScrollToCaret()
End If
End If
End Sub
This will, in my idea, avoid having these exceptions. But i'm not entirely sure if this would be the best, so i'll leave this here and see who is going to add his post.
Ps. Yes i know of 'ManualResetEvent' but thats not what im asking here.
- B.



