Solved

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

Posted on 2006-11-02
9
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 86

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 86

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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 86

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 86

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 86

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

739 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