简体   繁体   中英

How to use the windows waitfor utility

Windows 7 and later include a utility called "waitfor.exe". The program is supposed to listen and wait for a named signal to be received. It also can send a signal.

The syntax for waiting for a signal is something like: waitfor.exe somevent

The syntax for sending is supposed to be: waitfor.exe /S remotecomputer /SI somevent . There are also options for specifying a user and password to connect to a remote computer.

But I am having trouble getting this to work. The program appears to work okay when waiting for a signal. But I have never been able to get the program to work in send mode.

I've tried these possibilities:

  • waitfor /si someevent - produces error - ERROR: Unable to send signal 'someevent'.
  • waitfor /s someevent - syntax error
  • waitfor /s %COMPUTERNAME% someevent - syntax error
  • waitfor /s %COMPUTERNAME% /si someevent - produces error - ERROR: Unable to send signal 'someevent'.

I want to use this program to simply synchronize between two command windows, running as the same user. Neither is running elevated. Testing in this scenario produces the above errors.

I know I can find (or write) 3rd party utilities to do this, but I would like to use a windows built-in utility. I also would like to avoid using powershell, as its performance is poor, for my use case. Also also also, I know I can cobble together a synchronization mechanism using "if exist" on the receive side, plus creating a file on the send side (ditto for the registry, using reg.exe), but I'd like to avoid that also. The latency of such solutions is inversely proportional to performance (the receive side would need to use a polling loop).

Does anyone know how to make waitfor.exe work for this simple same-computer same-user scenario ? Or any other built-in with good performance and zero latency?


exact detail of error messages:

C:\>where waitfor
C:\Windows\System32\waitfor.exe

C:\>rem - did run "waitfor myevent" in another window

C:\>waitfor /s %COMPUTERNAME% /si myevent
ERROR: Unable to send signal 'myevent'.

C:\>waitfor /s %COMPUTERNAME% myevent
ERROR: Invalid syntax.
Type "WAITFOR /?" for usage.

C:\>

My computer is running Windows 2008 R2 server, and is part of a corporate active directory domain.


It seems like something is not right on your system. This works fine for me, using normal command prompt windows (not running with Administrative rights).

I opened a couple of command windows on my laptop. In the first, I typed "waitfor somevent". It commenced to wait. In the second, I typed "waitfor /si somevent" which gave a message "SUCCESS: Signal sent" and the waitfor command in the first command window then terminated.

I tried a second time, doing the same thing in the first window (waitfor somevent). In the second window I typed "waitfor /s ots-jburd-ltOTS-JBURD-LT /si somevent" (where ots-jburd-lt is the name of my laptop), and again, the waitfor in the first window terminated as expected.

I logged into my home network and ran some quick tests there, and everything worked as expected (sending 'somevent' from one computer to another).

My tests show that your syntax is correct. You report that a simple "waitfor /si someevent" fails with the message "ERROR: Unable to send signal 'someevent'". This does not happen on my machines. (And the posted question is on how to use the waitfor command.)

I'm not sure what can prevent a signal from being sent. Most likely a firewall setting? But my laptop is running on a public network right now, and all my defenses are at 100%, and it works on my laptop with no problems. Maybe you have some other 3rd-party anti-malware running which prevents signals from being sent?

This is a simple code that implements your example:

@echo off
setlocal
if "%1" neq "" goto %1


rem Start a Send - Receive pair
start "" /B "%~F0" Send ^> pipefile.txt
call "%~F0" Receive < pipefile.txt
echo Program ending.
goto :EOF


:Send
set "line="
set /P "line=Enter a line, EXIT to end: " > CON
echo %line%
waitfor /SI dataAvailable > NUL
if /I "%line%" neq "EXIT" goto Send
echo Send part ending... > CON
exit /B


:Receive
waitfor dataAvailable > NUL
set /P "data="
echo Data received: "%data%"
if /I "%data%" neq "EXIT" goto Receive
echo Receive part ending...
exit /B

This is the output:

C:\Users\Antonio\Documents\test> test.bat
Enter a line, EXIT to end: Line one
Data received: "Line one"
Enter a line, EXIT to end: Second line sent to receive part
Data received: "Second line sent to receive part"
Enter a line, EXIT to end: exit
Data received: "exit"
Receive part ending...
Program ending.

C:\Users\Antonio\Documents\test> Send part ending...

Windows 8.1 here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM