Solved

Thread with an Icon.Save command  cannot be ended by Scheduled Task "End Task" option.

Posted on 2006-11-02
9
155 Views
Last Modified: 2010-04-30
I have a VB.Net console application that issues an Icon.Save command to a memory stream. I defined a Scheduled task to process it. I issue start and stop commands from the Scheduled Tasks menu.

When I issue the end task, the status on the Scheduled Task display goes from "Running" to blank. However, from the Task Manager display the process is still there. The main task is still running; I put an Event log display in to verify it.

The problem occurs on 2 XP/Pro SP 2 (current) machines. A W/2000 server machine does not have the problem. The code was compiled with VS 2003. Here is a sample program to illustrate:

Imports System.Threading
Imports System.Drawing
Imports System.IO

Module Module1

    Dim Thr1 As Thread
    Dim stream As New MemoryStream
    Dim tIcon As New Icon("C:\Mike\OffStor\Icons\BackupSet\BackupSet.ico")

    Sub Main()

        Thr1 = New Thread(AddressOf Thr1Sub)
        Thr1.IsBackground = False
        Thr1.Start()

        While True
            Thread.Sleep(5000)
        End While

    End Sub

    Private Sub Thr1Sub()

        tIcon.Save(Stream)

    End Sub

 
End Module
0
Comment
Question by:MikeBroderick
  • 5
  • 4
9 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17859042
What exactly are you trying to achieve with this code?

This part:

        While True
            Thread.Sleep(5000)
        End While

causes an infinite loop and an unrepsonsive app due to the Sleep() call.

Give us the big picture...
0
 

Author Comment

by:MikeBroderick
ID: 17859195
This is huge simplification of the application. The main task is a tcp listener task that processes incoming commands. The sub task does lots of stuff then ends. it receives the name of a file to process, retrieves the associated icon for that file, then convert the icon into a base 64 string for further processing. I removed all of that because none of it causes the problem.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17859273
Well, the only thing that pops out is the IsBackground() setting of False:

    Thr1.IsBackground = False

From:
http://msdn2.microsoft.com/en-gb/library/system.threading.thread.isbackground.aspx

    "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."

Since it is NOT a background thread the app is waiting for it to complete before ending...
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

Author Comment

by:MikeBroderick
ID: 17859332
I'm sorry. Just for grins I changed that to False to see if it would work (grasping at straws) and forgot to change it back. True fails also.

I think the sub task is ending OK, it executes to end. If I comment out the icon.save command, I dont have the problem.

Is there any (efficient) way to search Microsoft for known problems, or to report the problem without paying 99 dollars for a support charge?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17859413
Are you actually using some kind of loop to keep your main task alive?

        While True
            Thread.Sleep(5000)
        End While

Look at inheriting from the ApplicationContext() class instead:
http://msdn2.microsoft.com/en-US/library/system.windows.forms.applicationcontext.aspx

A simple example:

Module Module1

    Public Sub Main()
        Application.Run(New MyContext)
    End Sub

    Public Class MyContext
        Inherits ApplicationContext

        Private Thr1 As Thread
        Private stream As New MemoryStream
        Private tIcon As New Icon("C:\Mike\OffStor\Icons\BackupSet\BackupSet.ico")

        Public Sub New()
            Thr1 = New Thread(AddressOf Thr1Sub)
            Thr1.IsBackground = True
            Thr1.Start()
        End Sub

        Private Sub Thr1Sub()
            tIcon.Save(Stream)
        End Sub

    End Class

End Module

0
 

Author Comment

by:MikeBroderick
ID: 17859588
Just gave it a shot. Same error.

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17859608
I'm outa suggestions then...  =(

The problem lies in the Icon.Save() method.
0
 

Author Comment

by:MikeBroderick
ID: 17859638
Thats what I came to conclude. I dont have to fix this (not enough to spend 100 dollars to ask Microsoft). Do you know of any way to check with Microsoft on known bugs with XP (it works on 2000)?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17859706
I'm not sure how to report bugs.

I just do searches on the msdn site:
http://msdn2.microsoft.com/en-us/default.aspx
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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