twain scanner Check connected devices - vb.net

I used the following code to read the scanner file and review it in the image box with twain scanner.
It works well if the scanner is connected to the computer.
If the scanner is not connected, the code stops the form and does not close.
I want a way to check the scanner connection or cancel if the scanner is not connected to the device (pc)
Try
Dim s As Collection
s = SCANNER.ScanImages()
For Each k In s
PictureBox1.Load(k)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try

Related

VB.NET catch error for ENTIRE application

Need help with some VB.net coding (NOT C or c++)
So far I'm using this code to catch errors for specific line(s) of codes:
Try
bla bla bla
Catch ex As Exception
msbox("Error: " & ex.message)
End Try
But sometimes the application stops due to an error where I don't have a catch; how do I on occasions like this call upon a specific Sub (catch the error) for ANY OTHER error in the ENTIRE application where the Sub will display the error message (where I also plan on sending my self an e-mail in that sub to notify me application has stopped)?
I'm not sure if it will conflict with all current Try/Catch commands in my application, but I would prefer to only catch the error on code that currently is not within a Catch handler.
Thank you so much!
This functionality is built into the VB application framework, which is enabled by default in WinForms applications. Open the Application page of the project properties, click the View Application Events button and add a handler for the UnhandledException event using the navigation bar at the top of the code window. Done!
The VB application framework hides some of the complexity of applications that you must implement yourself in C#. It includes a Startup event that you can handle instead of adding code to a Main method, a StartupNextInstance event to support single-instance apps with commandline arguments, multi-threaded splash screen functionality and more.
Regarding your emailing idea, just sure to add in a privacy notice before auto-emailing yourself anything in your apps; this can be a big bone of contention to users, & if an astute one catches it silently phoning home your rep is down the drain.
As for a global error handler, have a look here:
https://www.dotnetcurry.com/patterns-practices/1364/error-handling-dotnet-projects

NetworkChange.NetworkAvailabilityChanged, used in Outlook Add-In, does not fire when network cable is unplugged

I have an Outlook add-in with behavior that I'd like to modify, depending on whether a networked computer is available. To check availability, I was hoping to check once when the add-in is loaded, then wire up a handler to System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged. I have read that this event does not distinguish between real network adapters and adapters for virtual machines, and the like. As such, once the event is fired, I plan to ping the computer in question, to verify that it is available.
That said, while debugging my add-in, a breakpoint indicates that the event is not firing, even when I unplug my network cable. My pertinent code is as follows:
Imports System.Net.NetworkInformation
Imports Microsoft.Win32
Public Class ThisAddIn
Private Sub ThisAddIn_Startup() Handles Me.Startup
Try
'do an initial check to see if we are online, store result
drivesAvailable = checkForExcelNetworkDrives()
'code associated with other functionality
'wire up event handlers
AddHandler NetworkChange.NetworkAvailabilityChanged, New NetworkAvailabilityChangedEventHandler(AddressOf LocalNetwork_StateChanged)
'code associated with other functionality
Catch ex As Exception
End Try
End Sub
Then, inside a module (static class), I have the event handler.
Public Sub LocalNetwork_StateChanged(sender As Object, e As NetworkAvailabilityEventArgs)
'when the network state has changed, check to see if we can still access the Excel network drives
Dim newNetworkState As Boolean = checkForExcelNetworkDrives()
'if the drive connectivity has changed, throw a new event, to warn the rest of the system to update its behavior
If newNetworkState <> drivesAvailable Then
'then call event
RaiseEvent DriveAvailabilityChanged(Nothing, New DriveAvailabilityChangedEventArgs With {.DriveAvailable = newNetworkState})
End If
'update drivesAvailable
drivesAvailable = newNetworkState
End Sub
I haven't gotten to debugging my custom event yet, as NetworkAvailabilityChanged does not appear to be firing. Could this be because I'm using it in an Outlook add-in? Other events are wired up in the add-in's Load event, and they are working. Additionally, I stepped through the AddHandler line, and it appears to execute without exception.
My next step is to set up a timer and just ping the network computer regularly. I'd rather not resort to that, though.
I think I may have answered my own question. I have multiple network adapters, included some for virtual machines. As long any any of them were up, unplugging my actual network did nothing. Once I disabled all of the other adapters, unplugging my primary network connection caused NetworkAvailabilityChanged to fire.

Capturing System.AccessViolationException

I have a Windows service that crashes in an unhandled way with the exception System.AccessViolationException. I does happen in one of 1000 cases when trying to load a user profile (during impersonation). While I am interested in finding the solution more importantly is that I cannot capture this unhandled error - and terminate my service in a good way.
During start I have added this exeception handler:
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledException
If I hard code throw System.AccessViolationException as the same place in the code it is captured by my handler. But in the 1 out of 1000 calls the process is terminated and I can only see information about this in the event viewer.
Any ideas how to capture this?
Have you tried this?
Try
'your code
Catch ex As AccessViolationException
'write your ex.ToString() on a file or whatever
End Try

How to save a picture from a webcam to a file on the C drive

My application is extremely simple and to the point. When I click a button, it will go to an indicated IP address of a particular camera and either stream video from the IP address in iexplore, or save whatever image is currently on the camera depending if user chooses stream or static mode.
What I am having trouble with is going to a camera in still mode and saving the file to a folder/file in my C:// drive.
*The only difference between still mode and stream mode is I attach '/image' to the end of the IP address when I just want a static image. If I open the IP address just by itself it will stream the video (which is working fine).
The following code shows what I have so far, which is opening the IP address in both stream and still mode, as indicated.
I am using the ShellEx module.
Private Sub DS_Stream_Click() // Cam 1 video stream
ShellEx "http://999.999.999.100", vbNormalFocus
End Sub
Private Sub OS_Stream_Click() // Cam 2 video stream
ShellEx "http://999.999.999.101", vbNormalFocus
End Sub
Private Sub Update_Btn_Click(idx As Integer) // Cam 1 still pic
ShellEx "http://999.999.999.100/image", vbNormalFocus
End Sub
Private Sub ExecLink(Url As String, style As VbAppWinStyle)
ShellEx "iexplore.exe "
End Sub
So how can I go to 'Cam 1 still pic' IP address and save that image to my C drive somewhere( like a folder so I can go back and view all the still shots whenever I need to)?
I then plan on opening that image in a picture box on my form (so it scales down properly and retains clarity). The image shown will be the latest picture saved to my folder on the C drive.
Any thoughts?
maybe u need use of api code to download from link such az a urldownload or inet control and another ways , maybe u need use of activex control for take picture from webcam by ip or without ip using and in win7 or higher you need access permission to create file (run as administrator)

Msgbox Wait until, VB.NET

Is it possible to have a Msgbox without a button on it in a console application. I would like to have a msgbox pop up and then disappear when the task has been completed. Or could I send the msgboxresult to some form of window that would just disappear when the file has been written?
MsgBox("The users on the domain are being gathered. A prompt will appear when all information has been gathered.")
Dim userFile2 As String = savefileDialog1.FileName & ".txt"
Dim fileExists2 As Boolean = File.Exists(userFile2)
Using sw As New StreamWriter(File.Open(userFile2, FileMode.OpenOrCreate))
For Each d As DirectoryEntry In de.Children()
sw.WriteLine(d.Name)
Next
End Using
If you want to have a console app with a GUI, I'd suggest that it might be easier to just make a WinForms app. Just create a new tiny WinForms app, make the default Form small as a dialog box and make it have only one invisible Close button.
Then you can just show the Close button when it's finished.
Just remember to disable the Control box on the Form (the X up in the top right hand corner) and handle any keyboard combination that could close it.
Edit: Or if for any reason you have to have it as a Console app, then you could still write a tiny separate app that just does the GUI part that you need and have the Console app start up the GUI app, sending over the text to display.
First, a quick point. A MsgBox is a modal dialog so will halt execution until the user responds, you can't use this.
In general "console" applications should be non-graphical.
You don't want to use a console application to disiplay a window. Since .Net 4.0 is available to you may want a WPF application that can write to the console.
There is a post about outputting to the console with WPF here on SO.
You should do your work on a different thread perhaps using a System.ComponentModel.BackgroundWorker. This will allow the Window to respond to user interaction and render while your task progresses.