想大家请教个问题:关于多线程的……

zxphf 2005-05-20 04:11:55
想大家请教个问题:关于多线程的……
我在下载文件的时候启动一个线程
Thread downthread=new Thread(new ThreadStart(Locdownfile));
downthread.Start();启动后
我想在Locdownfile()函数里面把下载文件信息显示在一个listview里面,最重要的是我现在要用一个Timer定时刷新这个listview;现在我提醒注意的问题是整个定时刷新过程要在下载过程中……我现在不能做的是不能让下载和定时同时启动并完成整个下载过程……
...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2005-05-20
  • 打赏
  • 举报
回复
using System;
using System.Threading;
namespace ConsoleApplication1
{
public class myEventArgs : System.EventArgs
{
public int nCount;
public myEventArgs(int n)
{
nCount = n;
}
}
public class DownloadFile
{
public delegate void devent( object sender , myEventArgs e );
public event devent OnEvent;
public void thread()
{
for( int i=0 ; i<100 ; i++ )
{
if( OnEvent != null )
{
System.Threading.Thread.Sleep(100);
OnEvent( this , new myEventArgs(i) );
}
}
}
public DownloadFile()
{}
public void run()
{
System.Threading.Thread t = new Thread( new System.Threading.ThreadStart( thread ));
t.Start();

}
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
DownloadFile df = new DownloadFile();
df.OnEvent += new ConsoleApplication1.DownloadFile.devent(df_OnEvent);
df.run();
}

private static void df_OnEvent(object sender, myEventArgs e)
{
Console.WriteLine( e.nCount.ToString() + "%" );
}
}
}
zxphf 2005-05-20
  • 打赏
  • 举报
回复
不会吧,我下载有速度显示的,不然我当然不会无故让他刷新listview……
要是多开一个线程,你说如何控制他们的下载呢?平分一个文件的字节吗?在组合在一起,还是怎么的?你这个有思路吗?
rachy 2005-05-20
  • 打赏
  • 举报
回复
刷新应该不用考虑吧,你加一条自然就会显示多一行
如果是下载文件需要的时间较长的话,要开多一个线程来处理

Thread scanThread = new Thread(new ThreadStart(u.ScanWebsites));

public void ScanWebsites()
{
}
zxphf 2005-05-20
  • 打赏
  • 举报
回复
不知道我说的清楚不?或者大家给我个思路……

110,539

社区成员

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

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

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