I have a windows form app that run few tasks with a view to read a database sqlite send that file if it run on a machine otherwise it does the opposite (read the data and fill another sqlite database).
If i run application as user seems ok but if i create a scheduled task (on windows 10 or windows server 2012) i see the status is always running and sometimes i see in my custom logs error messages as
System.IO.Exception: process cant access file c:...mydatabase.db because is used by another process.
I think the scheduled task process not end so it try to access resources in use from previous execution It is possible?
I would to get status 'ready' as other tasks i have set.
I think making async my main method on program.cs is not good idea.
In my code you see i create sqlite database and if task is completed i create a zip file and send the zip with ftp.
static async Task Main(string[] args)
{
args = new string[] { "ARGUMENT1" };//test
if (args != null && args.Length > 0)
{
var task = args[0];
if (task == "ARGUMENT1")
{
var form = new SerializerForm();
try
{
var serialize = Task.Run(async () =>
{
await form.SerializeDbToSQLiteAsync();
});
serialize.Wait();
if (serialize.Status == TaskStatus.RanToCompletion)
{
Task.Run(async () =>
{
await form.CompressDatabaseAsync();
}).Wait();
Task.Run(async () =>
{
await form.FtpUploadAsync();
}).Wait();
await form.SendMailAsync($"Serializer complete");
}
}
catch (Exception ex)
{
string errorMessage = $"ex...";
await form.SendMailAsync(errorMessage);
await form.LogAsync("SerializationScheduled", errorMessage);
}
}
else
{
var form = new DeserializerForm();
//perform deserialization
}
else
{
//normal esecution windows form
}
}
UPDATE Although my code is bad as my english here the problem is not the use of async/await(that i repeat is bad as all of you said). I note that using static method indeed instantiate the SerializerForm object i note the code works well in both case: when user run the program and when i scheduled the app
So could you please explain why? Tks in advance