Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1082495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:18:23+00:00 2026-05-16T22:18:23+00:00

I’ve got an app working semi-background stuff in the GUI thread. Another thread won’t

  • 0

I’ve got an app working semi-background stuff in the GUI thread. Another thread won’t work and a timer won’t work. But I want the app to be responsive while I’m going my ‘background’ work.

I cound use application.doevents – but that gives me 3 bad choices:

  • loop with no sleeps – end peg an entire CPU (my task waits on stuff besides calcs & I/O so this matters)
  • loop with a sleep – then the app slows down
  • Try to make a ‘smart sleep’ loop that speeds up when lots of messages happen (check for messages in messagefilter function)

In the good ole’ days in sdk I can do this:

while(PeekMessage(&msg,0,0,0,PM_REMOVE) && still_working)  
{
       TranslateMessage( &msg );
       DispatchMessage(&msg);
      // do a bit of my background work with each message here, quit the loop when done.
}

Wnd have it be a bit better than DoEvents as it will process multiple messages in a row, but I still have to use ‘smart sleep’ to save cpu – I’d like to avoid that.

Also is there a way to do a primary message loop?

    MSG msg; 
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );

    }

Is there a proper .NET equivalent, or do I just have to p/invoke the sdk stuff?
Thanks!

EDIT Winforms. For all you answer-ers wondering why I can’t use application.run, or threads, what I want to do is handled the OnUnhandledException event, then leave my app open for a while while some winforms stuff closes it down gracefully and tells my watchdog program to restart the app. after allowing the user to finish a critical process, too. Yes, I know people thing that a program getting OnUnhandledException deserves to die (obviously MS), there are ‘dangerous’ things that could happen; none of them are as dangerous, however, as the wrath of my users.

Edit #2 – Looking for vb.net sourcecode exact translations of the above two functions.

Maybe somebody who’s good with p/invoke and declares could score a quick bounty 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:18:23+00:00Added an answer on May 16, 2026 at 10:18 pm

    If you are determined to write your own message loop you can.

    Application.Run is closed-source so you don’t know what it’s doing besides GetMessage, TranslateMessage and DispatchMessage, so you’d better at least call DoEvents instead. Since DoEvents only processes pending messages and doesn’t wait, you can call WaitMessage to yield the CPU until there are new messages to process.

    Declare Function WaitMessage Lib "user32.dll" () As Boolean
    
    Shared Sub Main()
        Dim f As New Form1
        f.Show()
        Do While f.Visible
            Try
                Application.DoEvents()
            Catch e As Exception
                MsgBox(e.Message)
            End Try
            WaitMessage()
        Loop
    End Sub
    

    This doesn’t end-peg the CPU, but then it doesn’t do any background work either.

    You haven’t said what your background work is exactly, but if it’s stuff which completes asynchronously you should replace WaitMessage with MsgWaitForMultipleObjectsEx – although you’ll need to get the Win32 handles of the objects you are waiting on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.