Application.Run and Windows Service (Multithreaded)


J

Jake K

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();
}
}
}
 
Ad

Advertisements

M

Mattias Sjögren

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
 
D

DeveloperX

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.
 
Ad

Advertisements

D

DeveloperX

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.
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 said:
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();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top