在winform代码中通过startUpload方法开启线程,并使用委托将UploadProBar方法传递给aws对象的TestPgBar方法,然后再进行调用,但是报异常System.InvalidOperationException:“线程间操作无效: 从不是创建控件“pgBar”的线程访问它 如何解决?注:不要使用跨过线程安全检查方式解决该问题
namespace Burn
{
public partial class Frm_File_Upload_Progress : AppListForm
{
public Frm_File_Upload_Progress()
{
InitializeComponent();
startUpload();
}
private void startUpload()
{
isUpload = true;
Thread thread = new Thread(() =>
{
AWS aws = new AWS();
//报错
aws.TestPgBar(UpdateProBar);
});
thread.Start();
thread.IsBackground = true;
}
private void UpdateProBar(int val)
{
this.pgBar.Value = val;
}
}
}
namespace Models
{
public class AWS
{
string accessKey = "";
string secretKey = "";
public AmazonS3Client s3Client;
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
public AWS()
{
AmazonS3Config config = new AmazonS3Config();
config.ServiceURL = "";
config.ForcePathStyle = true; //C#默认使用泛域名方式访问,修改默认配置使用url路径形式访问对象存储
s3Client = new AmazonS3Client(
accessKey,
secretKey,
config
);
}
public void TestPgBar(Action<int> @updateProBar)
{
for (int i = 0; i < 10; i++)
{
@updateProBar(i * 1000);
}
}
}
}