Solved

Multi-threading

Posted on 2004-04-24
7
198 Views
Last Modified: 2010-04-24
I tried to write an windows application using my .dll file.  I created a library MyLibrary in which I implemented a class called Scanner.  In the Scanner class, I had a method called Scanning which used a method from another .dll file to scan the driver license.  In the Scanner class, I also had another method called MyThread which created a thread with the starting entry is Scanner (New Thread(AddressOf Me.Scanner)).  Also, I created a windows application with a form and a button.  I import MyLibrary into this windows application.  When I click the button on the form to scan the driver license, the scanner scanned the driver license, but I could not interact with the form to type something in during the time the scanner scanned the driver license.  After it finished scanning, then I could interact with the form.  Is there anyway that I can interact with the form during the time the scanner scans the driver license? Please help me out and it would be nice if I can see the sample code.  Thanks.
0
Comment
Question by:UiTroi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 5

Expert Comment

by:KeirGordon
ID: 10908243
It is hard to help without seeing some of your code, but typically to solve an issue such as this one would insert Application.DoEvents() calls within any loops they have.  This call allows any pending messages to be proccessed and thus will often fix the issue of your form not responding while in a loop.

0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 125 total points
ID: 10914464
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this is not a very good multi threaded way of doing things in a multi-threaded environment ...
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

I am assuming that what you want is this scanner class to scan the image, nothing more nothing less ...

The scanning should occur within its own thread ...

Your application needs to be alerted to the completion of the scanning process (preferrably with the scanned image) ...

It sounds to me like you have a simple issue with your threading ....

Thread 1 (main application thread calls into Scanning() which I will assume returns an image. Scanning creates a new thread which actually does the scanning returning. The Scanning method then returns leaving thread 2 running. What does your main thread do at this point ? It sounds to me like it goes and does a few other things and then tries to access the image which is at present still being scanned. If you are locking the image while it is being scanned this would cause Thread 1 to be blocked until the scanning was done (thus making your interface lock up as it is the thread processing the interface).

A simple solution to this would be to use a delegate (aka a callback)
Public Delegate Sub ScanCompletedDelegate (ByVal Image as Image)
Then on you pass this value into your class or you create a custom event for your class that maps to this delegate ...

example Scanning(ByVal CompletedHandler as ScanCompletedDelegate)
then you would call the delegate with the image you get.

you can also do this with an event.

here is some quick only typed in here code (aka not tested) that should show the generally idea of using an event + a thread

Public Event ScanCompletedEvent(byval Image as Image)

doing this actually creates a delegate behind the scenes (if you were in C# you would do it yourself)

Public Class Scanner
    Private SingleScanThread as Thread
    Public Event ScanCompletedEvent(byval Image as Image)

    Public Property IsRunning
          Get
               //lock this
               dim ret as boolean = SingleScanThread is nothing
               //unlock this
               return ret
          End Get
    End Property

    Protected Sub OnScanCompletedEvent(ByVal Image as Image)
         RaiseEvent ScanCompleted(Image)
    End Sub    

    Protected Sub GetMyImage()
          try
              dim img as Image = OtherLibrary.DoScan()
              OnScanCompleted(img)
          catch Ex as ThreadException
              'Thread was cancelled
          catch Ex as Exception
              'Raise most likely another event which would alert the user to the other error
          end try
    End Sub

    public sub CancelThread()
          if this.IsRunning
               //lock this
               SingleScanThread.Abort()
               //unlock this
          end if
    end sub

    Public Sub Scanning()
          //lock this
          SingleScanThread=new Thread
            (new ThreadStart(addressof Me.GetMyImage))
          //unlock this
    End Sub
End Class

'on form
   Private WithEvents Scanner As Scanner = New Scanner()

   Public Sub DoScan()
      Scanner.Scanning()
   End Sub

   Private Sub ScanCompleted(ByVal Image as Image) Handles Scanner.ScanCompletedEvent
      Trace.Write("Scanning Completed")
   End Sub


Hope this helps you out.

Greg
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10914467
oops forgot when the thread dies to set it to nothing (so isRunning still works ...)

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10914472
also I believe its Stop() on the thread not abort()

http://www.dotnetbips.com/displayarticle.aspx?id=39 is a decent itro to threading reference
Also MSDN has a ton of good examples if you look in the threading area :)
0
 

Author Comment

by:UiTroi
ID: 11061071
This is not quite what I need, but it gives me the direction.  The problem was that when I was scanning, all theads were blocked.  I think that it is the problem of ActiveX control.  Anyway, thanks a lot for your time and your help.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11061174
the code above handles that case by starting a thread (which gets blocked) and calling back to your other thread (which is still alive) when the image completed scanning ...
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11061248

this creates a thread and returns to your main thread.
    Public Sub Scanning()
          //lock this
          SingleScanThread=new Thread
          (new ThreadStart(addressof Me.GetMyImage))
          //unlock this
    End Sub

this runs in the new thread
    Protected Sub GetMyImage()
          try
              //this calls your activex control
              dim img as Image = OtherLibrary.DoScan()

              //this messages your first thread which has been processing other tasks that the image has been completed (and delivers the image to the first thread)
              OnScanCompleted(img)
          catch Ex as ThreadException
              'Thread was cancelled
          catch Ex as Exception
              'Raise most likely another event which would alert the user to the other error
          end try
    End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question