तो चाहते हैं, इन और अन्य इसी तरह के सवाल मैं दो अलग-अलग कार्यक्रमों एक का उपयोग कर के साथ एक समस्या को सुलझाने की कोशिश आज दोपहर एक प्रमुदित हंस का पीछा करने पर चला गया के कुछ के माध्यम से जल्दी से नजर होने सिंक्रनाइज़ेशन (और फाइल सेव) विधि के रूप में फ़ाइल करें। एक असामान्य स्थिति का थोड़ा सा, लेकिन यह निश्चित रूप से मेरे लिए 'हाइलाइट किया गया है कि फाइल को लॉक किया गया है, तो जांचें', तो इसे खोलें, अगर यह 'दृष्टिकोण नहीं है तो इसे खोलें।
समस्या यह है: फ़ाइल हो सकती है जब आप इसे चेक करते समय और वास्तव में फ़ाइल खोलने के बीच लॉक हो जाते हैं। स्पोरैडिक को ट्रैक करना वाकई मुश्किल है फ़ाइल की प्रतिलिपि नहीं बना सकता है, क्योंकि यह किसी अन्य प्रक्रिया द्वारा उपयोग किया जाता है त्रुटि अगर आप इसे भी नहीं ढूंढ रहे हैं।
मूल संकल्प सिर्फ पकड़ ब्लॉक के अंदर फ़ाइल को खोलने का प्रयास करना है ताकि अगर यह बंद हो जाए, तो आप फिर कोशिश कर सकते हैं। इस तरह चेक और ओपनिंग के बीच कोई समय नहीं है, ओएस उन्हें एक ही समय में करता है।
यहां कोड फ़ाइल का उपयोग करता है।आदि
/// <param name="timeout">how long to keep trying in milliseconds</param>
static void safeCopy(string src, string dst, int timeout)
{
while (timeout > 0)
{
try
{
File.Copy(src, dst);
//don't forget to either return from the function or break out fo the while loop
break;
}
catch (IOException)
{
//you could do the sleep in here, but its probably a good idea to exit the error handler as soon as possible
}
Thread.Sleep(100);
//if its a very long wait this will acumulate very small errors.
//For most things it's probably fine, but if you need precision over a long time span, consider
// using some sort of timer or DateTime.Now as a better alternative
timeout -= 100;
}
}
parellelism पर एक और छोटी टिप्पणी File.Open, File.ReadAllText, File.WriteAllText,: यह कॉपी, लेकिन यह स्थिर फ़ाइल वर्ग के तरीकों में से किसी के साथ तरह ही काम करता एक सिंक्रोनस विधि है, जो प्रतीक्षा करते समय और थ्रेड पर काम करते समय दोनों को अपने धागे को अवरुद्ध कर देगी। यह सबसे आसान तरीका है, लेकिन यदि फ़ाइल लंबे समय तक बंद रहती है तो आपका प्रोग्राम उत्तरदायी नहीं हो सकता है। पेरेलेलिज्म यहां गहराई से जाने के लिए एक विषय बहुत बड़ा है, (और जिस तरह से आप एसिंक्रोनस रीड/राइट सेट अप कर सकते हैं, वह कई प्रकार का अपरिपक्व है) लेकिन यहां एक तरीका है जिसे इसे पेरेलेलाइज्ड किया जा सकता है।
public class FileEx
{
public static async void CopyWaitAsync(string src, string dst, int timeout, Action doWhenDone)
{
while (timeout > 0)
{
try
{
File.Copy(src, dst);
doWhenDone();
break;
}
catch (IOException) { }
await Task.Delay(100);
timeout -= 100;
}
}
public static async Task<string> ReadAllTextWaitAsync(string filePath, int timeout)
{
while (timeout > 0)
{
try {
return File.ReadAllText(filePath);
}
catch (IOException) { }
await Task.Delay(100);
timeout -= 100;
}
return "";
}
public static async void WriteAllTextWaitAsync(string filePath, string contents, int timeout)
{
while (timeout > 0)
{
try
{
File.WriteAllText(filePath, contents);
return;
}
catch (IOException) { }
await Task.Delay(100);
timeout -= 100;
}
}
}
और यहाँ है कि यह कैसे इस्तेमाल किया जा सकता है:
public static void Main()
{
test_FileEx();
Console.WriteLine("Me First!");
}
public static async void test_FileEx()
{
await Task.Delay(1);
//you can do this, but it gives a compiler warning because it can potentially return immediately without finishing the copy
//As a side note, if the file is not locked this will not return until the copy operation completes. Async functions run synchronously
//until the first 'await'. See the documentation for async: https://msdn.microsoft.com/en-us/library/hh156513.aspx
CopyWaitAsync("file1.txt", "file1.bat", 1000);
//this is the normal way of using this kind of async function. Execution of the following lines will always occur AFTER the copy finishes
await CopyWaitAsync("file1.txt", "file1.readme", 1000);
Console.WriteLine("file1.txt copied to file1.readme");
//The following line doesn't cause a compiler error, but it doesn't make any sense either.
ReadAllTextWaitAsync("file1.readme", 1000);
//To get the return value of the function, you have to use this function with the await keyword
string text = await ReadAllTextWaitAsync("file1.readme", 1000);
Console.WriteLine("file1.readme says: " + text);
}
//Output:
//Me First!
//file1.txt copied to file1.readme
//file1.readme says: Text to be duplicated!
मैं इस सवाल का जवाब यहाँ खेद पाया [Stackoverflow पर हल यह समस्या] [1] [1] : http://stackoverflow.com/questions/1406808/wait-for-file-to-be-freed-by-process – levi
संभावित डुप्लिकेट [क्या यह जांचने का कोई तरीका है कि कोई फ़ाइल है या नहीं n उपयोग?] (http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) –