Delay inside a worker thread

VC

    Next

  • 1. SHFileOperation() & delete files
    I have this code: CString deleteAll = s_rootDir + "\\*.temp\0"; SHFILEOPSTRUCT shFileOp; ZeroMemory(&shFileOp, sizeof(SHFILEOPSTRUCT)); shFileOp.hwnd = NULL; shFileOp.wFunc = FO_DELETE; shFileOp.pFrom = deleteAll; shFileOp.pTo = NULL; shFileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT; SHFileOperation(&shFileOp); Well, it doesn't delete temp files any more. The strange thing is that it worked before and few days ago it stopped working. I don't know what I have changed in my code. :( This is how I create temp files: CFile f; BOOL ok = f.Open(fileName, CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyNone); I can delete files manually using Windows Explorer, so that means they are not "in use" by some app. Do I have to close file after using it? What will happen if I don't? Can I force above code to delete files, no matter what happen? (i.e. file is in use, it was not closed...)
  • 2. OnMouseMove VS OnNcHitTest
    Hi All, What's the difference betwen them? I tried to capture the mouse event of my new control class. If I derive from CButton, it can capture both. However if I derive it from CStatic, I can only capture OnNcHitTest, but not the first one. But I want to make a transparent box as button and thus I cant afford CButton. How can I solve it please? Thankyou!
  • 3. CIPAddressCtrl
    My edit box repeatedly displays "4" when it should be displaying an IP address like string. Why isn't my code working? Any suggestion welcome. void CIPGetAddressDlg::OnRunme() { // TODO: Add your control notification handler code here DWORD DwordAddress=0; char CharAddress[100]; CIPAddressCtrl *ipaddressctrl = (CIPAddressCtrl *) GetDlgItem(IDC_IPADDRESSCTRL); CEdit *edit = (CEdit *) GetDlgItem(IDC_EDIT); DwordAddress = ipaddressctrl->GetAddress(DwordAddress); sprintf(CharAddress,"%d",DwordAddress); edit->SetWindowText(CharAddress); } Thank you.
  • 4. very very very urgent(help)
    hello, I created a dialog based application.In that i created a popup dialog.This i created modal dialog by mistake.I called it from menu item of parent dialog...Now i wanted it to make modeless.So i delete the windows handler for the menu item;inserted a new handler and wote code for the modeless dialog.. the code is... midialog *d; d=new midialog; d->Create(IDD_DIALOG)//dialog created by me for the class midialog d->ShowWindow(SW_SHOW); is there any need to override the OnOk() and OnCancel() functions corresponding to buttons in the dialog.... coz when i run the programme each time it gives me some random error like an exception since memory couldnt be read,MFC42.dll exception.Also is overriding WM_CLOSE of main dialog necessary reply soon..
  • 5. calling OnDraw from a thread to do an animation
    I have OpenGl running in my MFC application, in a CView. What I'm concerned about is getting smooth animation. For this I need frames to be drawn at regular intervals. Hopefully at the refresh rate of the monitor or faster (at least 60fps, if not 100fps). My drawing code is plenty fast (<3 ms), but I'm not getting the frames drawn to the screen fast enough. I first tried calling Invalidate(FALSE) to update my CView. This apparently calls WM_PAINT messages, which get delayed until there are no other messages waiting. This gets me in the right ball park, but it's not smooth. There are obvious glitches in the animation. I'm running my frame update in it's own thread. Here's what it is now: void CFooView::FrameTriggerWorkFunction() { CFooDoc* pDoc = (CFooDoc*)GetDocument(); while ((pDoc != NULL) && !m_StopFrameTrigger) { Sleep(1000/FRAMERATE); pDoc->StepAnimationAllElements(); Invalidate(false); } } How can I redraw the screen without waiting for WM_PAINT messages? I've tried calling OnDraw instead of Invalidate, but I get an error at line 888 of wincore.cpp.

Delay inside a worker thread

Postby VHVsaW8 » Wed, 26 Jan 2005 22:53:05 GMT

Hi,

I need some help/guidance on improving an application that we have. This 
application controls an XY cutting table machine and does this by interfacing 
with a motion control board (the board manufacturer provide an C++ API ).
Since it's a cutting machine, some tools must be turned on/off during its 
operation. This is archived by setting bits in I/O ports calling a function 
API. This bits must be set on /off in a predetermined order and must be a 
delay between this callings (some tools have an pneumatic actuator and they 
take some time to get in position). The API doesn have a elay function
for this, so the program must do this.

The application has 3 threads, the main thread, a otion controlworker 
thread (MC_T) and a onitorworker thread that just read the I/O ports 
(MO_T). The I/O bits are set in the MC_T worker thread and the delay is 
performed using de Sleep MFC function. We choose to put the thread in sleep 
because it use no CPU cycles and it will give a delay at least as long as the 
programmed sleep time (I understand that due to scheduling in Windows the 
time that the MC_T thread will become active will be longer that the 
programmed sleep time). The delay values usually are between 50 to 1000 ms. 
I looking for ways to improve this response time inside my threads. In 
other words, if the defined value was 60ms for some port for example, the 
MC_T waits only 60ms before executing the next block of code. It desirable 
that the new approach doesn consume CPU cycles.

Thanks !


RE: Delay inside a worker thread

Postby andmort » Thu, 27 Jan 2005 00:44:09 GMT

Hi,
Which verson of windows are you using, and which compiler are you 
developing with?

Does the board manufacturer provide callbacks which you can register for 
i.e. tell me when the status of something is such and such, then you get a 
hardware interupt which is signalled to your callback function in real 
time? They may even provide hardware timers which you can program and 
register for notification.

Alternatively, you can use a multimedia timers
 http://www.**--****.com/ 
m/_win32_about_multimedia_timers.asp

To be honest though, it sounds like your hardware and driver set is a 
little bit lacking. You can turn a tool on, and you can turn it off, so why 
can't you turn it on for a specific period of time. This should be provided 
by the device driver, you should simply have to call an API with an On Off 
flag and a period of time. Alternatively, the driver should inform you when 
the operation has completed with a callback.

Andy Mortimer [MS]
Please do not send email directly to this alias. This alias is for 
newsgroup purposes only

This posting is provided "AS IS" with no warranties, and confers no rights. 
OR if you wish to include a script sample in your post please add "Use of 
included script samples are subject to the terms specified at 
 http://www.**--****.com/ " 




RE: Delay inside a worker thread

Postby VHVsaW8 » Thu, 27 Jan 2005 04:11:02 GMT

Hello again

Thanks for the help Andy.
I'm using Windows 2000 and Windows XP, Visual Studio .NET2003 and C++ with 
MFC (no .NET framework is used).

About the board and API I think that I wasn't very clear in my explanation. 
The problem is not to let tool on/off for some period of time. Is more like 
switch the tool on, wait x seconds then execute next block of C++ code. I 
really would appreciate if the manufacturer had provided a function that will 
wait xx seconds after the port was set, but that is not the case ... This 
board has 4 ports, each one has 7 bits. Setting the bits in a particular port 
turns that I/O line on/off, but the function expetcs to receive a bitmap of 
bits to set. So all bits on that port are set at once. 

I'm looking at that link and I think that CreateWaitableTimer can do the 
trick. Since we design that part of code in a more procedural way rather than 
event-drive I think it can be more easily adapted.

Many thaks !





Re: Delay inside a worker thread

Postby Ian Semmel » Thu, 27 Jan 2005 06:21:37 GMT

I think that what you have to do is ditch the worker thread and the sleep, and 
instead opt for UI threads.

You can have multiple layers of threads with the controlling thread looking 
after the timers and logical device state etc.

Create invisible windows and use PostMessage to communicate between the threads 
(you should logically be using PostThreadMessage, but because of a fundamental 
design flaw in Windows they are useless).





Re: Delay inside a worker thread

Postby Scott McPhillips [MVP] » Thu, 27 Jan 2005 07:53:23 GMT



Tulio: Have you tried the Sleep() solution?  If the PC is not heavily 
loaded with other tasks then you will find, on average, that the Sleep() 
duration is quite close to the requested interval.  You could also 
improve it by using SetThreadPriority in the sleeping thread.

Ian: I use PostThreadMessage heavily and find it to be utterly reliable. 
  It is, of course, necessary to respect the documented limitation that 
the destination thread cannot display windows or message boxes.  But 
this is acceptable and hardly a fundamental design flaw.  Why do you 
assert that PostThreadMessage is useless?

-- 
Scott McPhillips [VC++ MVP]


Re: Delay inside a worker thread

Postby Jerry Coffin » Thu, 27 Jan 2005 13:49:53 GMT

In article < XXXX@XXXXX.COM >, 
 XXXX@XXXXX.COM  says...

[ ... ]



The most obvious choice would be to use multimedia timers instead of 
Sleep. The system is written to go to extra lengths to assure quick 
response to multimedia timers.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.

Re: Delay inside a worker thread

Postby Ian Semmel » Fri, 28 Jan 2005 03:22:13 GMT









Because they don't always get through.

If you are using PostThreadMessage, try moving windows about the screen or 
something that generates a lot of messages and PTM's will be lost.
If you post to a window they are not.
In a thread, you can never be sure what other threads or the main thread is doing.
I noticed there is some discussion in the 'PostThreadMessage Function' in VC7 
help about how you work around this, but I have found it much more reliable to 
use PostMessage to windows.

Similar Threads:

1.Performing a delay inside a worker thread

Hi,

I need some help/guidance on improving an application that we have. This 
application controls an XY cutting table machine and does this by interfacing 
with a motion control board (the board manufacturer provide an C++ API ).
Since it's a cutting machine, some tools must be turned on/off during its 
operation. This is archived by setting bits in I/O ports calling a function 
API. This bits must be set on /off in a predetermined order and must be a 
delay between this callings (some tools have an pneumatic actuator and they 
take some time to get in position). The API doesn have a elay function
for this, so the program must do this.

The application has 3 threads, the main thread, a otion controlworker 
thread (MC_T) and a onitorworker thread that just read the I/O ports 
(MO_T). The I/O bits are set in the MC_T worker thread and the delay is 
performed using de Sleep MFC function. We choose to put the thread in sleep 
because it use no CPU cycles and it will give a delay at least as long as the 
programmed sleep time (I understand that due to scheduling in Windows the 
time that the MC_T thread will become active will be longer that the 
programmed sleep time). The delay values usually are between 50 to 1000 ms. 
I looking for ways to improve this response time inside my threads. In 
other words, if the defined value was 60ms for some port for example, the 
MC_T waits only 60ms before executing the next block of code. It desirable 
that the new approach doesn consume CPU cycles.

Thanks !

2.CWinThread derived class with a worker thread inside

3.Limiting the thread pool worker thread size

Hello,

I am trying to use the threadpool for database worker threads and since i 
run into database connection limits, i decided to limit the thread pool 
threads to a small number. I started out with 5 so i have couple of threads 
for book keeping and the remaining (3) threads for database work. I do queue 
more than 3 work items though. What i am seeing in this restricted setup is 
that once the 3 database threads are engaged, the threading deadlocks 
somehow.

Now i look back to threadpool's documentation and find that the threadpool 
is also used by timers on wait operations. I know there is minimal details 
here, but before i go into details does this setup raise some warning flags 
in your minds?


TIA
Priyesh 

4.application and progress dialog in new thread - no worker thread

Hi, I have written a program that takes on some operations much more
time than I expected. As I have seen users clicking wildly on the
screen to make something happen, I want to follow the microsoft
principles: always show them something ;)

First I made up a little status dialog containing a gif image to show
a little animation and a TextBox for a changing status message during
the work of the main program.
Just showing the dialog box (Form) at the start of the lengthy
operation does not work, as the dialog box is of course frozen because
it cannot work while the main message loop is stalled at the call of
the first method. Most solutions work with a non UI worker thread
which signals its status of the work progress to the UI thread. As my
main program is a UI program and all classes in there interact very
close it would be difficult for me to seperate a non UI thread for the
work. My idea was now to create a thread which shows the dialog and I
signal a new status message to the thread from my main UI thread
during the work, while disabling all inputs to the main window and
showing the status dialog topmost.

How would I do something like that?

I guess I just start the dialog by opening the dialog form with
Application.Run(new Statusdialog()) in the worker function of my
thread. But how do I access the dialog to signal new messages to it?
There is only one status dialog shown at a time so there is only one
instance of the dialog. I could create a static instance variable with
an access function in the dialog class to access the instance
following the singleton pattern. But from my experience with C++ this
is an unsafe way and I should use messages for the communication or
does C# have a synchronized keyword like Java to make such calls
thread safe?

Thanks for any help or suggestions,
Alexander

5.UI thread or worker thread?

If I instantiate a Form object on a worker thread:
1. Will that object be owned by the worker thread or the UI thread?
2. What will object.InvokeRequired return if called in the worker
thread?
3. Do I need to use Invoke to show the Form?

Thanks
Harsh

6. Notification from worker thread to Main thread

7. Calling a main thread method from a worker thread

8. apartments and worker threads (and the thread-neutral apartment)



Return to VC

 

Who is online

Users browsing this forum: No registered users and 67 guest