Your problem may be solved if you simply turn off the auto exposure setting and let exposure and gain be determined by settings you have defined manually. You could also try turning off the IR Emitter component that assists the IR Sensor with exposure.
Edit: if you want to try doing it in the RealSense Viewer, the bottom of the image below shows the options for the emitter and auto exposure that you can toggle.
Code for controlling the emitter on-off is here, under the 'Librealsense2' heading:
Auto-exposure can be disabled with the instruction RS2_OPTION_ENABLE_AUTO_EXPOSURE
The link below discusses turning off auto-exposure and defining manual settings.
Thank you. Yes it is working. It is not effected by turning On / Off the light.
I added following code at start of program;
rs2::device_list devices = ctx.query_devices();
int dev_id = 0;
rs2::device dev = devices[dev_id];
std::vector <rs2::sensor> sensors = dev.query_sensors();
auto depth_sensor = dev.first<rs2::depth_sensor>();
depth_sensor.set_option(RS2_OPTION_EMITTER_ENABLED, 1.f); // Enable laser emitter
I am only using the depth data and it is better than before.
Thanks once again.