By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,060 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

PROBLEM WITH THREADS - WIN 2003

P: n/a
Mr
Hi

We have a application with Framework 1.1 and Vs .NET 2003.
the application has a service with pooling; like a
File system Watcher, but works.

the system works fine in the development enviroment with
a performance very good; but production not.
-------------------------------------------------------------------------------------------

SAMPLE CODE
-------------------------------------------------------------------------------------------

using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System;
using System.ServiceProcess;
using System.Diagnostics;
using System.Configuration;
using System.Threading;
using System.Runtime.Remoting;
using System.IO;
using System.Data;
using LibreriasCIV.Proyectos;
using CLSutilidades.Utilidades;
using System.EnterpriseServices;

public class CIVmonitor : System.ServiceProcess.ServiceBase
{
private DateTime timeStart; //Used to note the start time of the service
private DateTime timeEnd; //Used to note the end time of the service
private TimeSpan timeElapsed = new TimeSpan(0); //' Initialize to 0;
// Used to calculate difference between timeEnd and TimeStart
private TimeSpan timeDifference;
private bool isPaused = false; // Notes whether the service is paused;
//ManualResetEvent b = new ManualResetEvent(false);
private bool INICIAR;
private DataSet DSproyectos ;
CLSutilidades.Utilidades.cUtilidadesSeguimiento OBJutil= new
CLSutilidades.Utilidades.cUtilidadesSeguimiento();//PARA TRABAJAR LOS ARCHIVOS
#region " Component Designer generated code ";

public CIVmonitor()
{
// This call is required by the Component Designer.
InitializeComponent();
// Add any initialization after the InitializeComponent() call
}
//UserService overrides dispose to clean up the component list.
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
// The main entry point for the process
static void Main()
{
System.Runtime.Remoting.RemotingConfiguration.Conf igure(AppDomain.CurrentDomain.SetupInformation.Con figurationFile);
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one NT Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
ServicesToRun = new System.ServiceProcess.ServiceBase[] {new CIVmonitor()};
System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
}

//Required by the Component Designer
private System.ComponentModel.IContainer components = null;
// NOTE: The following procedure is required by the Component Designer
// It can be modified using the Component Designer.
// Do not modify it using the code editor.
private void InitializeComponent()
{
//
// CIVmonitor
//
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "CIVmonitor";

}

#endregion
// OnContinue occurs when the user asks a paused Service to continue. In
order
// for this method to be called the CanPauseAndContinue property must be
// set to true. (It is false by default.)
protected override void OnContinue()
{
// Begin measuring the elapsed time. This means setting the
// timeStart back to the current time, and resetting isPaused
// to false
CrearTabla();
if (isPaused)
{
timeStart = DateTime.Now;
}
isPaused = false;
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"","CIVmonitor Reanudo OperaciĆ³n ","CIVMONITOR","ProcessDir");

}

// OnPause occurs when the user asks a running Service to pause. In order
// for this method to be called the CanPauseAndContinue property must be
// set to true. (It is false by default.)
protected override void OnPause()
{
timeEnd = DateTime.Now;
DSproyectos = null;

if (! isPaused)
{
timeDifference = timeEnd.Subtract(timeStart);
timeElapsed = timeElapsed.Add(timeDifference);
}
isPaused = true;

OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"","CIVmonitor Pausado ","CIVMONITOR","ProcessDir");
}

// OnShutdown occurs when the computer is powered off and the
// Service has not been stopped. In order for this method to be called
// the CanShutdown property must be set to true. (It is false by default.)
protected override void OnShutdown()
{
this.OnStop();
DSproyectos = null;
}

// OnStart is called whenever the service is started.
protected override void OnStart(string[] args)
{
System.Threading.Thread.CurrentThread.Name = "GESTOR INTEGRAL DE ARCHIVOS
CIV";
System.Threading.Thread.CurrentThread.Priority=
System.Threading.ThreadPriority.Highest;

timeElapsed = new TimeSpan(0);
// Initialize the Start time
timeStart = DateTime.Now;
isPaused = false;
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"","CIVmonitor Iniciado "+timeElapsed,"CIVMONITOR","ProcessDir");
INICIAR = true;
Debug.Write("INICIO");
CrearTabla(); //CREAR DATASET CON CADA UNO DE LOS PROYECTOS ACTIVOS, ESTOS
SON LOS QUE SE MONITOREARAN
Pickup (); //DISPARAR LA FUNCION QUE MONITOREARA ARCHIVOS
Debug.Write("INICIO");
}

// OnStop is called whenever the service is stopped.
protected override void OnStop()
{
timeEnd = DateTime.Now;
if (! isPaused)
{
timeDifference = timeEnd.Subtract(timeStart);
timeElapsed = timeElapsed.Add(timeDifference);
}
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"","CIVMONITOR DETENIDO "+timeDifference,"CIVMONITOR","ProcessDir");
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"","CIVmonitor Detenido por "+timeElapsed,"CIVMONITOR","ProcessDir");
DSproyectos=null; //LIMPIAR DS
INICIAR =false; //BANDERA A FALSE PARA QUE NO ITERE MAS
}

private void ProcessDir()
{
//
// Processes the files in that directory
//
try
{
while(INICIAR)
{
Debug.WriteLine(" ITERANDO ");
//ITERAR POR CADA UNO DE LOS ELEMENTOS DEL DATASET
if(DSproyectos.Tables.Count > 0 )
{
if(DSproyectos.Tables[0].Rows.Count > 0)
{
//foreach(DataRow dr in DSproyectos.Tables[0].Rows) // LOS ARCHIVOS SE
ALMACENAN UNO A UNO EN UN VECTOR
for(int index=0;index<DSproyectos.Tables[0].Rows.Count;index++)
{
//LLAMAR AL OBJETO QUE VIGILA CARPETAS
CLScontroladora.Controladora.cControl OBJcontroladora = new
CLScontroladora.Controladora.cControl(
DSproyectos.Tables[0].Rows[index].ItemArray[0].ToString(),
//CLInombre
DSproyectos.Tables[0].Rows[index].ItemArray[1].ToString(),
//PROnombre
DSproyectos.Tables[0].Rows[index].ItemArray[2].ToString(), //PROalias
DSproyectos.Tables[0].Rows[index].ItemArray[3].ToString(),
//PROentrada
DSproyectos.Tables[0].Rows[index].ItemArray[4].ToString(), //PROlog
DSproyectos.Tables[0].Rows[index].ItemArray[5].ToString(),
//PRObackup
DSproyectos.Tables[0].Rows[index].ItemArray[6].ToString(),
//PROsalida
DSproyectos.Tables[0].Rows[index].ItemArray[7].ToString(),
//PROextension
DSproyectos.Tables[0].Rows[index].ItemArray[8].ToString(), //PROftp
ConfigurationSettings.AppSettings.Get("Conexion"). ToString());
//CONEXION DB
OBJcontroladora.ClasificarData();
}
Thread.Sleep(int.Parse(ConfigurationSettings.AppSe ttings["Pausa"].ToString())); //PAUSA EN MS PARA QUE NO SE COMA EL SERVICIO LA MAQUINA
}
}

}
}
catch(Exception exp)
{
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"",exp.Message,"CIVMONI TOR","ProcessDir");
}
}

private void CrearTabla()
{
SqlConnection connection = null;// SqlConnection that will be used to
execute the sql commands
DSproyectos = new DataSet("Proyectos"); // SAVE PROYECS
try
{
try
{
Debug.WriteLine("INICIALIZANDO CADENA DE CONEXION");
connection = new
SqlConnection(ConfigurationSettings.AppSettings["Conexion"]);
}
catch
{
Debug.WriteLine("ERROR");
CLSutilidades.Utilidades.cUtilidadesSeguimiento OBJutil= new
CLSutilidades.Utilidades.cUtilidadesSeguimiento();//PARA TRABAJAR LOS ARCHIVOS
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"","NO SE PUDO CONECTAR A LA DB","CIVMONITOR","CrearTabla");
return;
}
DSproyectos = SqlHelper.ExecuteDataset(connection,
CommandType.StoredProcedure, "SpGetProyectos", new SqlParameter("@Estado",
"ACT") );
Debug.WriteLine("SE RECUPERO EL DATASET
"+DSproyectos.Tables[0].Rows.Count);
}
catch(Exception exp)
{
string errMessage = "";
for( Exception tempException = exp; tempException != null ; tempException
= tempException.InnerException )
{
errMessage += tempException.Message + Environment.NewLine +
Environment.NewLine;
}
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"","ERROR
AL TRATAR DE CONSULTAR INFORMACION DE CLIENTES/PROYECTOS EN LA DATABASE -
"+exp.Message,"CIVMONITOR","ProcessDir");
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"",exp.Message,"CIVMONI TOR","ProcessDir");
}
finally
{
if(connection != null)
connection.Dispose();
}

}
private void Pickup ()
{
try
{
System.Threading.Thread.CurrentThread.Name = "Monitor CIV";
Thread objThread= new Thread(new ThreadStart(ProcessDir));
objThread.Name = "Gestor Directorios";
objThread.Priority = System.Threading.ThreadPriority.Highest;
objThread.Start();
Debug.WriteLine("PASO POR PICKUP ");
}

catch(ThreadAbortException exp)
{
OBJutil.WriteLOG(ConfigurationSettings.AppSettings .Get("LOGpath").ToString(),"",exp.Message,"CIVMONI TOR","ProcessDir");

}

}

}

We changed machine.config and nothing.

The performance in production is very slow, the system is WIN 2003.
DEVELOPMENT ENVIROMENT: WIN2000 - SQL SERVER 2000

PRODUCTION ENVIROMENT: WIN2003 - SQL SERVER 2000

What can i do?

Ideas

The application has a service with MultiThreading. For each task
starts a Thread.
Feb 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

It's not a good idea to post your entire code, nobody will read it
completely ( most of the us people are in the office :) )

Which is the slow part?

Did you check if you may have any sync problems, like two threads
reading/writing the same variable?

what if you try to pinpoint the problem, remove as much code as possible
until you get the method where the performance problem is.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 14 '06 #2

P: n/a
Mr
thanks

The threads in the server works very slow.

the production server has Win 2003 with SQL 2000.

The problem is; We need more priority for the execution of Threads, because
the process has only a few of CPU.
The program works fine, but very slow.
We need more priority.
ideas?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

It's not a good idea to post your entire code, nobody will read it
completely ( most of the us people are in the office :) )

Which is the slow part?

Did you check if you may have any sync problems, like two threads
reading/writing the same variable?

what if you try to pinpoint the problem, remove as much code as possible
until you get the method where the performance problem is.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Feb 14 '06 #3

P: n/a
You need to profile the code to see where the slow parts are. Since
the service is reading/writing data to a database I suspect that will
be your bottleneck. Changing the priority of the worker will have
little effect especially if the thread is speading most of its time in
an idle state waiting for the database IO to complete.

If you can post a short, but complete program that demonstrates the
slowness you're seeing we can help. Often times, in the process of
attempting to provide that short program, you'll discover the problem
yourself.

Also, like Ignacio, when all else fails use the process of
trial-and-error by removing and adding back code until you find the
performance problem.

Brian

Mr wrote:
thanks

The threads in the server works very slow.

the production server has Win 2003 with SQL 2000.

The problem is; We need more priority for the execution of Threads, because
the process has only a few of CPU.
The program works fine, but very slow.
We need more priority.
ideas?

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

It's not a good idea to post your entire code, nobody will read it
completely ( most of the us people are in the office :) )

Which is the slow part?

Did you check if you may have any sync problems, like two threads
reading/writing the same variable?

what if you try to pinpoint the problem, remove as much code as possible
until you get the method where the performance problem is.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Feb 14 '06 #4

P: n/a
Mr <Mr@discussions.microsoft.com> wrote:
We have a application with Framework 1.1 and Vs .NET 2003.
the application has a service with pooling; like a
File system Watcher, but works.

the system works fine in the development enviroment with
a performance very good; but production not.


<snip>

The first thing I'd do is try to reproduce the problem in a console
application. That would be a lot shorter to post, and a lot easier to
debug too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 14 '06 #5

P: n/a
To add what the others, said. It's wrong to set the priority of a thread
above normal, espcially in a service, it won't make your code run faster
anyway. In you thread procedure (ProcessDir) you run an endless loop, which
does't really shows what you are actually doing (the ClasificarData is
missing) except Sleeping for a while! Now, if this is what you call slow,
why did you include the Sleep call, and what amount of time are you putting
this (high priority!) thread asleep?

Willy.

"Mr" <Mr@discussions.microsoft.com> wrote in message
news:D1**********************************@microsof t.com...
| thanks
|
| The threads in the server works very slow.
|
| the production server has Win 2003 with SQL 2000.
|
| The problem is; We need more priority for the execution of Threads,
because
| the process has only a few of CPU.
| The program works fine, but very slow.
| We need more priority.
| ideas?
|
| "Ignacio Machin ( .NET/ C# MVP )" wrote:
|
| > Hi,
| >
| > It's not a good idea to post your entire code, nobody will read it
| > completely ( most of the us people are in the office :) )
| >
| > Which is the slow part?
| >
| > Did you check if you may have any sync problems, like two threads
| > reading/writing the same variable?
| >
| > what if you try to pinpoint the problem, remove as much code as possible
| > until you get the method where the performance problem is.
| >
| >
| >
| > --
| > Ignacio Machin,
| > ignacio.machin AT dot.state.fl.us
| > Florida Department Of Transportation
| >
| >
| >
Feb 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.