PC Review


Reply
Thread Tools Rate Thread

Application.Run and Windows Service (Multithreaded)

 
 
Jake K
Guest
Posts: n/a
 
      5th Jan 2007
I have a multithreaded application that I now want to convert into a Windows
Service. Does application.run work in a windows service? Are there things
to take into consideration when creating a multithreaded windows service as
opposed to a multithreaded windows forms application?


E.G.

namespace whatever
{
public partial class Form1 : Form
{
....
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
SomeArray = new SomeClass[# - #];

for (...)
{
...
...
ThreadStart ts = new ThreadStart(SomeClass.SomeMethod);
Thread wrkThread = new Thread(ts);
whatever.CurrentThread = wrkThread;
wrkThread.SetApartmentState(ApartmentState.STA);
wrkThread.Start();
}
return;

}
}
public class SomeClass
{
// ...
// ...
private void SomeMethod()
{
....
....
Application.Run();
}
}
}

 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      5th Jan 2007
>I have a multithreaded application that I now want to convert into a Windows
>Service. Does application.run work in a windows service?


Why are you considering calling it? Windows services don't have UI so
there's probably no point in doing so.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      5th Jan 2007
No is the short answer, the main class inherits from
System.ServiceProcess.ServiceBase and ServiceBase has a Run method.
The easiest way to see how it works is to create a service from the
Windows Service template in VS, if you don't have that, I can knock you
up an empty service that shows the basics.

Jake K wrote:
> I have a multithreaded application that I now want to convert into a Windows
> Service. Does application.run work in a windows service? Are there things
> to take into consideration when creating a multithreaded windows service as
> opposed to a multithreaded windows forms application?
>
>
> E.G.
>
> namespace whatever
> {
> public partial class Form1 : Form
> {
> ...
> public Form1()
> {
> InitializeComponent();
> }
>
> private void Form1_Load(object sender, EventArgs e)
> {
> SomeArray = new SomeClass[# - #];
>
> for (...)
> {
> ...
> ...
> ThreadStart ts = new ThreadStart(SomeClass.SomeMethod);
> Thread wrkThread = new Thread(ts);
> whatever.CurrentThread = wrkThread;
> wrkThread.SetApartmentState(ApartmentState.STA);
> wrkThread.Start();
> }
> return;
>
> }
> }
> public class SomeClass
> {
> // ...
> // ...
> private void SomeMethod()
> {
> ...
> ...
> Application.Run();
> }
> }
> }


 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      5th Jan 2007
Oh I should add, threading is simpler in a service as there's no GUI so
no BeginInvoke calls, however you do need to manage the process of the
servicebase receiving a Stop message. I've got a service or two here
which are multithreaded, I use ManualResetEvents to manage the thread
shutdown.

DeveloperX wrote:
> No is the short answer, the main class inherits from
> System.ServiceProcess.ServiceBase and ServiceBase has a Run method.
> The easiest way to see how it works is to create a service from the
> Windows Service template in VS, if you don't have that, I can knock you
> up an empty service that shows the basics.
>
> Jake K wrote:
> > I have a multithreaded application that I now want to convert into a Windows
> > Service. Does application.run work in a windows service? Are there things
> > to take into consideration when creating a multithreaded windows service as
> > opposed to a multithreaded windows forms application?
> >
> >
> > E.G.
> >
> > namespace whatever
> > {
> > public partial class Form1 : Form
> > {
> > ...
> > public Form1()
> > {
> > InitializeComponent();
> > }
> >
> > private void Form1_Load(object sender, EventArgs e)
> > {
> > SomeArray = new SomeClass[# - #];
> >
> > for (...)
> > {
> > ...
> > ...
> > ThreadStart ts = new ThreadStart(SomeClass.SomeMethod);
> > Thread wrkThread = new Thread(ts);
> > whatever.CurrentThread = wrkThread;
> > wrkThread.SetApartmentState(ApartmentState.STA);
> > wrkThread.Start();
> > }
> > return;
> >
> > }
> > }
> > public class SomeClass
> > {
> > // ...
> > // ...
> > private void SomeMethod()
> > {
> > ...
> > ...
> > Application.Run();
> > }
> > }
> > }


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Every ASP.NET application - a multithreaded windows app? Pavils Jurjans Microsoft ASP .NET 7 2nd Jun 2004 06:07 PM
Multithreaded System Service Sonya Erb Microsoft VB .NET 3 17th Feb 2004 05:12 PM
events in multithreaded application Christopher Kimbell Microsoft Dot NET Framework Forms 6 21st Jan 2004 10:56 PM
multithreaded Windows Service -- OnStop is impatient, kills my threads n_o_s_p_a__m Microsoft C# .NET 0 10th Oct 2003 09:10 PM
multithreaded windows service? threadstart doesn't get called n_o_s_p_a__m Microsoft C# .NET 2 8th Oct 2003 09:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:08 PM.