threading question

21/12/2004 - 10:24 por CK | Informe spam
I have the following code in a windows service, when I start the windows
service process1 and process2 work fine , but final process (3) doesnt get
called. i stop and restart the windows service and the final process(3) gets
called. what am I doing wrong with the threading? by the way
Directory.GetFiles(IncomingXMLPath1).Length is some global outcome from
process 1.

Thanks

1)
m_threadNewClaimInstitutional = new Thread(new
System.Threading.ThreadStart(process1));

m_threadNewClaimInstitutional.Start();

2)

m_ThreadNewClaimProfessional= new Thread(new
System.Threading.ThreadStart(process2));

m_ThreadNewClaimProfessional.Start();

//when all the files have been processed in both the process1 and process2
directories

//then is the final process

#)final

if(Directory.GetFiles(IncomingXMLPath1).Length==0 &&
Directory.GetFiles(IncomingXMLPath2).Length==0)

{

m_ThreadFinall = new Thread(new System.Threading.ThreadStart(process3));

m_ThreadFinall.Start();

}

Preguntas similare

Leer las respuestas

#1 A.Poblacion
21/12/2004 - 09:13 | Informe spam
I don't think you are doing anything wrong with the threading itself.
Rather, it looks like you aren't waiting properly for the completion of
processes 1) and 2) before going into the final step. Therefore, it checks
for wether the directories are empty, finds that they aren't (because
processes 1 and 2 haven't finished), and skips the process3. When you start
the service again, there has been enough time to compete precesses 1 and 2,
the directories have been emptied and the check succeeds, enabling process3
to run.

You should post the missing piece of code (where you check for completion of
1 and 2) for us to take a look and try to guess why it is not working
properly.


"CK" wrote in message
news:%
I have the following code in a windows service, when I start the windows
service process1 and process2 work fine , but final process (3) doesnt


get
called. i stop and restart the windows service and the final process(3)


gets
called. what am I doing wrong with the threading? by the way
Directory.GetFiles(IncomingXMLPath1).Length is some global outcome from
process 1.

Thanks

1)
m_threadNewClaimInstitutional = new Thread(new
System.Threading.ThreadStart(process1));

m_threadNewClaimInstitutional.Start();

2)

m_ThreadNewClaimProfessional= new Thread(new
System.Threading.ThreadStart(process2));

m_ThreadNewClaimProfessional.Start();

//when all the files have been processed in both the process1 and process2
directories

//then is the final process

#)final

if(Directory.GetFiles(IncomingXMLPath1).Length==0 &&
Directory.GetFiles(IncomingXMLPath2).Length==0)

{

m_ThreadFinall = new Thread(new System.Threading.ThreadStart(process3));

m_ThreadFinall.Start();

}


Respuesta Responder a este mensaje
#2 A.Poblacion
22/12/2004 - 09:35 | Informe spam
Errr... no, the code that I suggested you psot was not the code for
Process1. I meant the code you used in the main thread after you launched
processes 1 and 2. Something like this:

A) First you launch processes 1 and 2:

m_threadNewClaimInstitutional = new Thread(new
System.Threading.ThreadStart(process1));
m_threadNewClaimInstitutional.Start();
m_ThreadNewClaimProfessional= new Thread(new
System.Threading.ThreadStart(process2));
m_ThreadNewClaimProfessional.Start();

B) Then you wait for them to finish. One of the simplest ways is to use a
Join, but you could use a WaitHandle if you wanted to wait for both at the
same time:

m_threadNewClaimInstitutional.Join();
m_ThreadNewClaimProfessional.Join();

C) Next, after both 1 and 2 are done, you launch process 3:

m_ThreadFinall = new Thread(new System.Threading.ThreadStart(process3));
m_ThreadFinall.Start();


It is in the "B)" part where I think that you are missing something.



"CK" wrote in message
news:uJSos6$
Hi Poblacion,

Yes thats exactly what I want to do . I want the 3) process to run after


1)
and 2) complete. but I dont want to restart the service again (the whole
process works fine if i restart the service). Is there a way to put a


timer
for process 3) or any other thoughts?

Thanks
here is the code for process 1) . process 2) is the same ;

private void Process1()

{

//Instantiate the Converter class

Newclass objP= new NewClass();

//Process the Files one by one

while(Directory.GetFiles(IncomingXMLPathPLength>0)

{

if(Directory.Exists(IncomingXMLPathP) &&


Directory.Exists(PathIntermediate))

{

//Get the File in the directory

DirectoryInfo dir = new DirectoryInfo(IncomingXMLPathP);

FileInfo[] xmlfiles = dir.GetFiles("*.xml");

Thread.Sleep(1000);

foreach( FileInfo file in xmlfiles)

{


string strH="";

string InternalTransactionID="";

//Opens an existing UTF-8 encoded text file for reading.

StreamReader sr = File.OpenText(file.FullName);

string data= sr.ReadToEnd();


strH= objP.ConvertNewCt(data,out InternalTransactionID);

FileInfo outputFile = new
FileInfo(PathIntermediate+InternalTransactionID+".clm");

StreamWriter strWriter =new StreamWriter(File.Open(PathIntermediate+
InternalTransactionID +".clm",FileMode.Create, FileAccess.Write ));

strWriter.Flush();

strWriter.Write(strHMI.ToString());


strWriter.Close();

sr.Close();

file.Delete();

}

}

}

}






"A.Poblacion" wrote in
message news:
>I don't think you are doing anything wrong with the threading itself.
> Rather, it looks like you aren't waiting properly for the completion of
> processes 1) and 2) before going into the final step. Therefore, it


checks
> for wether the directories are empty, finds that they aren't (because
> processes 1 and 2 haven't finished), and skips the process3. When you
> start
> the service again, there has been enough time to compete precesses 1 and
> 2,
> the directories have been emptied and the check succeeds, enabling
> process3
> to run.
>
> You should post the missing piece of code (where you check for


completion
> of
> 1 and 2) for us to take a look and try to guess why it is not working
> properly.
>
>
> "CK" wrote in message
> news:%
>> I have the following code in a windows service, when I start the


windows
>> service process1 and process2 work fine , but final process (3) doesnt
> get
>> called. i stop and restart the windows service and the final process(3)
> gets
>> called. what am I doing wrong with the threading? by the way
>> Directory.GetFiles(IncomingXMLPath1).Length is some global outcome


from
>> process 1.
>>
>> Thanks
>>
>> 1)
>> m_threadNewClaimInstitutional = new Thread(new
>> System.Threading.ThreadStart(process1));
>>
>> m_threadNewClaimInstitutional.Start();
>>
>> 2)
>>
>> m_ThreadNewClaimProfessional= new Thread(new
>> System.Threading.ThreadStart(process2));
>>
>> m_ThreadNewClaimProfessional.Start();
>>
>> //when all the files have been processed in both the process1 and
>> process2
>> directories
>>
>> //then is the final process
>>
>> #)final
>>
>> if(Directory.GetFiles(IncomingXMLPath1).Length==0 &&
>> Directory.GetFiles(IncomingXMLPath2).Length==0)
>>
>> {
>>
>> m_ThreadFinall = new Thread(new


System.Threading.ThreadStart(process3));
>>
>> m_ThreadFinall.Start();
>>
>> }
>>
>>
>
>


Respuesta Responder a este mensaje
#3 CK
22/12/2004 - 11:17 | Informe spam
Hi Poblacion,

Yes thats exactly what I want to do . I want the 3) process to run after 1)
and 2) complete. but I dont want to restart the service again (the whole
process works fine if i restart the service). Is there a way to put a timer
for process 3) or any other thoughts?

Thanks
here is the code for process 1) . process 2) is the same ;

private void Process1()

{

//Instantiate the Converter class

Newclass objP= new NewClass();

//Process the Files one by one

while(Directory.GetFiles(IncomingXMLPathPLength>0)

{

if(Directory.Exists(IncomingXMLPathP) && Directory.Exists(PathIntermediate))

{

//Get the File in the directory

DirectoryInfo dir = new DirectoryInfo(IncomingXMLPathP);

FileInfo[] xmlfiles = dir.GetFiles("*.xml");

Thread.Sleep(1000);

foreach( FileInfo file in xmlfiles)

{


string strH="";

string InternalTransactionID="";

//Opens an existing UTF-8 encoded text file for reading.

StreamReader sr = File.OpenText(file.FullName);

string data= sr.ReadToEnd();


strH= objP.ConvertNewCt(data,out InternalTransactionID);

FileInfo outputFile = new
FileInfo(PathIntermediate+InternalTransactionID+".clm");

StreamWriter strWriter =new StreamWriter(File.Open(PathIntermediate+
InternalTransactionID +".clm",FileMode.Create, FileAccess.Write ));

strWriter.Flush();

strWriter.Write(strHMI.ToString());


strWriter.Close();

sr.Close();

file.Delete();

}

}

}

}






"A.Poblacion" wrote in
message news:
I don't think you are doing anything wrong with the threading itself.
Rather, it looks like you aren't waiting properly for the completion of
processes 1) and 2) before going into the final step. Therefore, it checks
for wether the directories are empty, finds that they aren't (because
processes 1 and 2 haven't finished), and skips the process3. When you
start
the service again, there has been enough time to compete precesses 1 and
2,
the directories have been emptied and the check succeeds, enabling
process3
to run.

You should post the missing piece of code (where you check for completion
of
1 and 2) for us to take a look and try to guess why it is not working
properly.


"CK" wrote in message
news:%
I have the following code in a windows service, when I start the windows
service process1 and process2 work fine , but final process (3) doesnt


get
called. i stop and restart the windows service and the final process(3)


gets
called. what am I doing wrong with the threading? by the way
Directory.GetFiles(IncomingXMLPath1).Length is some global outcome from
process 1.

Thanks

1)
m_threadNewClaimInstitutional = new Thread(new
System.Threading.ThreadStart(process1));

m_threadNewClaimInstitutional.Start();

2)

m_ThreadNewClaimProfessional= new Thread(new
System.Threading.ThreadStart(process2));

m_ThreadNewClaimProfessional.Start();

//when all the files have been processed in both the process1 and
process2
directories

//then is the final process

#)final

if(Directory.GetFiles(IncomingXMLPath1).Length==0 &&
Directory.GetFiles(IncomingXMLPath2).Length==0)

{

m_ThreadFinall = new Thread(new System.Threading.ThreadStart(process3));

m_ThreadFinall.Start();

}






email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida