Twain driver crashes, requiring restart of WIA...?

Discussions about TWAIN & WIA scanning in GdPicture.NET using GdPictureImaging.
Post Reply
reynolds

Twain driver crashes, requiring restart of WIA...?

Post by reynolds » Sat Feb 02, 2008 1:21 am

I was having a problem where if my program errored out, the twain driver wouldn't work until I rebooted. The problem was caused by not closing the twain driver before exiting. Not a graceful recovery at all. However, I found that instead of having to reboot, I could fix the problem by restarting the WIA service.

A twain driver is not a WIA driver, why is it required to either reboot or restart the WIA service if you exit without closing the twain driver? That doesn't make any sense, and it makes for extremely fragile code that is not robust.

reynolds

Re: Twain driver crashes, requiring restart of WIA...?

Post by reynolds » Sat Feb 02, 2008 1:47 am

Hmmm.... Now restarting the WIA service doesn't work anymore, only rebooting does...

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Twain driver crashes, requiring restart of WIA...?

Post by Loïc » Sat Feb 02, 2008 1:16 pm

Hi,

The first thing to do is to try to avoid crash from your application.
Also, I suggest you to close the device source each time you don't need to use it.

Only your application can close an opened source. This can be done using one of the TwainCloseSource(), TwainCloseSourceManager() or TwainUnloadSourceManager() methods or releasing from memory the GdTwain object.

Loïc

reynolds

Re: Twain driver crashes, requiring restart of WIA...?

Post by reynolds » Mon Feb 04, 2008 9:26 pm

Yes, I have a try / catch block around the areas where it needs to be open, so I'm only getting the problem if I terminate execution while stopped at a breakpoint in the debugger.

However, if you know an error can be generated, there is always the distinct possibility that it might pop-up at some other time, and so it is always a good idea to have a special catch case for that possibility, if it can be dealt with. Even if the resetting of the WIA service doesn't work 100% time, if it works even some of the time I think it is better to try that out before letting the program bomb.

Why does it stay stuck until you reboot? Isn't there some other way to reset the state of the driver?

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: Twain driver crashes, requiring restart of WIA...?

Post by Loïc » Tue Feb 05, 2008 4:13 pm

Hi,

Why does it stay stuck until you reboot?
Like I said before, if you open the source you need to close it yourself using the methods I gave you or releasing the gdtwain objects from the memory.

Why does it stay stuck until you reboot? Isn't there some other way to reset the state of the driver?
I think it is an unfortunate behaviour of the WIA layer. I can only suggest you to try to unplug and plug again your device or to use the TWAIN driver of it.

Regards,

Loïc

reynolds

Re: Twain driver crashes, requiring restart of WIA...?

Post by reynolds » Tue Feb 05, 2008 9:52 pm

The code listed below will restart the WIA service. I've tested it a dozen times now by terminating execution in the debugger without letting it go to the exception catch. The vast majority of the time it works great. However, I'm still getting a msgbox, directly from the driver I suspect, that says the scanner can't be accessed: "Communication failed. Make sure the power is on, the cable is firmly connected, etc..."

Is there any way to shut that dialog box off? It stops execution.


Private Sub RestartWIAService()
Dim Service As ServiceProcess.ServiceController = Nothing
Try
Dim serviceDisplayName As String = "Windows Image Acquisition (WIA)" 'Display Name
Dim services As ServiceProcess.ServiceController() = ServiceProcess.ServiceController.GetServices()
For Each Service In services
If Service.DisplayName = serviceDisplayName Then
Exit For
End If
Next Service
Catch ex As Exception
MsgBox("Error while intitialising scanner driver." & vbCrLf & "Cannot enumerate the services.")
End Try

Try
If Not Service Is Nothing Then
If Service.CanStop AndAlso (Not Service.Status = ServiceProcess.ServiceControllerStatus.Stopped) Then
Service.Stop()
Service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped)
End If

Service.Start()
Service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)
End If
Catch ex As Exception
MsgBox("Error while intitialising scanner driver." & vbCrLf & "Unable to restart Windows Image Acquisition (WIA) service.")
End Try
End Sub

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest