Delay inside a worker thread
by 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
by 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
by 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
by 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
by 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
by 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
by 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)