Error on AutomaticUpdaterBackend.ForceCheckForUpdate() when running as a service.

I'm trying to get automatic updates to work on my newly created windows service.So far I got the checking for updates to work as long as I'm running the program in the Visual Studio environment.As soon as I try to run it as a service I get the following exception:

A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dllAdditional information: Error creating window handle.

Here is the stack trace: at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp) at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.Application.MarshalingControl..ctor() at System.Windows.Forms.Application.ThreadContext.get_MarshalingControl() at System.Windows.Forms.WindowsFormsSynchronizationContext..ctor() at System.Windows.Forms.WindowsFormsSynchronizationContext.InstallIfNeeded() at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at wyDay.Controls.UpdateHelper.RecreateBackgroundWorker() at wyDay.Controls.UpdateHelper.SendAsync(UpdateHelperData uhd) at wyDay.Controls.AutomaticUpdaterBackend.ForceCheckForUpdate(Boolean recheck)

Seems like something is trying to create an instance of a form, which fails because of the lack of UI?

This is my source code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading.Tasks;using wyDay.Controls;

namespace GummiData{ public partial class Service1 : ServiceBase { public static AutomaticUpdaterBackend auBackend;

public Service1() { InitializeComponent(); }

[Conditional("DEBUG")] static void DebugMode() { if (!Debugger.IsAttached) Debugger.Launch();

Debugger.Break(); }

protected override void OnStop() {

}

public void OnDebug() { OnStart(null); }

protected override void OnStart(string[] args) { DebugMode(); InitializeAutomaticUpdater();

if (!auBackend.ClosingForInstall) { CheckForUpdates(); //InitializeQuartz(); } }

private void CheckForUpdates() { auBackend.ForceCheckForUpdate(); }

private void InitializeAutomaticUpdater() { auBackend = new AutomaticUpdaterBackend { //TODO: set a unique string. // For instance, "appname-companyname" GUID = "a-string-that-uniquely-IDs-your-service", UpdateType = UpdateType.Automatic, ServiceName = this.ServiceName };

auBackend.ReadyToBeInstalled += auBackend_ReadyToBeInstalled; auBackend.UpToDate += auBackend_UpToDate; auBackend.CheckingFailed += auBackend_CheckingFailed; auBackend.DownloadingFailed += auBackend_DownloadingFailed; auBackend.ExtractingFailed += auBackend_ExtractingFailed; auBackend.UpdateFailed += auBackend_UpdateFailed;

// Initialize() and AppLoaded() must be called after events have been set. // Note: If there's a pending update to be installed, wyUpdate will be // started, then it will talk back and say "ready to install, // you can close now" at which point your app will be closed. auBackend.Initialize(); auBackend.AppLoaded(); }

static void auBackend_UpdateFailed(object sender, FailArgs e) { Trace.WriteLine("Update Failed"); Trace.WriteLine(e.ErrorTitle); Trace.WriteLine(e.ErrorMessage); }

static void auBackend_ExtractingFailed(object sender, FailArgs e) { Trace.WriteLine("Extracting Failed"); Trace.WriteLine(e.ErrorTitle); Trace.WriteLine(e.ErrorMessage); }

static void auBackend_DownloadingFailed(object sender, FailArgs e) { Trace.WriteLine("Downloading Failed"); Trace.WriteLine(e.ErrorTitle); Trace.WriteLine(e.ErrorMessage); }

static void auBackend_CheckingFailed(object sender, FailArgs e) { Trace.WriteLine("Checking Failed"); Trace.WriteLine(e.ErrorTitle); Trace.WriteLine(e.ErrorMessage); }

static void auBackend_ReadyToBeInstalled(object sender, EventArgs e) { // ReadyToBeInstalled event is called when // either the UpdateStepOn == UpdateDownloaded or UpdateReadyToInstall

if (auBackend.UpdateStepOn == UpdateStepOn.UpdateReadyToInstall) { auBackend.InstallNow(); } }

static void auBackend_UpToDate(object sender, SuccessArgs e) { System.Diagnostics.Trace.WriteLine("Up To Date"); } }}

Hope you can help me sort this out.

Thanks in advance!

I don't know. We can't reproduce it here (but I haven't tried your code). I've skimmed your code and nothing obvious is popping out. Have you tried our example service in the AutomaticUpdater source code?

I have now done some more testing.In my project I replaced the reference to AutomaticUpdater.dll with the source code.That gave me to ability to find exactly were the error occurred.

In the UpdateHelper.cs file:void RecreateBackgroundWorker() { // Fixes windows service bugs: https://wyday.com/forum/t/995/huge-problems-with-automaticupdatebackend/ Application.DoEvents(); // <<<<====== This throws the error. bw = new BackgroundWorker(); bw.DoWork += bw_DoWork; bw.RunWorkerCompleted += bw_RunWorkerCompleted; }

When I removed Application.DoEvents(), it worked, well almost.Now wyUpdate.exe throws the same error when UpdateHelper tries to start the process:

System.ComponentModel.Win32Exception was unhandledMessage: An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Windows.Forms.dllAdditional information: Error creating window handle.

Without the source code to wyUpdate.exe I cant debug any deeper :/

I have also tried the example project in the source code with the same result.I'm running Windows 8.0

Any suggestions?