C# 异步安全地读取文件中的所有行

C# 异步安全地读取文件中的所有行,c#,io,.net-3.5,C#,Io,.net 3.5,我正在尝试异步、安全地读取文件(寻求的最低权限级别)。我正在使用.NET3.5,但找不到一个很好的例子(都使用async和await) 问题是,如何在.NET3.5中异步执行此操作,等待操作完成并将所有行发送回调用方 调用方可以等待操作完成,但读取必须在后台线程中进行 调用方是一个UI线程,我使用的是.NET 3.5。如果要等到操作完成,为什么需要异步执行 return File.ReadAllText(_path, new UnicodeEncoding()); 如果要等到操作完成,为什么需

我正在尝试异步、安全地读取文件(寻求的最低权限级别)。我正在使用.NET3.5,但找不到一个很好的例子(都使用async和await)

问题是,如何在.NET3.5中异步执行此操作,等待操作完成并将所有行发送回调用方

调用方可以等待操作完成,但读取必须在后台线程中进行


调用方是一个UI线程,我使用的是.NET 3.5。如果要等到操作完成,为什么需要异步执行

return File.ReadAllText(_path, new UnicodeEncoding());

如果要等到操作完成,为什么需要异步执行呢

return File.ReadAllText(_path, new UnicodeEncoding());

有几个选项,但最简单的方法是让此方法接受回调,然后在计算给定值时调用它。调用方需要传入回调方法来处理结果,而不是阻塞方法调用:

public static void GetLines(Action<string> callback)
{
    var encoding = new UnicodeEncoding();
    byte[] allText;
    FileStream stream = File.Open(_path, FileMode.Open);
    allText = new byte[stream.Length];
    //something like this, but does not compile in .net 3.5
    stream.ReadAsync(allText, 0, (int)allText.Length);
    stream.BeginRead(allText, 0, allText.Length, result =>
    {
        callback(encoding.GetString(allText));
        stream.Dispose();
    }, null);
}
publicstaticvoidgetlines(操作回调)
{
var encoding=新的Unicode编码();
字节[]所有文本;
FileStream stream=File.Open(_path,FileMode.Open);
allText=新字节[stream.Length];
//类似于此,但不在.NET3.5中编译
ReadAsync(allText,0,(int)allText.Length);
stream.BeginRead(allText,0,allText.Length,result=>
{
回调(encoding.GetString(allText));
stream.Dispose();
},空);
}

有几个选项,但最简单的方法是让此方法接受回调,然后在计算给定值时调用它。调用方需要传入回调方法来处理结果,而不是阻塞方法调用:

public static void GetLines(Action<string> callback)
{
    var encoding = new UnicodeEncoding();
    byte[] allText;
    FileStream stream = File.Open(_path, FileMode.Open);
    allText = new byte[stream.Length];
    //something like this, but does not compile in .net 3.5
    stream.ReadAsync(allText, 0, (int)allText.Length);
    stream.BeginRead(allText, 0, allText.Length, result =>
    {
        callback(encoding.GetString(allText));
        stream.Dispose();
    }, null);
}
publicstaticvoidgetlines(操作回调)
{
var encoding=新的Unicode编码();
字节[]所有文本;
FileStream stream=File.Open(_path,FileMode.Open);
allText=新字节[stream.Length];
//类似于此,但不在.NET3.5中编译
ReadAsync(allText,0,(int)allText.Length);
stream.BeginRead(allText,0,allText.Length,result=>
{
回调(encoding.GetString(allText));
stream.Dispose();
},空);
}

可能是这样的:

GetLines(string path, ()=>
{
    // here your code...
});

public void GetLines(string _path, Action<string> callback)
{
    var result = string.Empty;

    new Action(() =>
    {
        var encoding = new UnicodeEncoding();
        byte[] allText;
        using (FileStream stream = File.Open(_path, FileMode.Open))
        {
            allText = new byte[stream.Length];
            //something like this, but does not compile in .net 3.5
            stream.Read(allText, 0, (int)allText.Length);
        }
        result = encoding.GetString(allText);
    }).BeginInvoke(x => callback(result), null);
}
GetLines(字符串路径,()=>
{
//这是你的代码。。。
});
公共void GetLines(字符串_路径,操作回调)
{
var result=string.Empty;
新操作(()=>
{
var encoding=新的Unicode编码();
字节[]所有文本;
使用(FileStream stream=File.Open(_path,FileMode.Open))
{
allText=新字节[stream.Length];
//类似于此,但不在.NET3.5中编译
读取(allText,0,(int)allText.Length);
}
结果=encoding.GetString(allText);
}).BeginInvoke(x=>回调(结果),null);
}

可能是这样的:

GetLines(string path, ()=>
{
    // here your code...
});

public void GetLines(string _path, Action<string> callback)
{
    var result = string.Empty;

    new Action(() =>
    {
        var encoding = new UnicodeEncoding();
        byte[] allText;
        using (FileStream stream = File.Open(_path, FileMode.Open))
        {
            allText = new byte[stream.Length];
            //something like this, but does not compile in .net 3.5
            stream.Read(allText, 0, (int)allText.Length);
        }
        result = encoding.GetString(allText);
    }).BeginInvoke(x => callback(result), null);
}
GetLines(字符串路径,()=>
{
//这是你的代码。。。
});
公共void GetLines(字符串_路径,操作回调)
{
var result=string.Empty;
新操作(()=>
{
var encoding=新的Unicode编码();
字节[]所有文本;
使用(FileStream stream=File.Open(_path,FileMode.Open))
{
allText=新字节[stream.Length];
//类似于此,但不在.NET3.5中编译
读取(allText,0,(int)allText.Length);
}
结果=encoding.GetString(allText);
}).BeginInvoke(x=>回调(结果),null);
}

您想异步执行此操作,但要等到操作完成?必须调用GetLines async。您不在此处进行更改。是的,考虑到用户界面是调用方。您希望异步执行此操作,但要等到操作完成?您必须对GetLines进行异步调用。您不在此处进行更改。是的,考虑到用户界面是调用方。