多线程问题
我正在尝试为游戏制作一个服务器程序,我需要在 Windows 窗体应用程序中运行 3 个额外的线程。所有线程都需要能够访问相同的变量(线程1:将从我的数据库中提取用户命令并执行它们,线程2:将更新所有用户数据,线程3:将发送所有新变量返回数据库。)我已经研究了后台线程,我不确定这是否会做我希望他们做的事情(他们看起来可以运行我的代码,但看起来他们会有一个困难对所有的时间使用相同的变量线程。)
所以我想我的问题是,这是否可能以及实现此目的的最佳方法是什么,后台工作人员和调用可以解决问题吗?我这样做会遇到什么问题,任何人都有更好的想法。
顺便说一句,我有另一个相关的问题,我尝试了很多次仅使用简单的线程来完成此任务。现在,据我了解,在另一个线程上完成所有工作不应锁定我的用户界面,但确实如此。也许我以错误的方式执行线程,我的代码看起来像
dim newthread as new system.threading.thread(addressof runmycode)
newthread.start()`
这样的代码不准确,但为什么使用这样的线程会锁定 UI?不应该单独运行吗?
I'm trying to make a server program for a game, I need to run 3 extra threads in a windows form app. all the threads needs to be able to access the same variables, (thread1: is going to be pulling user commands from my database and executing them, thread2: is going to be updating all the users data and thread3: is going to be sending all the new variables back to the database.) I've looked into background threads and I'm not sure if thats going to do what i want them todo (they look like they could run my code but it looks like they would have a hard time working with the same variables for the all the threads.)
So i guess my question is, is this possible and what is best way to accomplish this, would background workers and invoke do the trick? what problems would i be looking at doing that, anyone have any better idea.
On a side note i had another question thats related ive tried a bunch of times to accomplish this using just simple threads. Now from what i understand doing all of my work on another thread should not lock up my ui, however it does. maybe im doing thread the wrong way, my code looks something like
dim newthread as new system.threading.thread(addressof runmycode)
newthread.start()`
this code isn't exact but why would using threading like this lock up the UI? Shouldn't it be running separately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建要“线程化”的对象类的“新”实例并公开您想要调用的函数,例如
You need to create a 'new' instance of the object class to be 'threaded' and expose the function you want to call, e.g.
这是我不久前写的一个教程,它将引导您完成在 VB2008 中创建基本的多线程程序。使用 VB2010,您可以根据需要删除委托,但想法几乎相同。
创建一个简单的多线程VB.Net 应用程序
Here's a tutorial I wrote a little while back that walks you through creating a basic multi-threading program in VB2008. With VB2010 you can remove the delegates if you want but the idea is pretty much the same.
Creating a simple multi-threaded VB.Net application