Results 1 to 10 of 10

Thread: Shutting Down a VB application

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2015
    Posts
    3

    Shutting Down a VB application

    I have developed a VB application which, when shut down either, by using me.close() or by clicking the 'red X', does remove the app from Task Managers current APPs list, but it places an unwanted running copy of the app in Task Managers 'Background Processes' list. When I restart the app I now have two versions running which causes errors.

    How can I stop this happening.

    Stuart Thomas

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Shutting Down a VB application

    Do you have multiple threads in this application?
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Shutting Down a VB application

    It sure sounds like you have a thread running, and the solution depends a bit on how you created that thread. There is a property to threads called IsBackground. This property doesn't do what some people expect. If you set that to False, then the thread will terminate when the application closes, but if you set it to True, the thread will keep running even after the application closes, which is what you are describing. See this link for an explanation of the property:

    http://dailydotnettips.com/2011/08/11/1538/
    My usual boring signature: Nothing

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Shutting Down a VB application

    Quote Originally Posted by Shaggy Hiker View Post
    ... There is a property to threads called IsBackground. This property doesn't do what some people expect. If you set that to False, then the thread will terminate when the application closes, but if you set it to True, the thread will keep running even after the application closes, which is what you are describing. See this link for an explanation of the property:

    http://dailydotnettips.com/2011/08/11/1538/
    Well, I've never seen a ".Net tip" that seems to be so contrary to what Microsoft says, or has been my experience. I've been using .IsBackground=True to make sure my background threads (which are receivers which may be waiting on a connection forever) exit when the main thread exits. The only Foreground thread I have is the GUI thread. All other threads I create are background threads. Only in the case of some extreme circumstance, where a secondary thread had to keep running to ensure it cleaned up finished something critical it was doing, would I leave it as a foreground thread.
    From MSDN:
    Public Property IsBackground As Boolean

    Gets or sets a value indicating whether or not a thread is a background thread.
    true if this thread is or is to become a background thread; otherwise, false.

    A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.
    So, you set .IsBackground = True if you want the Thread to be stopped when the program exits. You set it False (making it a Foreground Thread), if it should continue until it decides to exit.
    Last edited by passel; Jul 18th, 2016 at 05:39 PM.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Shutting Down a VB application

    p.s. <Edit>. I went back and reread the link that SH posted. I wanted to make a comment. I couldn't make a comment, but I also couldn't understand how that could have stood all this time.
    After carefully rereading it again, I see that the article is probably correct if interpreted properly, but written in such a way that it can be quite confusing, as SH's (and my first reading of it) prove.

    The first part that is misleading is this.
    A background thread will not impose restriction to the process to terminate. Hence if you create a new thread and set IsBackground of the Thread to false, that indicates that if there is only this thread available and all the Foreground threads has terminated, please terminate the process without waiting for the Background thread to complete.
    What he is saying here, but in a misleading way, is: please terminate the process without waiting for the Background thread to complete, let it continue to run, i.e. it is a foreground thread not a background thread.

    If that is not what he is saying, he is wrong.
    (And is confusing the issue by treating the process as a thing independent of the threads, or the process as being the "main" thread. The process is made of of all the threads and the process can't terminate independent of any threads. The process exists until all threads are exited).

    And he goes on to show how a background thread acts (again, in a confusing manner).
    Lets see how to define a Background Thread in your application :

    1: Thread th = new Thread(RunMe);
    2: th.IsBackground = true;
    3: th.Start();
    4: th.Join();

    The Thread is executing a method called RunMe, which might end eventually. The IsBackground property of the Thread th is set to true, that means if we do not write th.Join, or block the main thread to this thread, the program will eventually end terminating the RunMe execution abruptly.
    What did he say?
    If you don't block the main thread by using th.Join, or some other method, the Background thread (RunMe) would end abruptly. In other words, it would be exited when the main thread exited because IsBackground = True, so it is a Background thread.
    On the other hand, if you set IsBackground to false, it will keep on running even though your main thread terminates. The process will eventually terminate only when all the Foreground thread finishes its execution.
    It will keep on running because it is a foreground thread (IsBackground = False).
    Last edited by passel; Jul 18th, 2016 at 06:05 PM.

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Shutting Down a VB application

    The article is a bit confusing, I agree.

    Personally, I always set IsBackground to true - you never want a thread hanging around when the program has been requested (either by design or forcibly) to close.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Shutting Down a VB application

    Devil's advocate: a background thread is crucial if you want to implement the evil-as-heck "minimize to notification area instead of closing" behavior.

  8. #8
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Shutting Down a VB application

    And we still do not know if this is Thomas' problem...
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2015
    Posts
    3

    Re: Shutting Down a VB application

    Well! my problem seemed to have created some interest!

    Firstly, I am not an experienced VB programmer, but I am not aware that my code creates multiple threads. I certainly don't have any code referencing 'Threads'. I did have a line of code 'Imports System.Threading' but I have removed this without curing the problem. I have tried exiting the code with various lines e.g. 'me.close()', Application.Exit(), all to no event. I still get the copy of my application running in the 'Background Processes' List.

    I only get the problem when using a 'distribution' exe version of the code. No problem when using debug in VB 2010 Express.

    My code drives RS422 coms interfaces and exchanges simple messages with another task running in the same PC and so uses sections of code copied from sample code. The build works well in all respects other than when the exe is shut down.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Shutting Down a VB application

    I suspect that the code for the RS422 coms interfaces uses threads (even if it isn't obvious), so what you should be doing is ensuring that everything to do with that is stopped/closed at the appropriate point - such as in an event related to the closing of the Form.

    It is also a good idea to disable any timer controls at the same point, for basically the same reason.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width