C#多线程处理
2024-09-22 02:12:26
创建多线程,并带参数!
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading; namespace ThreadProcess
{
class Program
{
static void Main(string[] args)
{
string path = @"c:\test\";
string[] Dirs = Directory.GetFiles(path, "*.txt");
//创建相应线程数
ArrayList al = new ArrayList();
foreach(string s in Dirs)
{
if (!File.Exists(s.Substring(, s.LastIndexOf('.')) + ".lock"))
al.Add(s);
}
Thread[] t = new Thread[al.Count];
for (int i = ; i <al.Count; i++)
{
t[i] = new Thread(new ParameterizedThreadStart(Process));
t[i].Start(Dirs[i].ToString());
}
} static void Process(object fileName)
{
string procFile = (string)fileName;
string lockFile = procFile.Substring(, procFile.LastIndexOf('.')) + ".lock";
//if (File.Exists(lockFile))
//{
// return;
//}
//else
//{
// FileStream fs = new FileStream(lockFile, FileMode.CreateNew);
// fs.Close(); //}
FileStream fs = new FileStream(lockFile, FileMode.CreateNew);
fs.Close(); while (true)
{
//这里作相应处理工作
Console.Write(lockFile);
Thread.Sleep();
}
File.Delete(lockFile);
File.Delete(procFile); }
}
}
最新文章
- switch与ifelse的效率问题
- C#调用SQL Server分页存储过程
- unity 播放音乐
- C#数组的排序
- windows下svn自动更新
- js风格技巧
- 单位内部DNS架设及域名解析服务
- python wsgi
- 二维动态规划——Palindrome
- MybatisGenerator生成的mapper 少了识别主键的方法 byPrimaryKey()
- 洛谷P1774 最接近神的人_NOI导刊2010提高(02)(求逆序对)
- kvm qemu ,ubuntu debian rootfs 通过qemu复现路由器漏洞
- Hibernate+struct web项目问题总结
- L236
- Microsoft - Union Two Sorted List with Distinct Value
- AJAX学习必备三本书
- Arduino + SmartAirFilter 制作智能感应的 PM 空气净化器
- Eclipse+ADT+Android SDK 搭建安卓开发环境(版权属于forever-z)
- SpringBoot入门之spring-boot-maven-plugin
- 51nod-1636-dp
热门文章
- java jdk动态代理(proxy)
- SpringBoot使用qq邮箱发送邮件
- [已解决]ValueError: row index was 65536, not allowed by .xls format
- git 多用户多仓库配置
- 【vue】iView-admin2.0动态菜单路由
- PHP按权重随机
- 使用axios 的post请求下载文件,
- jeecg字典表—报表配置(popup弹框)
- 关于spring boot中 EmbeddedServletContainerCustomizer
- Android Studio自定义注释模板