如何从数据访问层中的函数触发进度条
我在类中有一个函数:对于此函数中的每个获取的记录,我将记录写入文件,我还想在进度栏中添加此获取的记录的值/计数,但是此进度栏存在于表单页面中并且函数存在于我的类中,我在表单页面中调用该函数,
这里是该函数的一部分,
this.path_to_file =
ConfigurationManager.AppSettings["ExportDir"] +
DateTime.Now.ToString(ConfigurationManager.AppSettings
["Export_FileName"]) + ConfigurationManager.AppSettings
["Export_Extension"];
FileStream fm = new FileStream
(this.path_to_file, FileMode.Create, FileAccess.ReadWrite,
FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fm, Encoding.Default);
List<Export> exportRecords = null;
exportRecords = ExportList();
try
{
int i = 0;
foreach (Export ex in exportRecords)
{
sw.Write(ex.ExportLine());
sw.Write(sw.NewLine);
sw.Flush();
i++;
}
}
catch (Exception exc)
{
Log.Write(exc.Message);
}
它返回字符串
I have a function in a class: for each fetched record in this function I write the record to a file, I also want to add the value/count of this fetched record in my progressbar, BUT this progressbar exists in the form page and the function exists in my class, I call the function in the formpage
here is a part of this function
this.path_to_file =
ConfigurationManager.AppSettings["ExportDir"] +
DateTime.Now.ToString(ConfigurationManager.AppSettings
["Export_FileName"]) + ConfigurationManager.AppSettings
["Export_Extension"];
FileStream fm = new FileStream
(this.path_to_file, FileMode.Create, FileAccess.ReadWrite,
FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fm, Encoding.Default);
List<Export> exportRecords = null;
exportRecords = ExportList();
try
{
int i = 0;
foreach (Export ex in exportRecords)
{
sw.Write(ex.ExportLine());
sw.Write(sw.NewLine);
sw.Flush();
i++;
}
}
catch (Exception exc)
{
Log.Write(exc.Message);
}
it returns strings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让您的班级提出一个带有进度的事件,您的表格也会相应更新。
或者更好的是,在后台线程上执行此操作:
http:// /msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Have your class raise an event with the progress, and your form update accordingly.
Or better yet, do it on a background thread:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx