• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 787
  • Last Modified:

Multitasking with VC++

I have written a VC++ program. When this program runs it completely blocks all other programs. Well at least it seems that way since it blocks the handling of the user interface.

How can I make it yield to other processes now and then. In VB and Access I would use a call to DoEvents, but I haven't been able to find something similar for VC++.

If I would turn this program into a service would it then block out all other services?
0
patrickl
Asked:
patrickl
2 Solutions
 
jhanceCommented:
Some more information about what you are doing would really help.
0
 
MDarlingCommented:
Sleep() would yield to other processes.

But if you're running on NT (and to lesser extent 9x) your other programs should get scheduled anyway.

Regards,
Mike.
0
 
Chandra VConsultantCommented:
You write your own DoEvents function and call it in your waiting loop instead of using Sleep

void DoEvents()
{
     MSG msg;
         
     // Process existing messages in the application's message queue.
     // When the queue is empty, do clean up and return.
     while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
     {
          if (!AfxGetThread()->PumpMessage())
               return;
     }
}
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
patricklAuthor Commented:
OK I'll check these suggestions out.

Just to add some extra info:

The software in question is an "ftp client server". It is basically an automated ftp client. It ftp's files that are placed into a directory to several ftp servers.

The trouble is when the connection to an ftp server times out the GUI hangs while it waits for the timeout. But also when it is sending or doing tasks out of the event queue loop.

The software is running on a Windows NT 4.0 server.
0
 
jhanceCommented:
>>The trouble is when the connection to an ftp server times out the GUI hangs while it waits for the timeout.
But also when it is sending or doing tasks out of the event queue loop.


This is due to using BLOCKING WINSOCK calls in the main thread of your application.  Once a network call is made, it does not return to the caller until it completes, so the thread "blocks".  Since the GUI depends on the thread returning to Windows to be able to process the next message and remain responsive to the user, it hangs.

This is poor GUI design since users get frustrated when the application appears unresponsive.  They don't know if it's working or it's hung.  There are two solutions:

1) If you must use the blocking WINSOCK calls, put them in a separate thread.  Then, they can complete in their own time but the main thread can still handle Windows messages and be responsive.  

2) My preferred method is to use WINSOCK ASYNCHRONOUS calls which are those beginning with WSA.  These, return immediately under all conditions.  They then post a message to your app when the operation completes and you can deal with it in the main message loop when it comes.  These take a bit of getting used to if you have only used the standard sockets library but once you spend some time using them you'll see that they fit into the Windows architecture better than other methods.  
0
 
patricklAuthor Commented:
Sorry, forgot all about this question. I guess I missed a notification mail since I completely missed that last response!

I used cvallabhaneni's answer for something else already and I just read jhance's reply which would probably work. In the mean time we bought WS-FTP Pro which does this stuff standard so no need to program things myself anymore.

Thanks all for the help!
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now