Solved

Keeping a background thread alive, even after the main application exits.

Posted on 2007-11-18
10
998 Views
Last Modified: 2013-12-04
I need one of my program’s background thread to keep running (will self terminate when it finishes); even when the process’s main thread has terminated. I do not want/need to keep the application’s primary thread alive.

Example:
Main Application
Thread process big file (e.g. add 9 to each byte inside the file, and write it to another file)
End Main Application
2 minutes later, thread finishes processing the big file.
I am currently creating worker thread with MFC’s AfxBeginThread, it will need to work with W2k, Xp,Vista (locked down)
Thank you
0
Comment
Question by:jmc0
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 40 total points
ID: 20310806
you need to create another process, not another thread, the simplest way to do it is to run another application.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 90 total points
ID: 20311055
When the primary thread of a process terminates so to do all the other threads. You'll need to spawn another process if you want it to live beyond the parent. You can do this using the API function CreateProcess()

http://msdn2.microsoft.com/en-US/library/ms682425.aspx

-Rx.
0
 

Author Comment

by:jmc0
ID: 20313244
Already aware of the CreateProcess function and what happens when process terminates (all its thread terminates). That's why, I was hoping someone can provide a alternate simple solution, specifically to keeping thread alive, even if the main application has ended.
Currently my 'thread' code is not an application/process exe; which is required for the CreateProcess function (unless someone can tell me how to CreateProcess with just a pfnThreadProc 'Points to the controlling function for the worker thread') ; this is why I don't  create another process.
I need one of my program's background thread to keep running (will self terminate when it finishes); even when the process' main thread has terminated. I do not want/need to keep the application's primary thread alive.
 

Example:

Main Application

Thread process big file (e.g. add 9 to each byte inside the file, and write it to another file)

End Main Application

2 minutes later, thread finishes processing the big file.

I am currently creating worker thread with MFC's AfxBeginThread, it will need to work with W2k, Xp,Vista (locked down)

Thank you

Open in new window

0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 40 total points
ID: 20313268
You can simply hide your main window and wait for the secondary thread to terminate.
User will assume that application has ended, but it will be still processing in the background.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 20 total points
ID: 20313545
Put this function into another application (no/hidden main window) and launch this new application as Jaime initially suggested.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Closing Comment

by:jmc0
ID: 31409893
None of the answer provides a 'real' solution to my problem.
0
 

Author Comment

by:jmc0
ID: 20346825
None of the answer does what I 'want' to do...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20346897
>> None of the answer does what I 'want' to do...
Unfortunately that's because what you want to do can't be done - at least not in the way you want to :(

If you are unhappy with the answers you should post a message to CS and ask then to delete the question and refund your points.

http://www.experts-exchange.com/Community_Support/General/

Sorry.

-Rx.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 20347049
An answer that is negative is still an answer.  (Would you have preferred no-one made any comments and you wasted many hours searching yourself?)

Why is it only worth a 'C' grade just because you would rather have had a positive answer?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20347266
>> An answer that is negative is still an answer.  (Would you have preferred no-one made any comments and you wasted many hours searching yourself?)
I agree! I wasn't saying we didn't give good answers, I was just pointing out that if the OP honestly felt we hadn't provided an appropriate response s/he could post a message to CS. It would, of course, then be down to a moderator to decide. Ultimately, the answers are right and I feel sure a moderator would clarify this!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
post4 challenge 28 82
What are new names for Microsoft test frameworks once called Moles and Pex? 2 53
scoreUp challenge 14 48
Not needed 13 54
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now