FolderBrowserDialog를 사용하여 C#에서 폴더를 선택하려고합니다. 처음에는 Thread 예외가 생겼습니다. 그래서 나는 무엇이 잘못되었는지를 알아 보았습니다.하지만 이제는 nother 문제에 머물러 있습니다. 나는 폴더가 선택되었을 때를 알고 싶어한다.FolderBrowserDialog를 스레딩하는 C#
이것은 내가 지금 가지고있는 것입니다.
private void btnWorkingFolder_Click(object sender, EventArgs e)
{
var t = new Thread(SelectFolder);
t.IsBackground = true;
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void SelectFolder()
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
txtWorkFolder.Text = dialog.SelectedPath;
}
}
}
여기에서의 문제는 동일한 스레드가 아닌 txtWorkingFolder에 대한 텍스트 설정을 할 수 없다는 것입니다. txtWorkingFolder에 대한 스레드를 변경하고 싶지 않아, 내 질문에, 어떻게 DialogResult.OK 일단 설정된 새 스레드에서 값을 변경합니까?
EDIT :
이것이 btnWorkingFolder가 Form1에()의 일부가 주이다 :
class sample
{
static void Main(string[] args)
{
Connect2Exchange conn = new Connect2Exchange();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
SECOND EDIT :
발생 다음 예외 주어진 예에서 코드를 시도 후 :
System.Threading.ThreadStateException was unhandled
Message=Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.FolderBrowserDialog.RunDialog(IntPtr hWndOwner)
at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
at System.Windows.Forms.CommonDialog.ShowDialog()
at Mail2DB.Form1.btnWorkingFolder_Click(Object sender, EventArgs e) in C:\Users\marthin\documents\visual studio 2010\Projects\Mail2DB\Mail2DB\Form1.cs:line 44
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Mail2DB.sample.Main(String[] args) in C:\Users\marthin\documents\visual studio 2010\Projects\Mail2DB\Mail2DB\sample.cs:line 26
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
도움이 필요하면 Thx! /Marthin
방금 시도해 본 결과는 같았습니다. ThreadException : ThreadStateException 처리되지 않았습니다. 전체 예외로 내 게시물을 편집했습니다. – Marthin
일반적으로 Program.cs에있는 Main() 메서드에 문제가 있습니다. 하지만 "Mail2DB.sample"과 비슷한 것으로 이동 한 것 같습니다. 일반적으로있는 것처럼 [STAThread] 특성을 가져야합니다. –
@Passant 고맙습니다. 문제가 있습니다.이 프로젝트는 오래된 프로젝트입니다. – Marthin