Disconnecting iTunes COM - vb.net

I'm currently working with the iTunes COM with .NET, and something I came across previously, which stopped me using it, has happened again and I can't for the life of me figure it out.
When I go to close iTunes during or after my program has closed, it tells me something is still using the "Application Scripting Interface", COM is still connected.
This is what I have (removed what is not required)
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler itunes.OnAboutToPromptUserToQuitEvent, AddressOf itunes_OnAboutToPromptUserToQuitEvent
End Sub
Private Sub itunes_OnAboutToPromptUserToQuitEvent()
System.Runtime.InteropServices.Marshal.ReleaseComObject(itunes)
End Sub
So, the above code does disconnect the COM to the extent that I need to restart iTunes to use it again, but, it doesn't get rid of the Application Scripting error. Meaning, I still need to click quit after the error dialog comes up. Everything else works fine, apart from this.
Any idea?
-- I've had a look at other questions that had this issue, but none of them resolved it for me. I'm not sure if the event doesn't work anymore with this current version of iTunes, but, it doesn't seem to work currently either way.

Had same issue with COM and Office, this solved it;
While System.Runtime.InteropServices.Marshal.ReleaseComObject(itunes) <> 0
Application.DoEvents()
End While

Related

VB Forms don't recognize each other in the same project

Hi everyone I need some help,
I am having a weird situation every time I try to call a window form I get this error
BC30469 Reference to a non-shared member requires an object reference.
I was originally working on Visual Studio 2010 when the first time contouring this problem so I thought that I may accidentally deleted or edited some code in the declaration of the form witch caused the problem so I closed the solution and created a new one to make sure that the problem is limited the solution not to VS. then I add window form "Form2" then created/added a button1 on Form1 to call Form2.Show()
simple code that should work fine but when I tape Form2.Show() it give that ERROR and red mark Form2
so I uninstalled VS2010 then Reset Windows 10 with option to wipe out all data on windows partition (I now that was extreme but I suspected that maybe the system was infected with some virus "prior action") so after that I checked the system with HitmanPro and found nothing then I installed VS2019 Community and get The some problem I searched on the web but did not found any similar case so here I am hoping that someone will resolve the mystery.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
End Class
Form2 is empty form I didn't make any change on it
Before this problem showing up everything work fine now even old project have the same issue
Thanks
Edit: Add project as simple
https://mega.nz/file/FgoXkCwA#ootxYrXGnR6sQR_Pifjvz617-r_Az1ozXWB49oGxqKU
the project dose not contain any executable file
I usually do something like this when calling a subform:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' using a Using block:
Using form2 as New Form2
form2.ShowDialog(Me)
form2.Close()
End Using
' using a With block
With New Form2
.ShowDialog(Me)
.Close()
End With
End Sub
ShowDialog(Me) keeps the subform open until a DialogResult is provided by the user (OK or Cancel usually).

Closing main form doesn't finish process (only some computers)

The problem is happening on 2 of the company's computers and on a client computer, but we can not identify a pattern.
I was able to reproduce the error using a simple program that only opens an OpenFileDialog. The program must be run by the generated executable itself (NOT by Debug) and it is still running in the background even after closing. Below is the code of the program, along with a link to download the project and a video demonstrating the error.
Code:
Public Class Form1
Private ofdAbrir As Windows.Forms.OpenFileDialog
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ofdAbrir = New Windows.Forms.OpenFileDialog
ofdAbrir.ShowDialog()
ofdAbrir.Dispose()
ofdAbrir = Nothing
End Sub
End Class
As you can see in the code above, I only have one form, so it is not the case that some form remains open and it is also not related to threads running since none is created.
To reproduce the problem, click on Button1, cancel the OpenFileDialog and try to close the form (clicking on X). The form apparently will close, but you will see at task manager that it still running. The big mystery is that this problem does not happens in all computers.
Video: https://drive.google.com/open?id=1sfdVUGQlwYNCQkl1Ht-cJSOb4433sqnT
Project: https://drive.google.com/open?id=1d4oJYUjaaZ9xnRj4CX3HXOQPqwMZmE0V
I couldn't reproduce the problem, but how about using this method:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using ofdAbrir As New Windows.Forms.OpenFileDialog
ofdAbrir.ShowDialog()
MsgBox(ofdAbrir.FileName)
End Using
End Sub
I've seen the video u posted...It is not your application that is running rather it is the vsHost.exe that is running :)...The problem shouldn't occur if u run the app outside visual studio.
If the problem is still there,just disable Enable Diagnostic Tools while debugging from Tools > Options > Debugging > Uncheck Enable Diagnostic Tools while debugging
Also u can disable Visual Studio hosting service from Project > Project Properties > Uncheck Enable the Visual Studio hosting service

How do I disable all exiting in Visual Basic

I'm writing an examination piece of software for my workplace and would like to know how I can trap and cancel key-presses such as:
ALT+F4
WIN+TAB
ALT+TAB
WIN
CTRL+ALT+DEL
I'm aware CTRL+ALT+DEL may not be possible, but if any of this is it'll be a step in the right direction!
Ideally I want to prevent the action, and then open a new form I've created saying 'Unauthorised keypress'
as #SQLHound link relates... us the FormClosing event to handle what happens when a user attempts to close. But if you want to block a boot attempt, then #Plutonix suggestion may help. Something along these lines...
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim splashScreen as New Form
splashScreen.OpenDialog
e.Cancel=True
End Sub

Custom hotkey even with unfocused form

I am having a bit problems trying this. I must admit that I am still novice programming. Well, I managed for do this in this way (VB.NET):
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If My.Computer.Keyboard.CtrlKeyDown Then
MORE IRRELEVANT CODE HERE
End If
End Sub
But with this way, even if it work, it dont let me customice the key shortcut (only Control, shift and others).
I did this too for try differents things:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = GlobalVariables.own_key Then
MORE IRRELEVANT CODE HERE
End If
End Sub
I give value to GlobalVariables.own_key from other button. This work perfectly, but only if program have the focus.
So... With the first code program work even if it haven't the focus but only with few keys... And with the second it let me use any key, but don't work if it haven't the focus.
I tryed understand the "keyboard hook" but I must admit that I didn't understad it and couldn't manage for work any of them.
Using VB.net 2012.
Really thanks so much for your help
It may not be an answer you like but it is the answer. In .NET, you cannot get notification of keys when your application does not have focus and you cannot get mouse events outside your applications windows. If you looked into the "Keyboard Hook", you should have read that little important fact regarding VB.
Global hooks are not supported in the .NET Framework

Cause program crash on purpose in VB.NET

I'd need a way to cause the program to crash on purpose when i click a button. But nothing comes to my mind that would still allow me to compile the program. any code that causes a hard crash for whatever reason. in particular i need it to close and not be able to continue. My beta testers need to test the recovery after crash feature. Thanks!
these things never happen when they should..
How about just throwing an unhandled exception?
Private Sub btnCrash_Click(sender As System.Object, e As System.EventArgs) Handles btnCrash.Click
Throw New System.Exception("The program has crashed catastrophically!")
End Sub
To effectively kill the process use Environment.FailFast() in a button click handler, like this:
Protected Sub buttonCrash_Click(sender As Object, e As EventArgs) Handles buttonCrash.Click
Environment.FailFast()
End Sub
This will not generate any exceptions, etc., it is the same as going to Windows Task Manager and killing the process.
Here is the documentation for Environment.FailFast Method (String)
How about the Environment.FailFast method?
Private Sub btn_click(...)
Environment.FailFast("Failure!")
End Sub