Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-18
10
Medium Priority
?
1,009 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 80 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 180 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 80 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 40 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

670 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