English 中文(简体)
如何在装上Xml文档时使用进步条
原标题:How to use Progress bar during loading an xml File

我想在装载遥控Xml文档时显示进展。 我正在视像C# 2008快版中使用Windows Application Form。

private void button1_Click(object sender, EventArgs e)
    {
        string text =  textBox1.Text;
        string url = "http://api.bing.net/xml.aspx?AppId=XXX&Query=" + text + "&Sources=Translation&Version=2.2&Market=en-us&Translation.SourceLanguage=en&Translation.TargetLanguage=De";

        XmlDocument xml = new XmlDocument();
        xml.Load(url);

        XmlNodeList node = xml.GetElementsByTagName("tra:TranslatedTerm");

        for (int x = 0; x < node.Count; x++ )
        {
            textBox2.Text = node[x].InnerText;
            progressbar1.Value = x;
        }
    }

排外法不是为了显示进展障碍。 请提供一些法典。 预 收

最佳回答

你想通过进展来思考什么? 而是下载档案(因为其大部)或处理档案?

Progress bar reflecting processing file

你的进步阻碍着变革,因为你的方法是同步的,否则就没有实现。 <代码>BackgroundWorker 类别完全针对这类问题设计。 它的主要工作是同步进行的,并且能够报告进展已经发生变化。 这里是如何改变旅行路线,以便使用:

private void button1_Click(object sender, EventArgs e)
{
    string text =  textBox1.Text;
    string url = "http://api.bing.net/xml.aspx?AppId=XXX&Query=" + text + "&Sources=Translation&Version=2.2&Market=en-us&Translation.SourceLanguage=en&Translation.TargetLanguage=De";

    XmlDocument xml = new XmlDocument();
    xml.Load(url);
    XmlNodeList node = xml.GetElementsByTagName("tra:TranslatedTerm");

    BackgroundWorker worker = new BackgroundWorker();

    // tell the background worker it can report progress
    worker.WorkerReportsProgress = true;

    // add our event handlers
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.RunWorkerCompleted);
    worker.ProgressChanged += new ProgressChangedEventHandler(this.ProgressChanged);
    worker.DoWork += new DoWorkEventHandler(this.DoWork);

    // start the worker thread
    worker.RunWorkerAsync(node);
}

现在,主要部分:

private void DoWork(object sender, DoWorkEventArgs e)
{
   // get a reference to the worker that started this request
   BackgroundWorker workerSender = sender as BackgroundWorker;

   // get a node list from agrument passed to RunWorkerAsync
   XmlNodeList node = e.Argument as XmlNodeList;

   for (int i = 0; x < node.Count; i++)
   {
       textBox2.Text = node[i].InnerText;
       workerSender.ReportProgress(node.Count / i);
   }
}

private void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // do something after work is completed     
}

public void ProgressChanged( object sender, ProgressChangedEventArgs e )
{
    progressBar.Value = e.ProgressPercentage;
}

Progress bar reflecting downloading file

Try using HttpWebRequest to have the file as a stream.

// Create a  WebRequest  object with the specified url. 
WebRequest myWebRequest = WebRequest.Create(url); 

// Send the  WebRequest  and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse(); 

// Obtain a  Stream  object associated with the response object.
Stream myStream = myWebResponse.GetResponseStream();

long myStreamLenght = myWebResponse.ContentLength;

So now you know the length of this XML file. Then you have to asynchronously read the content from stream (BackgroundWorker and StreamReader is a good idea). Use myStream.Position and myStreamLenght to calculate the progress.

I know that I m not very specific but I just wanted to put you in the right direction. I think it doesn t make sense to write about all those things here. Here you have links that will help you dealing with Stream and BackgroundWorker:

问题回答

进展阻碍着这里的工作,因为这是所有“同步”的法典。 <代码>Click活动从更新开始便把国际统一分类。

有两个解决办法。

你可以创造背景,处理XML案卷,背景报告进展到现场。 d 我建议使用<条码>BackgroundWorker,但在你之前有很多东西可以学习。

一种较容易的解决办法,尽管不是最佳的办法,是通过打电话Application来更新表格。 每次进展更新后,均采用。 这只能视为短期解决办法,因为如果任务迅速,你的申请似乎可以冻结。

这两种解决办法都有缺点——如果重新恢复进展,你的申请也将处理任何事件。 因此,在任务处理期间,你必须手工操作,并在任务完成后再使用。

A while back, I created a reusable solution for this issue. I created a "ProgressDialog" that takes a delegate. The progress dialog executes the delegate, and captures the progress, updates its progress bar, and even calculates the time remaining. The benefit of using the ProgressDialog is that I can show the dialog in "modal" mode, which blocks the access to the main UI, and even prevents the Click event from finishing.
Unfortunately, I don t have any code to share, just the idea.

The problem is that you are using window thread, and the window can not been redrawn, until you finish button1_Click.

利用背景工作者,你可以通过召集背景工作者来报告进展情况。 报告编写

首先,如果你重新测试,如果你不更新的理由是你重新处理,而且情况正好像主妇正在使用。

你们可以通过打电话申请来处理所有事件。 DoEvents(),但通过这样做,你还重新处理用户可能点击(包括这一纽芬兰语)的任何事项,这样,如果你想要多环境,并确保你不会最终处理额外工作,在方法启动时,就会失去形式。

尽管如此,一个更好的模式是把你的工作结合起来,在时间上或back上更新进展。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签