none
Stop running .dll RRS feed

  • Question

  • I've developed a custom .dll in Visual Basic .NET 2008. I'm using the code below in my calling program to call the custom .dll

    'Call the .dll

    Dim caller2 As New AsyncMethodCaller(AddressOf MyProject.Main)

    threadId = Thread.CurrentThread.ManagedThreadId()

    caller2.Invoke(3000, threadId)

    'Code Returns here
    ......

    When the code returns, I'm able to tell that the .dll is still running. How do I stop/end the .dll? Or do I need to stop the thread that is calling the .dll? I've looked at EndInvoke, but it doesn't seem to play well with .Invoke, but rather, it looks like it needs to work with .BeginInvoke. But because I need to call my .dll syncronously, I have to use .Invoke.

    Thursday, August 14, 2008 8:11 PM

Answers

  • I don't get it, using Invoke is pretty pointless.  Why not just:

      MyProject.Main(3000, threadId)

    How do you know it "is still running"?  A DLL doesn't run, it is just a collection of code.  Threads run.  You are starting a thread with Invoke() but you'll wait for it to end.  BeginInvoke() doesn't wait.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Tuesday, August 19, 2008 12:05 PM
    Friday, August 15, 2008 2:52 AM
    Moderator

All replies

  • The CLR does not unload an assembly (e.g., a DLL) until the AppDomain is unloaded.

    Most applications only use one AppDomain, which means the DLL stays loaded for the lifetime of the application.

    You can create a separate AppDomain which you can unload on demand.  However, you will need to do extra work to communicate with code running in that other AppDomain, and this sometimes gets complicated.

    See the following page for more information:  http://msdn.microsoft.com/en-us/library/yb506139.aspx

    [quote]
    For most applications, you do not need to create your own application domain; the runtime host creates any necessary application domains for you. However, you can create and configure additional application domains if your application needs to isolate code or to use and unload DLLs.
    [/quote]

    Friday, August 15, 2008 12:18 AM
  • I don't get it, using Invoke is pretty pointless.  Why not just:

      MyProject.Main(3000, threadId)

    How do you know it "is still running"?  A DLL doesn't run, it is just a collection of code.  Threads run.  You are starting a thread with Invoke() but you'll wait for it to end.  BeginInvoke() doesn't wait.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Tuesday, August 19, 2008 12:05 PM
    Friday, August 15, 2008 2:52 AM
    Moderator
  • BC -  I think an AppDomain is too complicated for me right now. But thank you for the suggestion and I will learn about it for future use.

    Nobugz - I understand when you say that a .DLL does not run, rather it's the code that is running in the DLL that is running. Might have worded that incorrectly. As you suggested, I was able to remove the .Invoke method and changed my code to:

    MyProject.Main(3000, thread) and that works just fine.

    I can tell the .dll is running because a textbox in the .dll that is used to search is still populated when I make a call to the .dll again. Clearing it does the trick, obviously, but I think programmatically it would make more sense for the .dll code to stop running when I am done with it.

    Are you saying that I can tell when the thread(that is running the dll) has ended? Can I force the thread to an end?
     
    I did try:

    Thread.CurrentThread.Abort()

    but that threw up an exception.

    Friday, August 15, 2008 3:55 PM
  • You've got a very confused notion of how code executes.  I don't really know how to whack you out of that mode of thinking.  I'd definitely recommend you stay away from threads until you've had a chance to do some reading.
    Hans Passant.
    Friday, August 15, 2008 11:14 PM
    Moderator