What's the best way to deal with a stuck COM call?

ikantspelwurdz

Well-known member
Joined
Dec 8, 2009
Messages
49
Programming Experience
1-3
I have a COM dll, which has a class that we'll call "MagicComObject." I have access to its source code, which is VB6, but it is too complicated for me to completely understand. My VB.NET project has a section that looks like this:
VB.NET:
        Dim iCounter As Integer = GetCounterFromRegistry()
        Dim mco As New MagicComObject
        While iCounter <= mco.Count
            Try
                mco.DoMagic(iCounter)
            Catch ex As Exception
                logger.WriteDebug(iCounter, ex.ToString)
            End Try
            iCounter += 1
            SetCounterToRegistry(iCounter)
        End While
The problem is that sometimes mco.DoMagic hangs. When it does hang, the CPU use is negligible, but my VB.NET code freezes right here, waiting for DoMagic to finish up.

I want to have a timeout setting, and have my code give up on mco.DoMagic if it does not return after this amount of time, kill the function in progress, and move on to the next one. Is this possible? I am assuming this will require using multithreading, which I am vaguely familiar with, but I don't really know what is considered best practices for using - my reading indicates that there are many ways to do multithreading, and some look more confusing than others. I am also concerned about zombie threads and memory leaks resulting from killing the DoMagic function prematurely.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,314
Location
Sydney, Australia
Programming Experience
10+
Are you able to target .NET 4.0 or later? If so then you can use the TPL. The Task class allows you to run a task asynchronously and then wait up to a certain number of seconds for it to complete.
 
Top Bottom