Solved

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

Posted on 2007-11-18
10
1,003 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
[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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
Build VS2015 project in bat command 3 108
Unix Command -- Challenging  question 7 106
Microsoft C++ code failing in executable that worked 9 144
What are MicroServices? 4 67
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…

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