Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42482

Multithreaded application - access to method's objects

$
0
0
Hi All,

I'm new in multithreaded programming and I'd like to ask you for help.
I want to develop application which needs to execute reports. I have to use multithreading functionality because execution one of the report can take a lot of time and others can't wait for process to finish.
I'm using external dll to connect to datasource, create session for specified project and then to execute report.

Below is the code I'm using:

In the loop I create and start thread with passed object.

Code:

Sub Main()
        Dim arr(2) As String
        Dim arrD(2) As String

        arr(1) = "0A9EBE87468B751C3663818889B10D73"
        arr(2) = "8AC5B5784154EBD2D585C38BF7F7E673"

        arrD(2) = "Project1"
        arrD(1) = "Project2"

      For i = 1 To 2
            Dim myThread As New Threading.Thread(AddressOf RefreshCube)

            Dim obj = New CubeData()
            obj.ID = arr(i)
            obj.DataSourceName = "192.168.0.11"
            obj.login = "administrator"
            obj.pwd = ""
            obj.ProjectName = arrD(i)

            myThread.Start(obj)
        Next
End sub

The problem occurs in RefreshCube method.
When first thread tries to init DataSource than I get message that Data Source can't be initialized. I think it's because the second thread in the meantime execute the same method and creates new object of DataSource and the first thread tries to initialize empty object.

Code:

Sub RefreshCube(Cube As CubeData)
Dim DataSource As DSSDataSource
dim retVal as String

    Try
            DataSource = New DSSDataSource()
            DataSource.Type = EnumDSSDataSourceType.DssDataSourceTypeServer
            DataSource.Location = Cube.DataSourceName
            DataSource.Mode = EnumDSSConnectionMode.DssConnectionModeServerAccess

            DataSource.AuthMode = EnumDSSAuthModes.DssAuthStandard
            DataSource.login = Cube.login
            DataSource.Passwd = Cube.pwd
            DataSource.Init() '<-- problem appears when thread tries to Init Datasorce

          'code for creating session and executing report
              ................................................

        Catch ex As Exception
            retVal = ex.Message
            Console.WriteLine(ex.Message)
        End Try
End Sub

The question is how I can make my code to execute reports asynchronusly without this conflict?

Thanks,
Lukasz

Viewing all articles
Browse latest Browse all 42482

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>