多线程 [英] multithreading

查看:77
本文介绍了多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要进行多线程处理.这是我的拳头尝试.失败了这是设置:

I need to do multithreading. It is my fist attempt. It failed. This is the setup:

有一个带有tabControl和几页的表单.其中一个进度条.应该用来衡量文件下载的进度.如果不进行多线程处理,则在下载文件时该线程已死,然后最终跳到100%.

There is a form with tabControl and a few pages. There is a progress bar on one of them. It is supposed to measure progress of file downloading. WIthout multhithreading it is dead while the files are downloaded and then in the end it jumps to 100%.

这就是我所做的.一切都已编译,但即使下载结束后,进度也从未被激活.换句话说,我的代码实际上使情况变得更糟.

This is what I did. Everything compiled but the progress never was activated even after the downloading was over. In other words my code actually made the things worse.

在主要形式中,我将这段代码放在处理下载的过程的开头:

In the main form I put in this code at the start of procedure that is handling the download:

ThreadStart threadDelegate =新的ThreadStart(Work.DoWork);
线程newThread的=新线程(threadDelegate);
newThread.Start();

ThreadStart threadDelegate = new ThreadStart ( Work.DoWork );
Thread newThread               = new Thread ( threadDelegate );
newThread.Start ( );

然后在下载的循环中,我将其放入:

Then in the loop that does downloading I put this in:

Thread.Sleep(0);
工作工作=新工作();
work.data = prg; //prg是已完成工作的计算百分比.

Thread.Sleep ( 0 );
Work work = new Work ( );
work.data = prg;  // prg is a calculated % of the work done.

循环结束后有一条语句:

After the loop ends there is a statement:

newThread.Abort();

newThread.Abort();

在一个单独的* cs文件中,我创建了一个像这样的类:

In a separate *cs file I created a class like this:

class Work
{
public int数据;
public static void DoWork() //Form1是我的主要表单
work works = new Work();
ff1.progressBar_Gr1pg3.Value = works.data; //这是第3页上的进度条
}
}

class Work
    {
        public int data;
        public static void DoWork (  )  
        {
            Form1 ff1 = new Form1 ( );  // Form1 is my main form
            Work works = new Work ( );
            ff1.progressBar_Gr1pg3.Value = works.data;  // this is the progress bar on page3
        }
    }

我希望我不要错过任何必不可少的东西.正如我说的那样,我从未见过这段代码的任何成果.

I hope I did not miss anything essential. As I said I never saw any fruit of this code.

这是我的测试代码.实际上,如果没有这个进度条,我可以活下去.我需要多线程处理其他事情.

It is a test code for me. In fact I can live without this progress bar. I need multithreading for other things.

请帮助.

推荐答案

我也有类似的情况,并且完全摆脱了计时器.相反,我使用了backgroundWorker组件:

I just had a similar situation, and I got rid of the timer altogether. Instead I used the backgroundWorker component:

http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

祝你好运,
俄罗斯

Good luck,
Russ


这篇关于多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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