Solved

Multi-threading

Posted on 2004-04-24
7
191 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now