多线程应用程序中的 SetCurrentDirectory [英] SetCurrentDirectory in multi-threaded application

查看:35
本文介绍了多线程应用程序中的 SetCurrentDirectory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 SetCurrentDirectory 不应该用于多线程应用程序,因为当前目录在进程中的所有线程之间共享.

I understand SetCurrentDirectory shouldn't be used in a multithreaded application since the current directory is shared between all threads in the process.

考虑到这一点,设置目录的最佳方法是什么.通过在打开文件时包含完整路径名而不是首先使用 SetCurrentDirectory 导航到它们,通常可以避免设置目录,但这是唯一的解决方案吗?

What is the best approach to setting the directory with this in mind. It can mostly be avoided setting the directory by including the full pathname when opening files instead of first navigating to them with SetCurrentDirectory, but is this the only solution?

推荐答案

我以前遇到过这个问题.

I've encountered this problem before.

任何需要当前目录的概念来支持相对路径或搜索的对象(例如构建工具)都有一个成员属性,它以其当前"路径维护,然后构建完整路径以打开/创建/搜索.

Any object that needs the concept of a current directory to support relative paths or searching (e.g. a build tool) has a member property that it maintains with its "current" path, then build the full path to open/create/search.

CurrentPath 的初始值可以在应用程序的加载阶段检索一次,例如main()、WinMain()、DllInit() 等通过 GetCurrentDirectory 存储在全局中.之后,Win32 版本将被忽略.

The initial value for CurrentPath can be retrieved once during the application's load phase, e.g. main(), WinMain(), DllInit(), etc. via GetCurrentDirectory and stored in a global. After that the Win32 version is ignored.

OPENFILENAME 结构有一个初始目录成员,因此文件打开/保存对话框不必使用 Win32 当前目录.

The OPENFILENAME structure has an initial directory member, so file open/save dialogs don't have to use the Win32 current directory.

这篇关于多线程应用程序中的 SetCurrentDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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