We discuss a method that allows to compute the thermodynamic Casimir force at a given temperature in lattice models by performing a single Monte Carlo simulation. It is analogous to the one used by de Forcrand and Noth and de Forcrand, Lucini and Vettorazzo in the study of 't Hooft loops and the interface tension in SU(N) lattice gauge models in four dimensions. We test the method at the example of thin films in the XY universality class. In particular we simulate the improved two-component phi^4 model on the simple cubic lattice. This allows us to compare with our previous study, where we have computed the Casimir force by numerically integrating energy densities over the inverse temperature.