VB.Net (or C #) 2008 Multithreaded Import

I want to create a multi-threaded text import tool (usually CSV in SQL Server 2005) and would like to do it in VB.NET, but I am not against C #. I have a VS 2008 test and just don't know where to start. Can someone point me where I can watch and play with the source of a VERY simple multithreaded application for VS 2008?

Thanks!

+2
source share
3 answers

A related DevX article is from 2001 and the .Net Framework 1.1, but today .Net Framework 2.0 provides the BackgroundWorker class. This is the recommended thread class if your application contains a foreground interface component.

From Themes and MSDN Threads :

If you need to run a background thread that interacts with the user interface, the .NET Framework 2.0 provides a BackgroundWorker component that communicates using events with a cross-thread marshaling user interface thread.

This example from the MSW BackgroundWorker Class displays the background task,% progress, and undo option. (The example is longer than the DevX sample, but has much 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 
+2
source

This is a great article:

http://www.devx.com/DevX/10MinuteSolution/20365

In particular:

 Dim t As Thread t = New Thread(AddressOf Me.BackgroundProcess) t.Start() Private Sub BackgroundProcess() Dim i As Integer = 1 Do While True ListBox1.Items.Add("Iterations: " + i) i += 1 Thread.CurrentThread.Sleep(2000) Loop End Sub 
+4
source

About the best document I've ever found was http://www.albahari.com/threading/

If I can, the problem with simple examples is that they are often too simple. After you go through the counting or sorting in the background, you usually need to update the user interface or the like, and there are some errors. Similarly, you rarely have to deal with resource conflicts in simple examples, and when threads degrade gracefully, when a resource is inaccessible (like a Db connection), a thought is required.

Conceptually, you need to decide how you are going to distribute your work in threads and how much you need. There, the overhead associated with managing threads and some mechanisms uses a common thread pool, which can be subject to the very conflict of resources (for example, every time you run a program that simply displays an empty form, how many threads you see in the task manager).

So, for your case, the threads performing the actual load should report otherwise if they were completed, if they failed (and what happened with the error). The controller should be able to cope with these processes and manage the start / stop processes, etc.

Finally (almost), assuming that creating something multithreaded will increase performance is not always true. If, for example, you split the file into segments, but it must move along the low-speed channel (ADSL), you are limited by external forces, and no amount of spoofed streams will cost. The same can be applied to database updates, web requests, anything that requires large amounts of disk I / O, etc.

Despite all this, I am not a prophet of death. The links here are more than sufficient to help you achieve what you want, but remember that one of the reasons why threads seem complicated is that it can be :)

If you want more control than BackgroundWorker / Threadpool but donโ€™t want to do everything yourself, there are at least two very good niche stream libraries that knock around the place (Wintellect and PowerThreading).

Greetings

Simon

+1
source

Source: https://habr.com/ru/post/1277190/


All Articles