Отказ от ответственности: через несколько подобных потоков без успеха.Файл, к которому обращается другой процесс
Hi Guys and Gals, Работает на twitch.tv chatbot и попадает в проблему с IOStreams. У меня есть файл (ViewerPoints.csv), в котором я храню некоторые произвольные точки, заданные для того, чтобы тратить время на канал, который будет использоваться для мини-игр. Я собираюсь запрограммировать позднее. Теперь каждый экземпляр StreamReader/StreamWriter в паре с .close() до следующего доступа будет это, однако я получаю следующее сообщение об ошибке:
System.IO.IOException was unhandled
HResult=-2147024864
Message=The process cannot access the file 'S:\Programming\Projects\C#\StarBot\StarBot\bin\Debug\ViewerPoints.csv' because it is being used by another process.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at StarBot.Program.assignPoints() in S:\Programming\Projects\C#\StarBot\StarBot\Program.cs:line 156
at StarBot.Program.Main(String[] args) in S:\Programming\Projects\C#\StarBot\StarBot\Program.cs:line 40
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
По существу файл уже используется, но я могу «Определите, где он используется и по чем. Я попытался перезагрузки (поскольку я много раз открывал и закрывал программу), я попытался удалить файл. Из того, что я вижу, нет других StreamReaders/Writers, открытых при вызове исключения, но Process Monitor показывает 2 экземпляра ViewerPoints.csv.
Полный код здесь все очень оцененная:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace StarBot
{
class Program
{
private static DateTime time;
private static double messageDelay = 30.0;
private static DateTime nextMessage = DateTime.UtcNow.AddSeconds(messageDelay);
private static string[] viewerArray;
private static string ViewerPointsFile = "ViewerPoints.csv";
static void Main(string[] args)
{
//password from www.twitchapps.com/tmi
//include "oauth:" portion
IRCClient irc = new IRCClient("irc.twitch.tv", 6667, "Star__Bot", "oauth:likeidreallyleavethiskeyinhere");
irc.joinRoom("dystarplays");
getViewers();
irc.sendChatMessage("Star__Bot Connected...");
while (true)
{
time = DateTime.UtcNow;
string message = irc.readMessage();
if(message!= null)
{
if (message.IndexOf('*') >= 0)
{
irc.sendChatMessage(processMessage(message));
}
}
if(time >= nextMessage)
{
assignPoints();
//print message from list
nextMessage = time.AddSeconds(messageDelay);
}
}
}
private static void assignPoints()
{
getViewers();
StreamReader sr = new StreamReader(ViewerPointsFile);
StringBuilder sb = new StringBuilder();
string viewerPointsFile = sr.ReadToEnd();
sr.Close();
int points;
string spoints;
viewerPointsFile = viewerPointsFile.Replace("\r\n", ",");
viewerPointsFile = viewerPointsFile.Remove(viewerPointsFile.Length - 1, 1);
string[] viewerPoints = viewerPointsFile.Split(',');
for (int i = 0; i< viewerPoints.Length; i= i + 2)
{
string viewerInFile = viewerPoints[i];
bool added = false;
foreach (string viewerInChannel in viewerArray)
{
if (viewerInFile.ToLower() == viewerInChannel.ToLower())
{
spoints = viewerPoints[Array.IndexOf(viewerPoints, viewerInFile) + 1];
points = int.Parse(spoints);
sb.Append(viewerInChannel + "," + ++points + "\r\n");
added = true;
}
}
if (!added)
{
spoints = viewerPoints[Array.IndexOf(viewerPoints, viewerInFile) + 1];
points = int.Parse(spoints);
sb.Append(viewerInFile + "," + points + "\r\n");
}
}
//error happens on the StreamWriter here
StreamWriter sw = new StreamWriter(ViewerPointsFile);
spoints = sb.ToString();
sw.Write(spoints);
sw.Close();
}
private static string getPoints(string user)
{
StreamReader sr = new StreamReader(ViewerPointsFile);
string line;
while ((line = sr.ReadLine().ToLower())!= null)
{
if (line.IndexOf(user) >= 0)
{
return line.Replace(",",": ");
}
}
sr.Close();
return user + ": 0";
}
}
}
[EDIT] отредактирован, чтобы удалить ненужный код.
это очень много кода для публикации. вы можете точно определить код, где это происходит, и обновить вопрос с помощью соответствующей информации, которую у нас нет времени, чтобы прочитать весь ваш обзор кода/кода, особенно если он не работает. – MethodMan
@MethodMan: Удален ненужный код и добавлен комментарий, чтобы выделить строку, где вызывается ошибка – Dystar
@Dystar какое значение имеет 'ViewerPointsFile' в момент исключения? – wimh