How to start process A after completing process B in c#?











up vote
0
down vote

favorite












I have written a windows service to move a file from one location to another and,



then read the file in the new location and write the data to the database.



In the middle of the execution there is an error message:



IOException: The process cannot access the file 'file path' because it is being used by another process.



My code:



File move class



  public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";

string targetPath = @"C:Userssource";


string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);


if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);

if (System.IO.Directory.Exists(sourcePath))
{

string files = System.IO.Directory.GetFiles(sourcePath);

fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;

File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");

}
catch (IOException ex) { }


}

}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}


Insert Class



public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();

while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";


I did not mention the whole code for the service since it's too lengthy.



As given in Solution 01 I tried to use,



using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))


line instead of



 using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))


But that triggers errors in the below keywords I have used.



I am not sure is it possible to use a file open instead of a reader class?



Basically, what I need to do is start the Insert process after completing the file move process.



Any suggestions on how to do that?



PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!










share|improve this question




















  • 2




    You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
    – Christopher
    Nov 22 at 2:36












  • @Adriano Repetti any thoughts?
    – Simon
    Nov 22 at 2:43










  • Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
    – Tom Blodget
    Nov 22 at 2:55










  • There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
    – Simon
    Nov 22 at 3:02










  • the whole you first code sample can be replaced with File.Move(sourceFile, destFile)
    – vasily.sib
    Nov 22 at 3:12















up vote
0
down vote

favorite












I have written a windows service to move a file from one location to another and,



then read the file in the new location and write the data to the database.



In the middle of the execution there is an error message:



IOException: The process cannot access the file 'file path' because it is being used by another process.



My code:



File move class



  public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";

string targetPath = @"C:Userssource";


string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);


if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);

if (System.IO.Directory.Exists(sourcePath))
{

string files = System.IO.Directory.GetFiles(sourcePath);

fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;

File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");

}
catch (IOException ex) { }


}

}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}


Insert Class



public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();

while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";


I did not mention the whole code for the service since it's too lengthy.



As given in Solution 01 I tried to use,



using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))


line instead of



 using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))


But that triggers errors in the below keywords I have used.



I am not sure is it possible to use a file open instead of a reader class?



Basically, what I need to do is start the Insert process after completing the file move process.



Any suggestions on how to do that?



PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!










share|improve this question




















  • 2




    You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
    – Christopher
    Nov 22 at 2:36












  • @Adriano Repetti any thoughts?
    – Simon
    Nov 22 at 2:43










  • Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
    – Tom Blodget
    Nov 22 at 2:55










  • There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
    – Simon
    Nov 22 at 3:02










  • the whole you first code sample can be replaced with File.Move(sourceFile, destFile)
    – vasily.sib
    Nov 22 at 3:12













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have written a windows service to move a file from one location to another and,



then read the file in the new location and write the data to the database.



In the middle of the execution there is an error message:



IOException: The process cannot access the file 'file path' because it is being used by another process.



My code:



File move class



  public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";

string targetPath = @"C:Userssource";


string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);


if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);

if (System.IO.Directory.Exists(sourcePath))
{

string files = System.IO.Directory.GetFiles(sourcePath);

fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;

File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");

}
catch (IOException ex) { }


}

}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}


Insert Class



public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();

while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";


I did not mention the whole code for the service since it's too lengthy.



As given in Solution 01 I tried to use,



using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))


line instead of



 using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))


But that triggers errors in the below keywords I have used.



I am not sure is it possible to use a file open instead of a reader class?



Basically, what I need to do is start the Insert process after completing the file move process.



Any suggestions on how to do that?



PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!










share|improve this question















I have written a windows service to move a file from one location to another and,



then read the file in the new location and write the data to the database.



In the middle of the execution there is an error message:



IOException: The process cannot access the file 'file path' because it is being used by another process.



My code:



File move class



  public void mysql()
{
string fileName = "Bargstedt.csv";
string sourcePath = @"\192.168.1.2DataCompany Files";

string targetPath = @"C:Userssource";


string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);


if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);

if (System.IO.Directory.Exists(sourcePath))
{

string files = System.IO.Directory.GetFiles(sourcePath);

fileName = System.IO.Path.GetFileName(fileName);
destFile = System.IO.Path.Combine(targetPath, fileName);
FileInfo info = new FileInfo(destFile);
bool exists = info.Exists;
if (exists == true)
{
try
{
int delay = 400;

File.Delete(@"C:UsersIsuruhsourceBargstedt.csv");
Thread.Sleep(delay);
System.IO.File.Copy(sourceFile, destFile, true);
Debug.WriteLine("file moved");

}
catch (IOException ex) { }


}

}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
}


Insert Class



public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))
//using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
List<string> listA = new List<string>();

while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(board_code,status,code,no1,no2,thickness,dimension,material,root,variable,number,stable,constant)"
+ "VALUES (?jobNo, ?strClientName, ?strClientReference, ?strJobCategory, ?datCommisioned, ?datPromisedDelivery, ?division, ?date_assigned, ?root, ?variable, ?number, ?stable, ?constant)";


I did not mention the whole code for the service since it's too lengthy.



As given in Solution 01 I tried to use,



using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.Read))


line instead of



 using(var reader = new StreamReader(@"C:UserssourceBargstedt.csv"))


But that triggers errors in the below keywords I have used.



I am not sure is it possible to use a file open instead of a reader class?



Basically, what I need to do is start the Insert process after completing the file move process.



Any suggestions on how to do that?



PS: This is possibly a duplicate question of the question I have mentioned above. I tried my very best to make this question unique. Appreciate if not marked as duplicate. Thank you!







c# file-io resources windows-services






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 2:53









JohnB

1,3291015




1,3291015










asked Nov 22 at 2:32









Simon

6210




6210








  • 2




    You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
    – Christopher
    Nov 22 at 2:36












  • @Adriano Repetti any thoughts?
    – Simon
    Nov 22 at 2:43










  • Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
    – Tom Blodget
    Nov 22 at 2:55










  • There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
    – Simon
    Nov 22 at 3:02










  • the whole you first code sample can be replaced with File.Move(sourceFile, destFile)
    – vasily.sib
    Nov 22 at 3:12














  • 2




    You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
    – Christopher
    Nov 22 at 2:36












  • @Adriano Repetti any thoughts?
    – Simon
    Nov 22 at 2:43










  • Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
    – Tom Blodget
    Nov 22 at 2:55










  • There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
    – Simon
    Nov 22 at 3:02










  • the whole you first code sample can be replaced with File.Move(sourceFile, destFile)
    – vasily.sib
    Nov 22 at 3:12








2




2




You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36






You do not properly dispose of the FileHandle. Filehandles and network connections are the classical examples of unmanaged resources that need disposing. Ideally use the using block to get rid of them reliably.
– Christopher
Nov 22 at 2:36














@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43




@Adriano Repetti any thoughts?
– Simon
Nov 22 at 2:43












Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55




Where is the exception thrown? I'm going to guess that unstated part of this story is that there is another process that writes the file in the original location and that this service is supposed to grab it when it's done. Is that right?
– Tom Blodget
Nov 22 at 2:55












There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02




There ain't any other process to "write the file in the original location (in your words)". Only the file move class which move the csv from original location to the new location. That's it! @Tom Blodget
– Simon
Nov 22 at 3:02












the whole you first code sample can be replaced with File.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12




the whole you first code sample can be replaced with File.Move(sourceFile, destFile)
– vasily.sib
Nov 22 at 3:12

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423097%2fhow-to-start-process-a-after-completing-process-b-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423097%2fhow-to-start-process-a-after-completing-process-b-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)