How to block a drawnow from executing callbacks?
7 次查看(过去 30 天)
显示 更早的评论
Mathworks has recklessly put a drawnow inside of its asynciolib for hardware read and writes:
toolbox\shared\asynciolib\+matlabshared\+asyncio\+internal\Stream.m Line 184: drawnow('limitrate');
This will then execute callbacks in the middle of data transfer! I could remove those lines of code for every Matlab install, but if an update restores the code then I have a dangerous bug. Is there a way I can temporarily pause the event queue from executing callbacks?
The following function CallbackTest has a 10 second main loop which should not be interrupted with the press of a button. However if TCPIP commands are executed in the loop then a hidden drawnow is invoked and the button callback will be processed.
function CallbackTest(Do_TCPIP)
% Test to see if TCPIP reads evaluate callbacks.
% There is a 10s main loop which the button callback should not interrupt.
% The main loop will not be interrupted when Do_TCPIP is 0.
% However when Do_TCPIP is 1 then it will be interrupted.
%Initialize figure and button
hFigure=uifigure();
uibutton(hFigure,ButtonPushedFcn=@ACallBack);
%Initialize TCPIP
echotcpip("on",4000);
t = tcpclient("localhost",4000);
configureTerminator(t,"CR");
waitfor(hFigure,'FigureViewReady')
%10 second main loop which should not be interrupted because there is no drawnow, figure, pause, or waitfor command
n=0;
while n < 20
if Do_TCPIP
writeline(t,"loop");
readline(t);
end
n=n+1;
java.lang.Thread.sleep(500);
end
% Wrap up
echotcpip("off");
delete(hFigure)
fprintf('Done!\n');
end
function ACallBack(~,~)
fprintf('Main loop interrupted.\n');
end
0 个评论
回答(1 个)
Nivedita
2024-6-6
Hey Brandon,
You can try using the following approach to block drawnow from executing callbacks:
I have introduced a global flag ALLOW_CALLBACKS to control whether the callback function should proceed with its intended action or simply return without doing anything. This way you can disable the callback during critical sections of the code programmatically. Here is a dummy example code:
function CallbackTest(Do_TCPIP)
% Initialize control flag
global ALLOW_CALLBACKS;
ALLOW_CALLBACKS = true;
% Initialize the GUI
hFigure = uifigure('Name', 'Callback Control Test');
uibutton(hFigure,ButtonPushedFcn=@ACallBack);
% Initialize TCP/IP setup if requested
if Do_TCPIP
echotcpip("on", 4000); % Enable echo server for testing
t = tcpclient("localhost", 4000);
end
% Critical section where callbacks should not interrupt
ALLOW_CALLBACKS = false;
for n = 1:20 % Example critical loop
fprintf('Critical operation in progress: Iteration %d\n', n);
if Do_TCPIP
% Simulate a critical TCP/IP operation
writeline(t, "This is a critical message.");
response = readline(t); % Assuming echo server, expect immediate response
fprintf('TCP/IP Echo Response: %s\n', response);
end
pause(1); % Simulate time-consuming operation
end
%Callbacks re-enabled
ALLOW_CALLBACKS = true;
% Wrap up...
if Do_TCPIP
clear t; % Close and clear the TCP/IP connection
echotcpip('off'); % Disable echo server
end
delete(hFigure); % Close the GUI
fprintf('Critical section completed. Callbacks re-enabled.\n');
end
function ACallBack(~,~)
global ALLOW_CALLBACKS;
if ~ALLOW_CALLBACKS
fprintf('Callback execution is currently disabled.\n');
return;
end
% Callback code that should be executed when allowed
fprintf('Callback executed: Main loop potentially interrupted.\n');
end
I hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interface-Based Instrument Communication 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!