[英]Write to a file that is being used by another process (Windows Service) in C#
[英]File being used by another process in windows service
我編寫了下面的代碼,該代碼運行良好,但問題是有時它使我的file being used by another process
這兩個作業正在訪問並寫入同一文件。 ClickProfileJob
首先運行,並在5秒后重復,然后根據5秒的計划運行第二個作業ClickLikeJob
。 我已經看到了一些解決方案,這些解決方案提出了我下面編碼的相同using
技術。
using Quartz;
using System;
using System.IO;
using Topshelf;
using Topshelf.Quartz;
namespace FinyaConsole
{
class Program
{
static void Main(string[] args)
{
userCreds creds = new userCreds();
if (creds.checkUser().Length > 3)
{
HostFactory.Run(x =>
{
x.Service<GiveHeartsService>(s =>
{
s.WhenStarted(service => service.OnStart());
s.WhenStopped(service => service.OnStop());
s.ConstructUsing(() => new GiveHeartsService());
s.ScheduleQuartzJob(q =>
q.WithJob(() =>
JobBuilder.Create<ClickProfileJob>().Build())
.AddTrigger(() => TriggerBuilder.Create()
.WithSimpleSchedule(b => b
.WithIntervalInSeconds(5)
.RepeatForever())
.Build()));
s.ScheduleQuartzJob(q =>
q.WithJob(() =>
JobBuilder.Create<ClickLikeJob>().Build())
.AddTrigger(() => TriggerBuilder.Create()
.WithSimpleSchedule(b => b
.WithIntervalInSeconds(5)
.RepeatForever())
.Build()));
});
//.DependsOnEventLog()
x.RunAsLocalSystem()
.StartAutomaticallyDelayed()
.EnableServiceRecovery(rc => rc.RestartService(1));
x.SetServiceName("FinyaHearts");
x.SetDisplayName("FinyaHearts");
x.SetDescription("This is a service.");
});
}
}
}
public class ClickProfileJob : IJob
{
public void Execute(IJobExecutionContext context)
{
try
{
using (StreamWriter sw = new StreamWriter(".\\visits_to_others.txt", true))
{
//Write a line of text
sw.WriteLine($"[{DateTime.Now}] Welcome from ClickProfileJob!");
Console.WriteLine($"[{DateTime.Now}] Welcome from ClickProfileJob!");
//System.IO.File.WriteAllText(@"path\visit_users.txt", userLink);
//Close the file
sw.Flush();
sw.Dispose();
sw.Close();
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
//Console.WriteLine("Executing finally block.");
}
}
}
public class ClickLikeJob : IJob
{
public void Execute(IJobExecutionContext context)
{
try
{
using (StreamWriter sw = new StreamWriter(".\\visits_to_others.txt", true))
{
//Write a line of text
sw.WriteLine($"[{DateTime.Now}] Welcome from ClickLikeJob!");
Console.WriteLine($"[{DateTime.Now}] Welcome from ClickLikeJob!");
//System.IO.File.WriteAllText(@"path\visit_users.txt", userLink);
//Close the file
sw.Flush();
sw.Dispose();
sw.Close();
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
//Console.WriteLine("Executing finally block.");
}
}
}
}
這不是Quartz特有的問題。 您可以使用SemaphoreSlim
或更好的簡單lock
。 只需創建一個基本作業,然后從中導出其他作業即可。 然后在兩個作業中鎖定LockObject
。
public abstract class LockableJobBase
{
protected static object LockObject = new object();
}
public class ClickProfileJob : LockableJobBase, IJob
{
public void Execute(IJobExecutionContext context)
{
try
{
lock (LockObject)
{
using (StreamWriter sw = new StreamWriter(".\\visits_to_others.txt", true))
{
// your sw stuff
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
//Console.WriteLine("Executing finally block.");
}
}
}
public class ClickLikeJob : LockableJobBase, IJob
{
public void Execute(IJobExecutionContext context)
{
try
{
lock (LockObject)
{
using (StreamWriter sw = new StreamWriter(".\\visits_to_others.txt", true))
{
// your sw stuff
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
//Console.WriteLine("Executing finally block.");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.