Ecco il messaggio di errore che sto riceve quando si tenta di aprire un OpenFileDialog nel mio programma:STAThread manca, ma è lì
"thread corrente deve essere impostato su singolo appartamento filo (STA) modalità prima che OLE le chiamate possono essere effettuate. Assicurarsi che la tua funzione principale ha STAThreadAttribute contrassegnato su di esso.Questa eccezione è solo generato se un debugger è collegato a il processo. "
Il problema con questo messaggio di errore è che il mio metodo principale ha l'attributo STAThread collegato ad esso. Sono in perdita su come gestirlo. Come posso aggiungere qualcosa se è già lì. Raddoppiare non è una buona opzione, e ho provato a cancellarlo, a costruire l'app, aggiungerla e ricostruirla senza successo. Io proprio non capisco.
private void btnOldFind_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
openFileDialog1.FileName = "";
openFileDialog1.ShowHelp = false;
openFileDialog1.AutoUpgradeEnabled = true;
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Filter = "Microsoft Installer (*.msi)|*.msi|All Files (*.*)|*.* ";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBoxOldInstallation.Text = openFileDialog1.FileName;
}
}
e il metodo principale è:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
E non threading è fatto in modo esplicito. Solo un programma piuttosto semplice per essere onesti.
modalità EDIT2 ::
Qui è il messaggio di errore completo che include stack di chiamate
System.Threading.ThreadStateException è stata gestita Messaggio = "thread corrente deve essere impostato su singolo appartamento filo (STA) prima che le chiamate OLE possano essere fatte, assicurati che la tua funzione principale abbia scritto STAThreadAttribute. Questa eccezione viene sollevata solo se un debugger è collegato al processo. " Fonte = "System.Windows.Forms" StackTrace: a System.Windows.Forms.FileDialog.RunDialog (IntPtr hWndOwner) a System.Windows.Forms.CommonDialog.ShowDialog (proprietario IWin32Window) a System.Windows.Forms .CommonDialog.ShowDialog() in MSI_Comparison_GUI.Form1.btnOldFind_Click (mittente dell'oggetto, EventArgs e) in c: \ tfs \ DocuWare .NET \ DocuWare NewGen \ src \ Tools \ MSI_Comparison \ MSI_Comparison_GUI \ Form1.cs: riga 70 in Sistema .Windows.Forms.Control.OnClick (EventArgs e) in System.Windows.Forms.Button.OnClick (EventArgs e) in System.Windows.Forms.Button.OnMouseUp (MouseEventArgs mevent) in System.Windows.Forms. Control.WmMouseUp (Messaggio & m, MouseButto pulsante ns, Int32 scatto) a System.Windows.Forms.Control.WndProc (Messaggio & m) a System.Windows.Forms.ButtonBase.WndProc (Messaggio & m) a System.Windows.Forms.Button.WndProc (Messaggio & m) a System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Messaggio & m) a System.Windows.Forms.Control.ControlNativeWindow.WndProc (Messaggio & m) a System.Windows.Forms.NativeWindow. DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) a System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG & msg) a System.Windows.Forms.Ap plication.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop (Int32 dwComponentID, Int32 motivo, Int32 pvLoopData) in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (motivo Int32, contesto ApplicationContext) a System.Windows.Forms.Application.ThreadContext.RunMessageLoop (motivo Int32, contesto ApplicationContext) a System.Windows.Forms.Application.Run (Form mainForm) a MSI_Comparison_GUI.Program.Main() in c: \ tfs \ DocuWare .NET \ DocuWare NewGen \ src \ Tools \ MSI_Comparison \ MSI_Comparison_GUI \ Program.cs: riga 18 in System.AppDomain._nExecuteAssembly (Assembly Assembly, String [] args) in System.AppDomain .ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args) a Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() a System.Threading.ThreadHelper.ThreadStart_Context (stato oggetto) a System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, ContextCallback callback, Object state) a System.Threading.ThreadHelper.ThreadStart() InnerException:
Puoi mostrarci il codice, come crei il dialogo? E hai creato un altro thread nel tuo programma? –
è stato aggiunto per il tuo piacere di lettura: P – Adkins
Sei sicuro di essere eseguito nel thread principale a quel punto e che openFileDialog1 è stato creato nel thread principale? –