Hi Peter,
The sample I posted is the whole code, there is nothing missing apart from
the form design and the program.cs. The code as is is exactly as I selected
all, then copied/pasted. I don't know what else I can add to it to make a
full and complete sample.
There are 2 buttons on the form. One is to select the source (this should be
done to ensure you are using the correct scanner and should be done prior to
scanning). The other is to run the scan.
If it is missing something, then I am missing something in the development,
but I don't know what, which I guess is partly what I am asking for.
It appears that none of the WIA objects has a dispose method. Some of the C#
samples I have seen have been using the ReleaseComObject to release (after
checking that it is not null first)
I have initialised the img now... ImageFile img = new ImageFile[50];
(The sheet feeder has a max sheet count of 50)
I have removed the ReleaseComObject lines as well.
What is really odd now is that the scan will proceed, though still stops and
holds onto the third sheet. I have to press the cancel butto on the scanner
itself, but it still hold the sheet and I have to pull it out.
I am single stepping... each step is highlighted in yellow (as I expect)
The first picture is saved in the first loop, so the first run is OK. The
second loop will stop on the scanItem.Transfer with a green highlight. (The
yellow highlight was on that line immediately prior to stepping, as soon as
I stepped, it green highlighted)
The call stack is as follows...
[Managed to Native Transition]
> Scan.exe!Scan.Test.RunScan() Line 91 + 0x14 bytes C#
Scan.exe!Scan.Test.button1_Click(object sender = {Text = "Scan"},
System.EventArgs e = {X = 0x00000089 Y = 0x0000000c Button = Left}) Line 52
+ 0x7 bytes C#
System.Windows.Forms.dll!System.Windows.Forms.Control.OnClick(System.EventA rgs
e) + 0x6a bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnClick(System.EventAr gs
e) + 0x49 bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.OnMouseUp(System.Windo ws.Forms.MouseEventArgs
mevent = {X = 0x00000089 Y = 0x0000000c Button = Left}) + 0xc8 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref
System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button,
int clicks) + 0x2f7 bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref
System.Windows.Forms.Message m) + 0x526 bytes
System.Windows.Forms.dll!System.Windows.Forms.ButtonBase.WndProc(ref
System.Windows.Forms.Message m) + 0xc7 bytes
System.Windows.Forms.dll!System.Windows.Forms.Button.WndProc(ref
System.Windows.Forms.Message m) + 0x2b bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.O nMessage(ref
System.Windows.Forms.Message m) + 0xd bytes
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.W ndProc(ref
System.Windows.Forms.Message m) + 0x36 bytes
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallba ck(System.IntPtr
hWnd, int msg = 0x00000202, System.IntPtr wparam, System.IntPtr lparam) +
0x57 bytes
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageL oop(int
dwComponentID, int reason = 0xffffffff, int pvLoopData = 0x00000000) + 0x2f1
bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(int
reason = 0xffffffff, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.ApplicationContext}) + 0x17d bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.Run MessageLoop(int
reason, System.Windows.Forms.ApplicationContext context) + 0x53 bytes
System.Windows.Forms.dll!System.Windows.Forms.Application.Run(System.Window s.Forms.Form
mainForm) + 0x2e bytes
Scan.exe!Scan.Program.Main() Line 18 + 0x1a bytes C#
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile,
System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x39
bytes
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio. HostingProcess.HostProc.RunUsersAssembly()
+ 0x2b bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object
state) + 0x3b bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.Executi onContext
executionContext, System.Threading.ContextCallback callback, object state) +
0x81 bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x40 bytes
So, I cannot proceed after I have a green highlight, so I have to stop the
debugging and click the cancel button on the scanner and pull out the third
sheet. I then stack the sheets again and run the program again, single
stepping.
The scanner then clicks but doesn't scan, however, the program still runs
and when it saves the image, it is now actually the second image from the
ADF that is saved. So, to me, there is something with the scanItem.Trasnfer
line again. The current green message is "Value does not fall within the
expected range". Single stepping then goes into the Dispose of the
Test.Designer.cs then studio stops debugging.
It is now at this point that I have to reboot the computer as nothing will
work anymore.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Peter Duniho" <no.peted.s
...@no.nwlink.spam.com> wrote in message
news:%234VlXBjfKHA.5036@TK2MSFTNGP04.phx.gbl...
> David wrote:
>> Hello,
>> I know that WIA is not everyones speciality. Unfortunately, there is not
>> a lot of info on the net about using it within .NET and what I do find is
>> not quite suitable for what I am trying to achieve.
>> However, my main question here is am I using COM correctly? Am I closing
>> it down properly so that I can re-use it later?
> I would say that, as a general rule, if you need to dispose an object,
> even a COM object, you would use the Dispose() method. COM interop is
> supposed to handle the COM reference counting on our behalf. I would
> think that the ReleaseComObject() method would be needed only when for
> some reason you are dealing with extra ref counts outside of the single
> count you get when you create a COM object via COM interop in .NET.
> That said, surely either would work, and in any case, unless the COM
> server has a requirement that only one instance of the object ever exists
> at any given time, I don't know why failing to release one instance would
> prevent another from being created. That kind of requirement is unusual.
> In fact, based on the docs for ReleaseComObject(), which point out that if
> you try to use a COM object _after_ it's been release, you'll get a
> NullReferenceException, and since that's the exception you're getting, my
> first thought is that maybe you are releasing the COM object too soon. In
> other words, yes…you have an object lifetime bug, but it's the other way.
> That is, you're releasing it when you shouldn't, rather than not releasing
> it when you should.
> Unfortunately, from the code example it's not really clear how that would
> happen, especially with respect to the line you call out as the
> problematic one. I suppose one explanation might be that you are
> debugging a "Release" build of the program, and so there's not a precise
> correlation between the program line and the actual call stack (the
> green-highlighted line is trying to tell you that's a line in the call
> stack during the exception, but not the actual line where the exception
> occurred…the actual line would be marked in yellow).
> The code as posted seems like it has an obvious null reference problem.
> But as noted above, a green highlight should mean the exception is
> actually occurring lower in the call stack. So your description seems
> inconsistent with the apparent problem (i.e. I'd expect yellow
> highlighting in the "RunScan()" method, not green).
> That said, I'll point out the apparent problem and you can figure out if
> it's a real one or not: your code appears to be trying to use the "img"
> local variable in the "RunScan()" method without the variable ever being
> initialized (it's initialized to "null", rather than an actual array
> instance).
> It's also not clear to me why it's valid to always release the "scanItem"
> object, even if it is a COM object you get from the Scanner object. I
> don't know the specifics about the Scanner object, but I wouldn't expect a
> simple indexer retrieval to add-ref the interface returned. Are you sure
> it does?
> And of course, releasing the "img" variable when it's only ever null would
> be pointless at best, and wrong at worst (the docs say ReleaseComObject()
> will throw an ArgumentException if you pass it null).
> It is always the case that without a concise-but-complete code example it
> can be difficult or impossible to determine a problem in someone else's
> code, and I think that's especially true in this particular context. I
> realize there are probably a lot of prerequisites to executing this code,
> but the fact is, without being able to see the exception "live" it's hard
> to know for sure what we're looking at. :(
> Pete