VB.Net(或C#)2008多线程导入 [英] VB.Net (or C#) 2008 Multi Threaded Import
问题描述
我希望构建一个多线程文本导入工具(通常是CSV到SQL Server 2005),并希望在VB.NET中这样做,但我不反对C#。我有VS 2008试用版,只是不知道从哪里开始。任何人都可以指出我可以看到的方向并使用VS 2008的非常简单多线程应用程序的来源吗?
I am looking to build a multi-threaded text import facility (generally CSV into SQL Server 2005) and would like to do this in VB.NET but I am not against C#. I have VS 2008 trial and just dont know where to begin. Can anyone point me in the direction of where I can look at and play with the source of a VERY simple multi-threaded application for VS 2008?
谢谢!
推荐答案
引用的 DevX 文章来自2001和.Net Framework 1.1,但是今天.Net Framework 2.0提供了 BackgroundWorker 上课。如果您的应用程序包含前台UI组件,那么这是推荐的线程类。
The referenced DevX article is from 2001 and .Net Framework 1.1, but today .Net Framework 2.0 provides the BackgroundWorker class. This is the recommended threading class if your application includes a foreground UI component.
来自 MSDN线程和线程:
如果你需要运行后台线程
与用户界面交互,
.NET Framework版本2.0
提供了一个BackgroundWorker组件
,它使用事件进行通信,并使用
跨线程封送到
用户界面线程。
If you need to run background threads that interact with the user interface, the .NET Framework version 2.0 provides a BackgroundWorker component that communicates using events, with cross-thread marshaling to the user-interface thread.
此示例来自 MSDN BackgroundWorker类显示后台任务,进度%和取消选项。 (该示例比DevX示例更长,但功能更多。)
This example from MSDN BackgroundWorker Class shows a background task, progress %, and cancel option. (The example is longer than the DevX sample, but has a lot more functionality.)
Imports System.ComponentModel
Partial Public Class Page
Inherits UserControl
Private bw As BackgroundWorker = New BackgroundWorker
Public Sub New()
InitializeComponent()
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
AddHandler bw.DoWork, AddressOf bw_DoWork
AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
End Sub
Private Sub buttonStart_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
If Not bw.IsBusy = True Then
bw.RunWorkerAsync()
End If
End Sub
Private Sub buttonCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
If bw.WorkerSupportsCancellation = True Then
bw.CancelAsync()
End If
End Sub
Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
For i = 1 To 10
If bw.CancellationPending = True Then
e.Cancel = True
Exit For
Else
' Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500)
bw.ReportProgress(i * 10)
End If
Next
End Sub
Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
If e.Cancelled = True Then
Me.tbProgress.Text = "Canceled!"
ElseIf e.Error IsNot Nothing Then
Me.tbProgress.Text = "Error: " & e.Error.Message
Else
Me.tbProgress.Text = "Done!"
End If
End Sub
Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
Me.tbProgress.Text = e.ProgressPercentage.ToString() & "%"
End Sub
End Class
这篇关于VB.Net(或C#)2008多线程导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!