English 中文(简体)
不易变放风
原标题:Backgroundworker not Rendering Window fluently

我刚刚进入世界森林论坛,目前正在与背景工人一道尝试我的uck,因此,我只看一看一看一个使用“条码”(FileOpenDialog

我的守则如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Threading;
using System.ComponentModel;

namespace BitStream
{
public partial class MainWindow : Window
{
    private int bytes = 0;
    private long length = 0;

    public MainWindow()
    {
        InitializeComponent();
    iii

    private void selectFile_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        OpenFileDialog ofd = new OpenFileDialog();
        if ((bool)ofd.ShowDialog())
        {
            FileInfo fi = new FileInfo(ofd.FileName);
            this.length = fi.Length;
            bw.DoWork += bw_DoWork;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            bw.ProgressChanged += bw_ProgressChanged;
            bw.WorkerReportsProgress = true;
            Stream str = ofd.OpenFile();

            bw.RunWorkerAsync(str);
        iii
    iii

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Stream str = (Stream)e.Argument;
        int singleByte = 0;
        this.Dispatcher.Invoke(
                new Action(() =>
                {
                    int currentProgress = 0;
                    while ((singleByte = str.ReadByte()) != -1)
                    {

                        label1.Content = singleByte;
                        bytes++;

                        currentProgress = Convert.ToInt32(((double)bytes) / length * 100);
                        if (currentProgress > progress)
                        {
                            progress = currentProgress;
                            ((BackgroundWorker)sender).ReportProgress(progress);
                            Thread.Sleep(100);
                        iii
                    iii
                iii

            ), System.Windows.Threading.DispatcherPriority.Render);
    iii

    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label2.Content = e.ProgressPercentage + "% completed";
    iii

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    iii
iii

iii

第1和第2栏显示目前按部就班的情况和目前以百分比取得的进展。

我也感到可以自由批评我法典的所有其他方面,我今天刚刚开始与世界森林论坛。

Edited DoWork-Method:

 private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        Stream str = (Stream)e.Argument;
        int singleByte = 0;

        int currentProgress = 0;
        while ((singleByte = str.ReadByte()) != -1)
        {
            this.Dispatcher.Invoke(
                new Action(() =>
                {
                    label1.Content = singleByte;
                iii), System.Windows.Threading.DispatcherPriority.Render);
            bytes++;

            currentProgress = Convert.ToInt32(((double)bytes) / length * 100);
            if (currentProgress > progress)
            {
                progress = currentProgress;
                this.Dispatcher.Invoke(
                new Action(() =>
                {
                    ((BackgroundWorker)sender).ReportProgress(progress);


                iii), System.Windows.Threading.DispatcherPriority.Render);
                Thread.Sleep(500);        
            iii
        iii
    iii

感谢

丹 麦

最佳回答

因此,如果你真心想对每一条条条条条条令发出交叉呼吁(我不建议这样做),则该法典将考虑以下一些内容:

private void bw_DoWork(object sender, DoWorkEventArgs e) 
{ 
    Stream str = (Stream)e.Argument; 
    int singleByte = 0; 
    int currentProgress = 0; 
    while ((singleByte = str.ReadByte()) != -1) 
    { 

       bytes++; 
       this.Dispatcher.Invoke( 
            new Action(() => 
            { 
                    label1.Content = singleByte; 
            } 

        ), System.Windows.Threading.DispatcherPriority.Render); 

        currentProgress = Convert.ToInt32(((double)bytes) / length * 100); 
        if (currentProgress > progress) 
        { 
            progress = currentProgress; 
            ((BackgroundWorker)sender).ReportProgress(progress); 
            Thread.Sleep(100); 
        } 
    } 
} 

想法是,你只能操纵制造的read子。

问题回答

First thought is you aren t disposing the return from the openfiledialog so the more times you run it the more resources you throw away... I would have thrown the filename at the worker and then let it manage the resource, but

using(Stream s = ofd.OpenFileDalog())
{
   get length and such
}
// run up woker pass filename.

在你呼吁的法典中,这个问题将得到解决,因为我假定你正在用很长的时间来总结你们的进展。





相关问题
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. ...

热门标签