将VB.net Windows应用程序从Visual Studio 2003升级到VS 2005 [英] Upgrading a VB.net Windows application from Visual Studio 2003 to VS 2005

查看:73
本文介绍了将VB.net Windows应用程序从Visual Studio 2003升级到VS 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以提供帮助.

我将Windows应用程序从Visual Studio 2003升级到2005.运行转换向导并更改/重写了几种方法后,我可以编译该应用程序(在VS 2003中已经可以正常使用)

但是,当运行带有2005年附加的调试器的项目并且选择执行以下代码的打开文件对话框时,出现错误当前线程必须设置为单线程单元(STA)模式,然后才能进行OLE调用.请确保您的Main Function的主函数上已标记STAThreadAttribute.仅当调试器附加到进程时,才会引发此异常." .

堆栈跟踪在System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)在System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window)中显示为

所有者)在System.Windows.Forms.CommonDialog.ShowDialog()中在Pupil_Accounts_Final_App.frmmain.importbob()在C:\ Documents and Settings \ ***** \ My Documents \ Visual Studio 2005 \ *中**** Accounts App V2数据库升级\ frmmain.vb:在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)处的行379
在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象处)状态),在System.Threading.ThreadHelper.ThreadStart()


中,然后进入我的puplicMain类,该类调用按钮所在的表单(frmmain)来调用代码,启动线程化进程以调用该方法以打开对话框,并添加了SingleThreadAttribute,如下面的主代码所示,但是仍然会引发相同的错误.然后,我将此属性添加到按钮单击事件和方法中(再次如下所示),但再次出现相同的问题.

我已经在Web上搜索了好几个小时,试图找到解决方案,但是发现了一篇类似的文章.建议删除BIN目录中的所有旧DLL文件,但我还是尝试这种方法.有谁知道我的问题可能是什么? MSDN和我所有其他常用的调试方法都无法与此配合使用...


打开对话框的方法:

I wonder if anyone can help.

I am upgrading a Windows Application from Visual Studio 2003 to 2005. After running the conversion wizard and changing/rewriting several methods I got the app to compile (already works perfectly in VS 2003)

However when running the project with a Debugger attached in 2005 and I select the open file dialog box which executes the below code I get the error "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.".

The stack trace is shown as

at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
at System.Windows.Forms.CommonDialog.ShowDialog()
at Pupil_Accounts_Final_App.frmmain.importbob() in C:\Documents and Settings\*****\My Documents\Visual Studio 2005\*****Accounts App V2 database upgrade\frmmain.vb:line 379
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


So I then went into my puplicMain class which invokes the form (frmmain) where the button is located for calling the code to start the threaded process to call the method to open the dialog and added the SingleThreadAttribute as shown in the main code below, however the same error is still thrown. I then added this attribute to the button click event and the method (again shown below) but again same problem again.

I have been searching about the Web for several hours trying to find a solution however found one similar article which suggested removing all old DLL files in the BIN directory, I tried this still no luck. Does anyone have any idea what my problem could be? MSDN and all my other usual methods of debugging are just not working with this...


Method for open dialog:

STAThreadAttribute() _<br />    Private Sub importpupils_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimportbob.Click<br />        ''starts threading to make program appear faster and optomise performance<br />        Dim t As New Thread(AddressOf importbob)<br />        t.Name = "Import Bob"<br />        importPupilsThread = True<br />        t.Start()<br />    End Sub<br /><br />    <STAThreadAttribute()> _<br />    Public Sub importbob()<br />        ''this sub open''s a stream reader so bob''s master file can be taken into the program<br />        Dim file As FileStream<br />        Dim inputstream As StreamReader<br />        Dim temp As String<br />        Dim item As ListViewItem<br />        Dim i As Integer<br /><br />        open.Filter = "CSV File (*.csv)|*.csv"<br />        If open.ShowDialog = DialogResult.OK Then<br />            Try<br />                file = New FileStream(open.FileName, FileMode.Open)<br />                inputstream = New StreamReader(file)<br />            Catch ex As Exception<br />                MsgBox("A Exception has occured." + vbCrLf + ex.Message)<br />                Exit Sub<br />            End Try<br />            btnimportbob.Enabled = False<br /><br />            Try<br />                Do While inputstream.Peek > -1<br />                    temp = inputstream.ReadLine<br />                    item = bobslist.Items.Add(temp.Split(",", 11, StringSplitOptions.None)(0))<br />                    For i = 1 To 10<br />                        item.SubItems.Add(temp.Split(",", 11, StringSplitOptions.None)(i).Trim)<br />                    Next<br />                Loop<br />            Catch ex As Exception<br />                MsgBox("A exception has occured. You may have imported the wrong file or the file is corupt" + vbCrLf + ex.Message)<br />                inputstream.Close()<br />                importPupilsThread = False<br />                Exit Sub<br /><br />            End Try<br />            inputstream.Close()<br />        End If<br /><br />        importPupilsThread = False<br />        btnimportseemis.Enabled = True<br />    End Sub



主入口点到程序publicMain



Main entry point to the program publicMain

Module publicmain<br />    Public Const appversion As String = "2.0.0"<br />    Public Const appauthor As String = "*****"<br />    Public Const apptitle As String = "*******"<br />    Public MainForm As frmmain<br />    Public importPupilsThread As Boolean = False<br />    Public importSeemisThread As Boolean = False<br />    Public nextThread As Boolean = False<br /><br /><br />    '' Main entry point in to application<br />    '' Creates new instance of the main form then shows the dialog<br />    <STAThreadAttribute()> _<br />    Sub Main()<br />        MainForm = New frmmain()<br />        MainForm.ShowDialog()<br />    End Sub

推荐答案



我认为您不能为此使用STAThreadAttributes目的.以下是两种可能的解决方案:
1.在启动线程之前是否进行用户交互;
2.或在调用Thread.Start

:之前设置Thread.ApartmentState).

Hi,

I don''t think you can use STAThreadAttributes for that purpose. Here are two possible solutions:
1. do you user interaction before you start the thread;
2. or set Thread.ApartmentState before calling Thread.Start

:)


这篇关于将VB.net Windows应用程序从Visual Studio 2003升级到VS 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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