在线求解答——C#多线程————————————————

lijun_net 2010-11-17 03:54:21


初次和线程打交道。想做一个这样的例子, 5个按钮分别控制5个进度条, 点击后各自显示进度,

但是后台代码遇到问题, 晕了:贴出来

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Thread MainThread;


private void button1_Click(object sender, EventArgs e)
{

ProgressBar prb = sender as ProgressBar;
Thread th1 = new Thread(new ThreadStart());
th1.Start();

}

private void Form2_Load(object sender, EventArgs e)
{
MainThread = Thread.CurrentThread;
MainThread.Name = "主线程";
}

void DoThings(ProgressBar prb)
{
for (int i = prb.Minimum; i < prb.Maximum; i++)
{
prb.Value = i;
}
}
}
}



主要就是在button1的事件处理中, 对Thread的声明出了问题, 一定要用委托吗?不能直接调用后面写的Dothing方法?参数怎么办?

http://wenwen.soso.com/z/q236069021.htm?cid=q.t2.m
...全文
211 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lijun_net 2010-11-17
  • 打赏
  • 举报
回复

看来没人来了
diecode 2010-11-17
  • 打赏
  • 举报
回复
关键在这里吧
Invoke(new MethodInvoker(。。。。。。
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ihandler 的回复:]

引用 4 楼 lijun_net 的回复:

3# ZengHD
效果是有了, 但是更迷糊了, 像这 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
都是内置的吗?

那像我那样 声明一个线程 Thread th=new Thread();
后面括号里的该接什么呢?

必须是委托?

如果想直接给方法, 可以……
[/Quote]

还是报错,
错误 1 “DoThings”的重载均与委托“System.Threading.ParameterizedThreadStart”不匹配 C:\Documents and Settings\lijun\桌面\lijun\WindowsApplication1\WindowsApplication1\Form2.cs 31 37 WindowsApplication1

Dothings的方法在1楼的代码里
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ihandler 的回复:]

引用 4 楼 lijun_net 的回复:

3# ZengHD
效果是有了, 但是更迷糊了, 像这 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
都是内置的吗?

那像我那样 声明一个线程 Thread th=new Thread();
后面括号里的该接什么呢?

必须是委托?

如果想直接给方法, 可以……
[/Quote]


IHandler 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lijun_net 的回复:]

3# ZengHD
效果是有了, 但是更迷糊了, 像这 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
都是内置的吗?

那像我那样 声明一个线程 Thread th=new Thread();
后面括号里的该接什么呢?

必须是委托?

如果想直接给方法, 可以么, 那该如何写呢
[/Quote]
Thread t1 = new Thread(new ThreadStart(无参数方法名,不带括号));
Thread t2 = new Thread(new ParameterizedThreadStart(有1个参数方法名,不带括号));
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
不能沉啊
Teng_s2000 2010-11-17
  • 打赏
  • 举报
回复
whbah 2010-11-17
  • 打赏
  • 举报
回复
class ThreadDemo
{
private static Thread _thread1;
...
private static Thread _threadi;
void Main()
{
_thread1=new Thread(new ThreadStart(Thread1Start()));
_thread1.Start();
}
private void Thread1Start()
{
//TODO:add code;
}
}
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
怎么办, 我很笨, 请帮帮忙
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zenghd 的回复:]

C# code
private void Run(ProgressBar pb)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
while (true)
{
Invoke(new ……
[/Quote]

而且 如果点击顺序是这样的go5,go4,go3,go2,go1,

那么, 这段代码还是会优先执行go1和go2,

为什么不是5个同时执行呢?

我理想的效果是 5个线程, 分别负责5个Progressbar, 同时执行
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
3# ZengHD
效果是有了, 但是更迷糊了, 像这 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
都是内置的吗?

那像我那样 声明一个线程 Thread th=new Thread();
后面括号里的该接什么呢?

必须是委托?

如果想直接给方法, 可以么, 那该如何写呢
ZengHD 2010-11-17
  • 打赏
  • 举报
回复
private void Run(ProgressBar pb)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate {
while (true)
{
Invoke(new MethodInvoker(delegate {
if (pb.Value >= pb.Maximum)
{
pb.Value = 0;
}
else
{
pb.Value++;
}
}));

Thread.Sleep(10);
}
}));
}

private void button1_Click(object sender, EventArgs e)
{
Run(progressBar1);
}

private void button3_Click(object sender, EventArgs e)
{
Run(progressBar2);
}

private void button2_Click(object sender, EventArgs e)
{
Run(progressBar3);
}

private void button4_Click(object sender, EventArgs e)
{
Run(progressBar4);
}

private void button5_Click(object sender, EventArgs e)
{
Run(progressBar5);
}
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
刚发现了个错误。

sender并不是ProgressBar, 我写错了,
lijun_net 2010-11-17
  • 打赏
  • 举报
回复
快来人啊

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧