尝试进行多线程处理时,WriteFile()崩溃 [英] WriteFile() crashes when trying to carry on multithreading

查看:150
本文介绍了尝试进行多线程处理时,WriteFile()崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在通过脚本访问的mfc dll,所有这些工作正常.我向其中添加了一个多线程组件,并尝试使用WriteFile()函数写入我的串行端口,但是以某种方式,在执行第二个write命令后,WriteFile()函数将退出应用程序.

没有多线程位,一切都可以正常工作,我可以根据需要应用任意数量的writefile命令.

多线程:我正在使用

Hello,

I am working on an mfc dll that is accessed via a script and all this works fine. I have added a multi-threading component to it and am trying to use the WriteFile() function to write to my serial port, but somehow the WriteFile() function exits the application after the 2nd write command gets executed.

Without the multithreading bit, everything works normally and I can apply as many writefile commands as I want.

Multi-threading: I am using

CreateThread(NULL,0,WorkerThread,this,0,0);



创建我的线程.使用"WorkerThread"执行后台前面所述的writefile操作.

另外,我需要在我定义的时间间隔内编写函数时使用Sleep()函数.目前,该程序只是在尝试使用Sleep()时退出.因此,我暂时将其删除,但稍后会需要它.

这是一个已知问题还是解决方案却很明显的问题?

更新:我已经尝试过接近问题的某个地方,但是仍然无法解决它.显然我的WriteFile()参数似乎有问题.



to create my thread. Using "WorkerThread" to carry out the writefile operations described earlier in the background.

Additionally, I need to use the Sleep() function while writing it at intervals defined by me. At the moment, the program just quits when trying to use Sleep(). So, I just removed it for the time being but would need it at a later stage.

Is this a known problem or something with a but-obvious solution?

Update: I have sort of tried to reach somewhere close to the problem but still not been able to resolve it. Apparently it looks like there is some problem with my WriteFile() parameters.

WriteFile(theApp.m_hCom,&tBuffer,sizeof(tBuffer),&iBytesWritten,NULL);



它没有正确使用sizeof(tBuffer),因此它崩溃了.我签出了要传递的字符串,该字符串与我需要传递的字符串完全相同,但是如果我按上述方式编写代码(对于WriteFile()),它会使程序崩溃.当我保持字符串长度时,即手动将sizeof(tBuffer)参数设置为14,则程序将运行,但由于缓冲区的总字符串大小为38,因此该命令未执行.
请让我知道.

在此先感谢.



It is not taking the sizeof(tBuffer) properly and because of which it is crashing. I checked out the string to be passed, which is exactly equal to what I need to pass but its crashing out the program if I write the code as done above (for WriteFile()). When I keep the stringlength i.e. manually set the sizeof(tBuffer) parameter to 14, then the program runs but the command does not get executed as the total string size of buffer is 38. Urgently need help on this.

Kindly let me know.

Thanks in advance.

推荐答案

这是一个经典的生产者/消费者问题,需要进行同步才能使其正常工作.就像在现实生活中一样,不可能只用一支铅笔同时书写两者.您需要制定有关共享的规则,或让一个负责记下来的人负责.

请查看链接以获取更多信息:
http://msdn.microsoft.com/en-us/library/aa645740%28v = vs.71%29.aspx [ ^ ]

祝你好运!
This is a classic producer/consumer problem that needs synchronization to make it work properly. It''s just like in real life, it is impossible to write both at the same time using just one pencil. You need to make rules about sharing or make one person responsible for writing stuff down.

Check out the link for more info:
http://msdn.microsoft.com/en-us/library/aa645740%28v=vs.71%29.aspx[^]

Good luck!


好吧,我终于开始工作了.我不确定这是如何工作的,但是经过反复尝试和错误后才起作用.其背后的主要理解仍是未知的.

我通过Visual C ++中提供的调试器"选项运行DLL,使它可以访问命令提示符,并允许它在编译DLL时运行我的脚本以调试程序.似乎Visual C ++不喜欢这个主意,并且尝试这样做时一直崩溃.

当我尝试仅通过python脚本访问DLL时,在这里和那里进行了几次尝试,使它像一个美丽的东西一样工作.似乎Write()函数没有问题,但事实是我是通过调试器运行的,而不是仅使用脚本来尝试.

我知道我在问题中没有提到我的脚本,但是我认为它与我的线程有关,因为我只是线程的初学者.

谢谢你们的答复.真正了解多线程的整个概念确实很有帮助.
Well, I finally got it working. I am not sure how this works but some trial and errors made it work. The main understanding behind this is still unknown.

I was running my DLL via the Debugger option available in Visual C++, giving it an access to the command prompt and allowing it to run my script on compiling the DLL in order to debug my program. It seems like Visual C++ did not like the idea and kept on crashing when trying to do it that way.

When I tried accessing the DLL just by the python script, a few tries here and there made it work like a beauty. Seems like there was no problem with the Write() function but with the fact that I was running it via the Debugger and did not try it with just the script.

I know I haven''t mentioned about my script in my question but I was assuming it had something to do with my thread as I am just a beginner in threading.

Thanks guys for your replies. Was really helpful to actually understand the whole concept of multithreading.


这篇关于尝试进行多线程处理时,WriteFile()崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆