多线程 线程池 最大线程数

anncesky anncesky 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

for (int i = 0; i < 10; i++)
{
Thread.Sleep(1);
ThreadPool.QueueUserWorkItem(DoWork, i);
}
}
private void DoWork(object o)
{
Thread.Sleep(1000);
int id=Thread.CurrentThread.ManagedThreadId;
int userThread, ioThread;
ThreadPool.GetAvailableThreads(out userThread,out ioThread);
int userMaxThread, ioMaxThread;
ThreadPool.GetMaxThreads(out userMaxThread, out ioMaxThread);
this.Invoke(new Action(() => {
textBox1.Text = string.Format("{0}111i:{1} ID:{2} HashCode:{3} Aviad:{4} IO:{5}\r\n", textBox1.Text,
o.ToString(), id,
Thread.CurrentThread.GetHashCode(), userThread, userMaxThread);
textBox1.SelectionStart = textBox1.TextLength;
textBox1.ScrollToCaret();
}));

for (int k = 0; k < 10; k++)
{
ThreadPool.SetMaxThreads(10, 1);
ThreadPool.QueueUserWorkItem(DoWork2, k);
}
}

private void DoWork2(object o)
{
Thread.Sleep(100);
int id = Thread.CurrentThread.ManagedThreadId;
int userThread, ioThread;
ThreadPool.GetAvailableThreads(out userThread, out ioThread);
int userMaxThread, ioMaxThread;
ThreadPool.GetMaxThreads(out userMaxThread, out ioMaxThread);
this.Invoke(new Action(() =>
{
textBox1.Text = string.Format("{0}222i:{1} ID:{2} HashCode:{3} Aviad:{4} IO:{5}\r\n", textBox1.Text,
o.ToString(), id,
Thread.CurrentThread.GetHashCode(), userThread, userMaxThread);
textBox1.SelectionStart = textBox1.TextLength;
textBox1.ScrollToCaret();
}));
}

private void Form1_Load(object sender, EventArgs e)
{
ThreadPool.SetMaxThreads(10, 1);
}


}
}


在线程中启用线程,最大线程数无论怎么设置都无效果,都是1023,这是为什么哦,怎么处理?
...全文
评论 6
收藏 已收藏
分享
返回顶部
anncesky
C# 111,129 成员 642,539 内容