如何从数据访问层中的函数触发进度条

发布于 2024-08-13 14:46:35 字数 1007 浏览 1 评论 0原文

我在类中有一个函数:对于此函数中的每个获取的记录,我将记录写入文件,我还想在进度栏中添加此获取的记录的值/计数,但是此进度栏存在于表单页面中并且函数存在于我的类中,我在表单页面中调用该函数,

这里是该函数的一部分,

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2024-08-20 14:46:35

让您的班级提出一个带有进度的事件,您的表格也会相应更新。

或者更好的是,在后台线程上执行此操作:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文