backgroundworker in windows service or console application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it true that a backgroundworker will only work in winforms applications,
and not in console applications and/or windows services ?

If have an object (Task) that instantiates different backgroundworker
instances. If I start the task object from a windows form, it works fine. If
I do this from a windows service, it will start the Task object, but will
never fire my real object.

Any help would be nice.
 
Although I haven't tried different environments, I don't see a reason why
this would be true. The backgroundworker is a wrapper for threads - threads
work everywhere. Now you may have some issues with webforms due to the
timing of the long running tasks and the availability of the context object
however that is not related to it functioning correctly. I'll check tonight
to make sure - there's always a chance that I'm wrong.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
Michel Meex said:
Is it true that a backgroundworker will only work in winforms
applications,
and not in console applications and/or windows services ?

If have an object (Task) that instantiates different backgroundworker
instances. If I start the task object from a windows form, it works fine.
If
I do this from a windows service, it will start the Task object, but will
never fire my real object.

Any help would be nice.

The BackgroundWorker cannot be used from windows services, it is designed
to be used in combination with a UI thread, that is a thread that has a
message queue, windows services don't have a UI thread or a thread that
pumps the message queue.

Willy.
 
Willy,

Thanks for your answer. It's not documented however. Do you know if there's
a simple way to achieve the same functionality in a windows service ?

Michel
 
true, true. i forgot that part.
Thanks for your answer. It's not documented however. Do you know if there's
a simple way to achieve the same functionality in a windows service ?
You can use a simple thread to accomplish this task.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
Michel,

If you are looking for the exact same functionality, stop looking, Windows
Services should not have UI threads, so there is no need to "Post" messages
to the UI thread's message loop.
However, if you are looking for a way to run a short running function on a
separate thread, you'll have to look at the ThreadPool class, for functions
that run for a long period (say > 2-5 seconds) you better use the Thread
class.

Willy.
 
Thanks for your answer. I wasn't aware of the fact that the backgroundworker
was only ment for UI purposes. In my case, I only need to start processes for
a short time at a scheduled moment. So I am going to implement the Thread
class like I did in the past. It would have been very nice if there was some
backgroundworker functionality for non-ui purposes. Saves me some development
time.

Regards,
Michel
 
It works fine in a console or service app with no forms. The delegates are
run on thread pool threads instead of the UI thread. So there is nothing
special going on, just delegates, BeginInvoke, and events. Try it yourself.
 
Back
Top