Help with Multi threading

V

Vb2Machines

Hello to all,
I have a need to make a progress bar respond while a query is
processing. I looked into the multi threading and I've had mixed results.

I've tried a new thread with no success and I've tried a delegate thread
with limited results. When I use the delegate method and call the
begininvoke I see the progress bar update one time then stop and the query
begins to execute at that time but the progress bar stops updating until the
query is done.

Help please. Here are snippets of the code

Try

RunProgressBar(True)

Application.DoEvents()

Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

dsSku = SqlHelper.ExecuteDataset(g_AppParams.OEESqlDbConnString,
CommandType.StoredProcedure, SQL, sqlParam)

Me.Cursor = System.Windows.Forms.Cursors.Default

If Not dsSku.Tables(0).Rows.Count > 0 Then MsgBox("No Data present for
this line or machine for this time period!", MsgBoxStyle.Exclamation)

dgSku.DataSource = dsSku.Tables(0)

RunProgressBar(False)

Catch ex As Exception

Dim oEvent As New cEventLog

oEvent.WriteToEventLog(ex.Message & " -- rdoLastHour_CheckedChanged ",
1111)

End Try

Private Sub RunProgressBar(ByVal BarState As Boolean)

Me.prgFetchData.Visible = True

'Dim t As New Thread(AddressOf UpdateProgressBar) 'Creates the new
thread

'If BarState Then t.Start() Else t.Abort()

Dim td As New TaskDelegate(AddressOf UpdateProgressBar)

If BarState Then td.BeginInvoke(Nothing, Nothing) Else
td.EndInvoke(Nothing) 'Runs on a worker thread from the pool

End Sub

Private Sub UpdateProgressBar()

'Slowly increment the progress bar

Dim i As Integer

For i = 1 To 10

Me.prgFetchData.Value += 10

Thread.CurrentThread.Sleep(500) 'half-second delay

Me.prgFetchData.Refresh()

Next

End Sub

Delegate Sub TaskDelegate()

Am I missing something completely obvious?

thanks in advance,
 
J

Jon Skeet [C# MVP]

Vb2Machines said:
I have a need to make a progress bar respond while a query is
processing. I looked into the multi threading and I've had mixed results.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
I've tried a new thread with no success and I've tried a delegate thread
with limited results. When I use the delegate method and call the
begininvoke I see the progress bar update one time then stop and the query
begins to execute at that time but the progress bar stops updating until the
query is done.

Help please. Here are snippets of the code

Snippets don't really help very much, unfortunately.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(Unfortunately I'm about to be off the newsgroups for a week or so, but
I'm sure someone will pick the thread up.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top