Jobs Running Multiple Times

I am using HangFire like this, just to test it out,

BackgroundJob.Enqueue(() => Process.Start(“calc”));

Anyway I have realised that this cannot work, but I have noted a weird behaviour that I am curious about.

When you do the above the job just keeps running over and over again until you force it to stop. The screenshot below is a partial screenshot. It started calc up 13 times before I could stop it. Why is that?

This is because Process.Start returns an instance of the Process class that can’t be serialized, as we can see from the log messages produced:

Please don’t enqueue Process.Start method calls, use a wrapper method, see the following code snippet for example.

public static void StartProcess(string fileName)
    var process = Process.Start(fileName);
    if (process == null)
        throw new InvalidOperationException("No process resource is started (for example, if an existing process is reused).");


I will think how to move background jobs to the Failed state instead of re-running them again and again in cases like yours.

After fixing your background jobs, ensure you have no background jobs based on previous Process.Start method call in your database.

Thanks. We have since done what you suggested and put this in a wrapper method, and created our database fresh. I wasn’t expecting the job to keep repeating so I appreciate the explanation.

I have also found the logs now which I hadn’t previously which helps a lot.

Since version 1.5.0 (just released), Hangfire tries to serialize the result, and in case of an exception it simply writes “Can’t serialize the return value” message instead of the result. So serialization issues don’t lead to infinite processing loops now.