Hello experts
Here I found how to listen to disk inserted or new drive added events:
https://learn.microsoft.com/en-us/answers/questions/1555337/optical-drive-disc-inserted-event?source=docs
And this is my code:
<SecurityPermission(SecurityAction.LinkDemand, Flags:=SecurityPermissionFlag.UnmanagedCode)>
Protected Overrides Sub WndProc(ByRef m As Message)
If Button1.Enabled = False Then Exit Sub
If m.Msg = WM_DEVICECHANGE AndAlso m.WParam.ToInt32() = DBT_DEVICEARRIVAL OrElse m.WParam.ToInt32() = DBT_DEVICEREMOVECOMPLETE OrElse m.WParam.ToInt32() = DBT_DEVNODES_CHANGED Then
If m.LParam <> Nothing AndAlso Marshal.ReadInt32(m.LParam, 4) = DBT_DEVTYP_VOLUME Then RefreshButton_Click(Me, Nothing)
End If
MyBase.WndProc(m)
End Sub
But however, this line:
If Button1.Enabled = False Then Exit Sub
Will cause the form stop working normally and strange behavior, almost hang!
Removing that single line will make it work as expected!
Am I doing it correctly? And is this method safe at all or forget it?
One more thing is that is it safe to use it on multiple forms?
Thanks :)