从 DLL 运行后台异步进程
我有一个 DLL,我想将其作为后台进程运行。因此,在 C# 应用程序中,我首先希望在所选的 DLL 中运行此进程,然后让我的应用程序继续在后台运行 DLL 进程。
我看过一些文章和网站,但不确定哪条是最好的选择。
有人有什么想法吗?
干杯
I have a DLL that I would like to run as a background process. So in the C# Application I would first like to run this process in the chosen DLL then let my Application continue with the DLL processes running in the background.
I've seen a few articles and sites but not sure which is the best path to take.
Anyone have any ideas?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DLL不是进程,不能自行运行。但是,您可以设计 Windows 服务并将服务代码包装在 DLL 调用周围,以使该功能可作为连续运行的后台进程使用。
http://msdn.microsoft.com/en -us/library/d56de412(v=vs.80).aspx
DLL is not process and can't be run itself. However you can design a windows service and wrap service code around your DLL call to make the functionality available as a continously running background process.
http://msdn.microsoft.com/en-us/library/d56de412(v=vs.80).aspx
假设您提前选择 .dll(静态绑定):
将 .dll 作为引用添加到您的项目中。然后调用 MyDll.Core.Start();,其中 MyDll 是 .dll 的默认命名空间,Core 是 .cs 类,Start() 是您定义的函数,该函数执行
ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), null);
并且 DoWork 执行您的实际后台工作。假设您希望最终用户从应用程序中选择 .dll(后期绑定):
请参阅 http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx 并将示例中的
GetUserName
更改为Start 以匹配我上面的示例。
Assuming you are choosing the .dll in advance (static binding):
Add the .dll as a reference into your project. Then call
MyDll.Core.Start();
where MyDll is the default namespace of your .dll, and Core is a .cs class, and Start() is a function you defined that doesThreadPool.QueueUserWorkItem(new WaitCallback(DoWork), null);
and DoWork does your actual background work.Assuming you want the end-user to choose the .dll from within the application (late binding):
See http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx and change
GetUserName
in the example toStart
to match my example above.