views:

59

answers:

2

Hello,

I start making program that will burn CD/DVDs, and everything is okay. I found way to burn with IMAPI2 API, but now I have problem: I can't get progress bar of that burning. Here is code:

Dim CDD1 As New IMAPI2.MsftDiscMaster2
Dim CDD2 As New IMAPI2.MsftDiscRecorder2

Dim FSI As New IMAPI2FS.MsftFileSystemImage
Dim CDD3 As New IMAPI2.MsftDiscFormat2Data

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Index = 0
    Dim UniqueID = ""
    Dim Directory
    Dim Path = "C:\lll"
    Dim result
    Dim Stream

    Label1.Text = "----- Started -----."

    UniqueID = CDD1.Item(Index)
    Label1.Text = Label1.Text & vbCrLf & "ID found: " & UniqueID

    CDD2.InitializeDiscRecorder(UniqueID)
    Label1.Text = Label1.Text & vbCrLf & "Recorder selected!"

    Directory = FSI.Root
    Label1.Text = Label1.Text & vbCrLf & "Directory is here: " & Directory.ToString

    CDD3.Recorder = CDD2
    Label1.Text = Label1.Text & vbCrLf & "Recorder 2 selected!"

    CDD3.ClientName = "IMAPI2 TEST"
    Label1.Text = Label1.Text & vbCrLf & "Client Name Selected!"

    FSI.ChooseImageDefaults(CDD2)
    Label1.Text = Label1.Text & vbCrLf & "Default selected!"

    Directory.AddTree(Path, False)
    Label1.Text = Label1.Text & vbCrLf & "Directory added!"

    result = FSI.CreateResultImage()
    Stream = result.ImageStream

    Label1.Text = Label1.Text & vbCrLf & "Writing content to disc..."

    If (CDD3.IsCurrentMediaSupported(CDD2) = True) Then
        If (CDD3.IsRecorderSupported(CDD2) = True) Then

            CDD3.Write(Stream)

        Else
            MsgBox("Not Suported Recorder!")
        End If
    Else
        MsgBox("Not Suported Media!")
    End If

    Label1.Text = Label1.Text & vbCrLf & "----- Finished -----"

End Sub

When command

CDD3.Write(Stream)

is triggered, program freeze, and don't respond until data is burned completely.

Is there any way to stop this, to stop program freezing and enabling progress bar?

Thanks.

A: 

You need to use threading. So in your button click event handler you start off a new thread that does the actual burning and while that's going on in it's separate thread, the main thread can continue to update the GUI (including your progress bar).

See Thread.Start for a simple sample and if you want further information I'd suggest starting here: Managed Threading

ho1
Thank you for your answer.But now, I have new problem.When I start new Thread, it's require to do all what I have done in Form1, to select drive, name, recorder, etc. because it can't recognize it from Form1.CDD3.Write().Even if I do all this in Threat, I still can't get any data from it, because it require new registration of driver data, but that is not allowed.Can you please write me how to make this? Just how to pass data and objects from main Form to Class and back.Thank you.
@user: If you just need to send some data from the main thread to the worker thread, use the version of the `Thread` constructor that takes a `ParameterizedThreadStart`, just create a class to hold the data, create an instance off it and send that instance in as a parameter. See here for more information: http://msdn.microsoft.com/en-us/library/1h2f2459.aspx
ho1
You can also use that object instance to send data back to the main thread if you need to. Just make it contain some more members to contain that data as well and make the main thread keep a reference of it. However, if you're using the same data from multiple threads, you have to deal with synchronization as can be seen here: http://msdn.microsoft.com/en-us/library/z8chs7ft.aspx
ho1
A: 

Thank you. I figure it out that this can be done with BackgroundWorker element too. And it's not freezing, but still I can't get percent of done work.

I need to know how much sectors/data is burned to disc in every second while it's burning. Is it reliable way to count by WriteSpeed?

Get file size that is burning, and then divide it by Writing speed and calculate how much time it will take to burn it.

Is it reliable way?