C# ThreadPool.QueueUserWorkItem 多 CPU
我有一个程序使用:
ThreadPool.QueueUserWorkItem(new WaitCallback(FireAttackProc), fireResult);
在 Windows7 和 Vista 上它运行良好。
当我尝试在 XP 上运行它时,结果与其他的有点不同。
我只是想知道为了正确执行 QueueUserWorkItem 我是否需要双 CPU 系统?
我尝试测试的 XP 安装了 .Net 3.5。
欢迎输入。
编辑:回调过程播放一系列声音文件。 win7和vista都可以玩。但在XP中只有几个人玩。我从该计划中没有得到任何例外。
编辑:是的,XP 盒子是单核的。 5岁以上。
编辑:我的应用程序使用 Winsock,我在 XP 计算机上运行客户端和服务器。我将尝试在每台机器上运行一个实例,看看它的反应如何。
编辑:你如何播放声音?
SoundPlayer fire = new SoundPlayer(Properties.Resources.fire);
fire.PlaySync();
fire.Dispose();
I have a program that uses:
ThreadPool.QueueUserWorkItem(new WaitCallback(FireAttackProc), fireResult);
On Windows7 and Vista it works fine.
When I try to run it on XP the result is a bit different from the others.
I was just wondering in order to execute QueueUserWorkItem properly do I need a dual CPU system?
The XP I tried to test on had .Net 3.5 installed.
Inputs most welcome.
EDIT: The callback proc plays a series of sound files. in win7 and vista they all play. but in xp only a couple of them plays. I get no exceptions from the program.
EDIT: Yes the XP box is single core. more than 5 years old.
EDIT: My app uses Winsock and I ran both the client and server on the XP machine. I will try running it with a single instance per machine and see how it reacts.
EDIT: How are you playing the sounds?
SoundPlayer fire = new SoundPlayer(Properties.Resources.fire);
fire.PlaySync();
fire.Dispose();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
主要区别在于,在单核系统上,一次只能运行一个线程。如果您的程序设计得正确,那么这应该不重要,因为操作系统会切换线程进出并为您管理。
如果您在单核系统上发现差异,这很可能意味着您的代码中存在竞争条件。唯一的区别应该是它需要更长的时间 - 因为操作系统无法同时运行两个线程。
The main difference is that, on a single core system, only one thread can run at a time. If your program is designed properly, this shouldn't matter, as the operating system switches the threads in and out and manages this for you.
If you're seeing a difference on a single core system, this most likely means you have a race condition in your code. The only difference should be that it takes longer - since the OS can't run both threads concurrently.
Vista 和 Windows 7 处理音频的方式与 Windows XP 不同,因此这可能是问题的真正根源(即它与 QueueUserWorkItem 无关)。
您如何播放声音(因为您可以通过多种不同的方式来播放声音)?
编辑:当您说您正在播放“一系列”声音时,您的意思是您正在尝试一个接一个地播放声音,还是您正在尝试同时播放一堆声音同时?
Vista and Windows 7 handle audio differently from Windows XP, so that's probably the real source of your problem (i.e. it has nothing to do with
QueueUserWorkItem
).How are you playing the sounds (since there are many different ways you could be doing this)?
Edit: When you say you're playing a "series" of sounds, do you mean you're trying to play one sound after another, or you're trying to play a bunch of sounds all at the same time?
你究竟看到了什么?
线程的计时是不确定的,因此如果您在单处理器计算机上运行它,您会看到不同的结果也就不足为奇了。这是因为只有一个核心(没有超线程),一条指令只能一次执行,所以你不会看到真正的并行执行。
但是,Windows XP 支持多核,就像 Windows 7 或 Vista 一样。我假设你运行它的 XP 机器比较旧,并且只有 1 个 CPU?
What exactly did you see?
Timing with threads is non-deterministic, so it is not surprising you saw different results if you ran it on a single processor machine. That is because with only a single core (without hyperthreading), a single instruction can only execute at once, so you will not see true parallel execution.
BUT, windows XP supports multiple cores, just like Windows 7 or Vista. I assume the XP machine you ran it on was older, and only had 1 CPU?
您可能会遇到声音文件播放 API 的问题。下面的链接讨论了使用 C# 应用程序中的 PInvoke 以非常快的时间间隔或接近同时播放多个声音文件的问题。这可能与您的问题相似吗?
http://www.hanselman.com/blog/CategoryView。 aspx?category=BabySmash&page=3
You might be hitting problems with the sound file play API. The below link talks about problems playing multiple sound files at very fast intervals or close to simultaneously using PInvoke from a C# app. Could this be similar to your issue?
http://www.hanselman.com/blog/CategoryView.aspx?category=BabySmash&page=3
线程池管理器的行为很重要。它会尝试小心地避免调度比核心数更多的线程。所以如果你的XP机器有一个单核CPU,它只会允许一个线程运行。只有当线程“卡住”并且不能及时完成时,它才会允许另一个线程启动。这些调度决策每秒做出两次。
鉴于您使用线程来播放声音,线程池线程不是合适的解决方案。您应该创建自己的线程。
The behavior of the ThreadPool manager matters. It tries to carefully avoid scheduling more threads than you have cores. So if your XP machine has a single-core CPU, it will only allow one thread to run. Only when threads get "stuck" and do not complete in a timely manner will it allow another thread to start. These scheduling decisions are made twice a second.
Given that you are using threads to play sounds, a thread pool thread is not the appropriate solution. You should create your own Thread.