Is it possible to use Hangfire for Windows Applications?


#1

I use Hangfire on my Webservers already, so I am familar with the basics of Hangfire.

I am wondering If Hangfire will also work in a pure Windows Forms environment? I am talking about using Hangfire in a Windows Application to execute Background Jobs. At the moment, all my background jobs are performed on different threads in my windows application. But what happens if the application or pc crashes or is shutting down during processing? All my jobs are canceled and no one will see. But hangfire will retry those jobs after application restart and in future, I may create a Windows Service which executes jobs, triggered by other Windows applications.

So my Question is: Has anyone ever tried to use Hangfire this way? Or is this a stupid idea?


#2

@awpross, what background jobs do you want to run in your application? And what your application looks like?


#3

Using Hangfire for desktop applications seems counterintuitive to the structure of the operating system.


#4

most commonly this would be things like updating database entries, sending mails and other long running tasks which should be running under the user account who triggered the job.

At present, I use background tasks but if the app is closed before the tasks are completed, they won’t be triggered after app restart. Maybe I will include my own resumeable function, but If hangfire can do it, it would be faster to implememt.


#5

If your application is a server one that is placed near the database, why not? It is not tied to any application-level framework. But for client applications that are being installed on user machines it is definitely overkill.


#6

Long Time ago, I started this thread. And now, 3 years later, I have my first implementation of Hangfire inside my client Application.
As documentation of Hangfire changed over years, I was able to integrate it easylie. Now, I have a windows-service for hosting hangfire server, a client application, also hosting a local version of hangfire and even a Dashboard implementation using owin. Now my dreams have become true :wink:


#7

I hope you are still watching this thread. Please contact me, I need to do the same but everything is web based… I can pay you for your time and/or code.

-DC


#8

On a windows server use Hangfire to have a single repository that replaces the Windows scheduled tasks. You can either use a windows service, or windows console, or windows form application as the server.

There are 2 things you need: 1: BackgroundJobServer to do the work, and 2: the Hangfire Dashboard to monitor it.

The dashboard is OWIN http hosted. If you user a windows service or console application to host the BackgroundJobServer you would access the dashboard via a browser window.

If you user a windows form application to host the BackgroundJobServer you can use a windows browser control in the form to view the dashboard from within the same application.

The BackgroundJobServer and dashboard need t be disposed when the form closes.

public partial class taskForm : Form
{
private BackgroundJobServer _server;
private IDisposable _dashServer;

public taskForm()
{
    InitializeComponent();
    InitializeOwin();
    this.WebBrowser1.NavigateAndWait("http://localhost:9095/hangfire");
}

private void InitializeOwin()
{
    GlobalConfiguration.Configuration.UseSqlServerStorage(connectionString.getSchedulerCs().ConnectionString);
    _server = new BackgroundJobServer();

    using (var connection = JobStorage.Current.GetConnection())
    {
        foreach (var recurringJob in connection.GetRecurringJobs())
        {
            RecurringJob.RemoveIfExists(recurringJob.Id);
        }
    }

    RecurringJob.AddOrUpdate("Monitor Polling", () => tasks._monitor_Clients(), Cron.MinuteInterval(15));
    RecurringJob.AddOrUpdate("Daily Averages", () => tasks._daily_averages(), Cron.Daily(0, 1));

    StartOptions options = new StartOptions();
    options.Urls.Add("http://localhost:9095");
    options.Urls.Add("http://127.0.0.1:9095");
    options.Urls.Add(string.Format("http://{0}:9095", Environment.MachineName));
    _dashServer = WebApp.Start<OwinStartup>(options);
}

private void taskForm_Closing(object sender, EventArgs e)
{
    _server.Dispose();
    _dashServer.Dispose();
}

}

public class OwinStartup
{
public void Configuration(IAppBuilder app)
{
var options = new DashboardOptions { AppPath = null };
app.UseHangfireDashboard("/hangfire", options);
}
}