如何增加进度条当我写xml? [英] how to increase progressbar when i write xml?

查看:160
本文介绍了如何增加进度条当我写xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据集DS

DataSet ds = new DataSet();
SQL = "SELECT * FROM MyCount";
adp = new SqlCeDataAdapter(SQL, Conn);
adp.Fill(ds, "MyCount");
adp.Dispose();



我写这样的XML: ds.WriteXml(@\ MyPath\Count.xml);

我需要提高进度,当我做这个动作

i need to increase progressbar when i do this action.

该怎么办呢?

感谢

推荐答案

不幸的是,该数据集,也可以用中WriteXML()方法使用的TextWriter / XmlWriter的让你订阅任何ValueWritten事件,这将是做最彻底的方法。

Unfortunately, neither the DataSet nor the TextWriter/XmlWriter you can use with the WriteXml() method allow you to subscribe to any "ValueWritten" event, which would be the cleanest way to do it.

如果你绝对必须有一个工作进度栏将显示用户你多少XML写的,我建议你派生自己的XmlWriter实现,将暴露,并触发一个事件订阅的,你可以利用颠簸每个写你的进度条。您可以包含您实现真正做到书写(我强烈建议这样做),<击>一个XmlTextWriter但你可以从XmlTextWriter的直接派生没有。这是因为你会从XmlTextWriter的可扩展方法不是虚拟的,所以你要隐藏的XmlTextWriter的使用关键字实现。然后,DataSet.WriteXml(的XmlWriter)重载将把你的类为基础的XmlWriter,所以你写的那个方法隐藏的XmlTextWriter的实现将被忽略。

If you ABSOLUTELY MUST HAVE a working progress bar that will show the user how much XML you've written, I would suggest you derive your own XmlWriter implementation that will expose and fire a subscribable event that you can use to bump your progress bar with each write. You can contain an XmlTextWriter in your implementation to actually do the writing (and I highly recommend doing so), but you cannot derive from XmlTextWriter directly. This is because the methods you will be extending from XmlTextWriter are not virtual, so you would have to hide XmlTextWriter's implementation with the new keyword. The DataSet.WriteXml(XmlWriter) overload will then treat your class as the base XmlWriter, so the methods you wrote that hide XmlTextWriter's implementations would be ignored.

其他注意事项你需要权衡:

Other considerations you'll need to weigh:


  • 您不会知道的书面文件的确切大小,直到你的XmlWriter被告知关闭()它通过流中WriteXML()。因此,每个凸点将需要一些受过教育的猜测。

  • 如果您所有的文件预计将大致相同的大小,你能拿出的平均大块%的的比例,将成为您的一步到位。

  • 您也可以尝试直接设置进度百分比,移动通过在每个读取剩余进步一些固定百分比,这将渐近地移动朝100%,但没有达到它,直到进度条刻录机的关闭(并触发一个事件说你就大功告成了)。

  • 对于所有的事件,一定要正确地释放所有订阅的事件时,发布商或用户配置

  • 对于所有的UI元素,确保你没有试图调用与UI线程之外的进度条工作处理程序。这限制了你的多线程选项,从另一个线程调用会中WriteXML导致处理程序在该线程中运行。但是,你必须让UI线程处理器的一些时间重绘吧,这样一台单线程选项将不能工作。考虑使用异步写入幕后,和/或更新进度条后进行,一Thread.yield()

  • You won't know the exact size of the written file until your XmlWriter is told to Close() its stream by WriteXml(). Thus, each "bump" will need to be some sort of educated guess.
  • If all your files are expected to be roughly the same size, you can come up with an average "chunk to percent" ratio that will become your "step".
  • You can also try setting the progress percentage directly, moving the progress bar by some fixed percentage of the remaining progress on each read, which will move it asymptotically towards 100% but not reach it until your writer is closed (and fires an event saying you're done).
  • As with all events, make sure you properly release all subscriptions to the event when either the publisher or subscriber is disposed.
  • As with all UI elements, make sure you're not trying to call the handlers that work with the progress bar outside of the UI thread. This limits your multithreading options, as calling WriteXml from another thread will cause the handlers to be run in that thread. But, you must allow the UI thread some processor time to redraw the bar, so a flat single-threaded option won't work either. Consider using asychronous writes behind the scenes, and/or performing a Thread.Yield() after updating the progress bar.

编辑:其实,因为XmlWriter的声明抽象方法其中的XmlTextWriter必须实现,可以进一步覆盖它们在你的自定义类,这样你就可以从XmlTextWriter的直接继承

Actually, because XmlWriter declares abstract methods which XmlTextWriter must implement, you CAN further override them in your custom class, so you can inherit directly from XmlTextWriter.

一些示例代码:

public class ObservableXmlTextWriter: XmlTextWriter
{
   public delegate void XmlWriteHandler(object sender, XmlWriteEventArgs e);

   public event XmlWriteHandler XmlWritten;

   public event EventHandler XmlWriteComplete;

   public class XmlWriteEventArgs:EventArgs
   {
      public object Value{get; private set;}
      public XmlWriteEventArgs(object value) {Value = value;}
   }

   public override WriteValue(string value)
   {
      base.WriteValue(value);
      if(XmlWritten != null) XmlWritten(this, new XmlWriteEventArgs(value));
   }

   public override WriteValue(int value)
   {
      base.WriteValue(value);
      if(XmlWritten != null) XmlWritten(this, new XmlWriteEventArgs(value));
   }

   ... //override ALL Write methods to fire XmlWritten as above

   //Dispose will call Close(), so just make sure to do one or the other
   public override Close()
   {
      base.Close(value);
      if(XmlWriteComplete!= null) XmlWriteComplete(this, new EventArgs()));
   }
}

...

public void XmlWriteHandler(object sender, XmlWriteEventArgs e)
{
   //Feel free to come up with your own algorithm for approaching 100%;
   //the number of times this event fires will be proportional to the
   //number of data elements (rows * columns) in the DataSet.
   MyProgressBar.Increment((MyProgressBar.Maximum - MyProgressBar.Value) * .05)
}

public void XmlWriteCompleteHandler(object sender, EventArgs e)
{
   MyProgressBar.Value = MyProgressBar.Maximum;
}

这篇关于如何增加进度条当我写xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆