The reason you are getting this error message is because you are trying to update a GUI control from a different thread than the main which is not possible. GUI controls should always be modified on the same thread which was used to create them.
You could use Invoke if InvokeRequired which will marshal the call to the main thread. Here's a nice article.
But probably the easiest solution would be to simply use a BackgroundWorker because this way you no longer need to manual marshal the calls to the GUI thread. This is done automatically:
var worker = new BackgroundWorker();
worker.DoWork += (sender, e) =>
{
// call the XYZ function
e.Result = XYZ();
};
worker.RunWorkerCompleted += (sender, e) =>
{
// use the result of the XYZ function:
var result = e.Result;
// Here you can safely manipulate the GUI controls
};
worker.RunWorkerAsync();