473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multithread Revisted

On the main thread I have a status bar in a Windows form that I want to
update when a long database process runs. The database process runs on a
separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long routine,
starting a new thread each time. But the problem is that I need the thread
to finish before code execution continues. The main reason this does not
just run on the same thread is that I need to get the darn status bar
updated, but the UI freezes up. I tried using t.Join() which does wait for
the thread to finish, but it does not allow the UI to be freed up. And I
tried setting a global variable to detect when the thread is done, but the
code after the thread just runs in a Do Loop until the variable is set,
which also freezes up the UI. Any ideas of how to get this working?
Derek


Jul 5 '06 #1
10 1161
Hello Derek,

The reason the UI becomes unresponsive is that the long database operations
block the message pump. Instead of spinning off new threads, which are unneeded,
just massage the message pump immediately after updating the status bar.
(See MSDN documentation for application.DoEvents)

-Boo
On the main thread I have a status bar in a Windows form that I want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that I
need the thread to finish before code execution continues. The main
reason this does not just run on the same thread is that I need to get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up the
UI. Any ideas of how to get this working?

Derek

Jul 5 '06 #2
On Tue, 4 Jul 2006 17:29:29 -0700, Derek Hart wrote:
The database process will be called about 10 times in one long routine,
starting a new thread each time. But the problem is that I need the thread
to finish before code execution continues. The main reason this does not
just run on the same thread is that I need to get the darn status bar
updated, but the UI freezes up. I tried using t.Join() which does wait for
the thread to finish, but it does not allow the UI to be freed up. And I
tried setting a global variable to detect when the thread is done, but the
code after the thread just runs in a Do Loop until the variable is set,
which also freezes up the UI. Any ideas of how to get this working?
Do all your database stuff in a single thread and use Control.Invoke or
Control.BeginInvoke to update your progress bar. These methods will
marshall the call to the UI thread for you. A lot cleaner than these ugly
hacks with Application.DoEvent loops dating from VB6.
Jul 5 '06 #3
Hello derek

so you want a responsive progressbar huh :-)

i use the following

Delegate Sub AsynchMethodInvoker()
Public Sub SetPbValue(ByVal Count As Integer)
If Me.pbMain.InvokeRequired Then
Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)
Me.Invoke(d, New Object() {Count})
Else
Me.pbMain.Value = Count
End If
End Sub

now just call the method from your other thread and it will just work fine
:-)

ofcourse is pbmain the progress bar
hope this helps
Michel Posseth [MCP]



"Mehdi" wrote:
On Tue, 4 Jul 2006 17:29:29 -0700, Derek Hart wrote:
The database process will be called about 10 times in one long routine,
starting a new thread each time. But the problem is that I need the thread
to finish before code execution continues. The main reason this does not
just run on the same thread is that I need to get the darn status bar
updated, but the UI freezes up. I tried using t.Join() which does wait for
the thread to finish, but it does not allow the UI to be freed up. And I
tried setting a global variable to detect when the thread is done, but the
code after the thread just runs in a Do Loop until the variable is set,
which also freezes up the UI. Any ideas of how to get this working?

Do all your database stuff in a single thread and use Control.Invoke or
Control.BeginInvoke to update your progress bar. These methods will
marshall the call to the UI thread for you. A lot cleaner than these ugly
hacks with Application.DoEvent loops dating from VB6.
Jul 5 '06 #4

if you want a responsive UI , one that is capable of terminating a hughe
update for instance , and the ability to perform simultanious tasks like
updating a progressbar that gives reall info , and a UI that repainst itself
after moving the form etc etc well then you can only acomplish this with
multithreading

The dissadvantage of using asynchronous code is that it is usualy slower as
synchronous code , however how slow is it when the user terminates the
program because he believes it doesn`t run at all because it is unresponsive

application.doevents is overdone in almost all situations you could better
call a refresh on the control
regards

Michel Posseth [MCP]
"GhostInAK" wrote:
Hello Derek,

The reason the UI becomes unresponsive is that the long database operations
block the message pump. Instead of spinning off new threads, which are unneeded,
just massage the message pump immediately after updating the status bar.
(See MSDN documentation for application.DoEvents)

-Boo
On the main thread I have a status bar in a Windows form that I want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that I
need the thread to finish before code execution continues. The main
reason this does not just run on the same thread is that I need to get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up the
UI. Any ideas of how to get this working?

Derek


Jul 5 '06 #5
ahum :-(

copied the wrong delegate
Delegate Sub AsynchMethodInvoker()
should be

Delegate Sub CrossThreadInvParaMB(ByVal val As Integer)

sorry ;-)
"M. Posseth" wrote:
Hello derek

so you want a responsive progressbar huh :-)

i use the following

Delegate Sub AsynchMethodInvoker()
Public Sub SetPbValue(ByVal Count As Integer)
If Me.pbMain.InvokeRequired Then
Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)
Me.Invoke(d, New Object() {Count})
Else
Me.pbMain.Value = Count
End If
End Sub

now just call the method from your other thread and it will just work fine
:-)

ofcourse is pbmain the progress bar
hope this helps
Michel Posseth [MCP]



"Mehdi" wrote:
On Tue, 4 Jul 2006 17:29:29 -0700, Derek Hart wrote:
The database process will be called about 10 times in one long routine,
starting a new thread each time. But the problem is that I need the thread
to finish before code execution continues. The main reason this does not
just run on the same thread is that I need to get the darn status bar
updated, but the UI freezes up. I tried using t.Join() which does wait for
the thread to finish, but it does not allow the UI to be freed up. And I
tried setting a global variable to detect when the thread is done, but the
code after the thread just runs in a Do Loop until the variable is set,
which also freezes up the UI. Any ideas of how to get this working?
Do all your database stuff in a single thread and use Control.Invoke or
Control.BeginInvoke to update your progress bar. These methods will
marshall the call to the UI thread for you. A lot cleaner than these ugly
hacks with Application.DoEvent loops dating from VB6.
Jul 5 '06 #6
Michel
application.doevents is overdone in almost all situations you could better
call a refresh on the control
But you see than in my idea in the by the OPr explained sample only the
control, which is AFAIK done by the progressbar itself.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht
news:AC**********************************@microsof t.com...
>
if you want a responsive UI , one that is capable of terminating a hughe
update for instance , and the ability to perform simultanious tasks like
updating a progressbar that gives reall info , and a UI that repainst
itself
after moving the form etc etc well then you can only acomplish this with
multithreading

The dissadvantage of using asynchronous code is that it is usualy slower
as
synchronous code , however how slow is it when the user terminates the
program because he believes it doesn`t run at all because it is
unresponsive

application.doevents is overdone in almost all situations you could better
call a refresh on the control
regards

Michel Posseth [MCP]
"GhostInAK" wrote:
>Hello Derek,

The reason the UI becomes unresponsive is that the long database
operations
block the message pump. Instead of spinning off new threads, which are
unneeded,
just massage the message pump immediately after updating the status bar.
(See MSDN documentation for application.DoEvents)

-Boo
On the main thread I have a status bar in a Windows form that I want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that I
need the thread to finish before code execution continues. The main
reason this does not just run on the same thread is that I need to get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up the
UI. Any ideas of how to get this working?

Derek



Jul 5 '06 #7
Not it shouldn`t be necesary ,

i wrote a nice demo for the TS and for the sceptic people :-)

take a form throw 2 buttons on it and a progressbar

call the progressbar pbmain
now copy paste this :

Imports System.Threading
Public Class Form1
Private Mvar_Cancell As Boolean
Friend Property Cancell() As Boolean
Get
Return Mvar_Cancell
End Get
Set(ByVal value As Boolean)
Mvar_Cancell = value
Me.Button1.Enabled = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Cancell = False
With pbMain
.Value = 0
.Maximum = 100
.Minimum = 0
End With

Dim AsyncProcess As New AsynchMethodInvoker(AddressOf Me.pbLoop)
AsyncProcess.BeginInvoke(Nothing, Nothing)

AsyncProcess = New AsynchMethodInvoker(AddressOf Me.DoSomething)
AsyncProcess.BeginInvoke(Nothing, Nothing)

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Cancell = True
End Sub

Private Sub DoSomething()
Do While Not Me.Cancell
Debug.WriteLine("hello world")
Loop
End Sub
Delegate Sub CrossThreadInvParaMB(ByVal val As Integer)
Private Sub pbLoop()
Dim i As Integer
Do While Not Me.Cancell
Thread.Sleep(50)
i += 1
If i = 101 Then i = 0
SetPbValue(i)
Loop
i = 100
SetPbValue(i)
End Sub
Delegate Sub AsynchMethodInvoker()
Public Sub SetPbValue(ByVal Count As Integer)
If Me.pbMain.InvokeRequired Then
Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)
Try
Me.Invoke(d, New Object() {Count})
Catch ex As Exception

End Try
Else
Me.pbMain.Value = Count
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Cancell = True
End Sub
End Class
i hope this gives you all an idea in what i mean ,,, the method that is now
updating the progressbar runs in a seperate thread to make it even nicer i
run another worker thread wich prints hello world statements to the debug
window

these threads can comunicate to the progressbar through the SetPbValue method
i hope this gives the TS some new input to solve his problem


"Cor Ligthert [MVP]" wrote:
Michel
application.doevents is overdone in almost all situations you could better
call a refresh on the control
But you see than in my idea in the by the OPr explained sample only the
control, which is AFAIK done by the progressbar itself.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht
news:AC**********************************@microsof t.com...

if you want a responsive UI , one that is capable of terminating a hughe
update for instance , and the ability to perform simultanious tasks like
updating a progressbar that gives reall info , and a UI that repainst
itself
after moving the form etc etc well then you can only acomplish this with
multithreading

The dissadvantage of using asynchronous code is that it is usualy slower
as
synchronous code , however how slow is it when the user terminates the
program because he believes it doesn`t run at all because it is
unresponsive

application.doevents is overdone in almost all situations you could better
call a refresh on the control
regards

Michel Posseth [MCP]
"GhostInAK" wrote:
Hello Derek,

The reason the UI becomes unresponsive is that the long database
operations
block the message pump. Instead of spinning off new threads, which are
unneeded,
just massage the message pump immediately after updating the status bar.
(See MSDN documentation for application.DoEvents)

-Boo

On the main thread I have a status bar in a Windows form that I want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that I
need the thread to finish before code execution continues. The main
reason this does not just run on the same thread is that I need to get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up the
UI. Any ideas of how to get this working?

Derek



Jul 5 '06 #8
My dear Michel,

I only did mean that
>>application.doevents is overdone in almost all situations you could better call a refresh on the control
I see that nowhere in your code.

If that is done to refresh and the form as it is hidden by another program, than it will not help, so it is neither a solution.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht news:99**********************************@microsof t.com...
Not it shouldn`t be necesary ,

i wrote a nice demo for the TS and for the sceptic people :-)

take a form throw 2 buttons on it and a progressbar

call the progressbar pbmain
now copy paste this :

Imports System.Threading
Public Class Form1
Private Mvar_Cancell As Boolean
Friend Property Cancell() As Boolean
Get
Return Mvar_Cancell
End Get
Set(ByVal value As Boolean)
Mvar_Cancell = value
Me.Button1.Enabled = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Cancell = False
With pbMain
.Value = 0
.Maximum = 100
.Minimum = 0
End With

Dim AsyncProcess As New AsynchMethodInvoker(AddressOf Me.pbLoop)
AsyncProcess.BeginInvoke(Nothing, Nothing)

AsyncProcess = New AsynchMethodInvoker(AddressOf Me.DoSomething)
AsyncProcess.BeginInvoke(Nothing, Nothing)

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Cancell = True
End Sub

Private Sub DoSomething()
Do While Not Me.Cancell
Debug.WriteLine("hello world")
Loop
End Sub
Delegate Sub CrossThreadInvParaMB(ByVal val As Integer)
Private Sub pbLoop()
Dim i As Integer
Do While Not Me.Cancell
Thread.Sleep(50)
i += 1
If i = 101 Then i = 0
SetPbValue(i)
Loop
i = 100
SetPbValue(i)
End Sub
Delegate Sub AsynchMethodInvoker()
Public Sub SetPbValue(ByVal Count As Integer)
If Me.pbMain.InvokeRequired Then
Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)
Try
Me.Invoke(d, New Object() {Count})
Catch ex As Exception

End Try
Else
Me.pbMain.Value = Count
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Cancell = True
End Sub
End Class
i hope this gives you all an idea in what i mean ,,, the method that is now
updating the progressbar runs in a seperate thread to make it even nicer i
run another worker thread wich prints hello world statements to the debug
window

these threads can comunicate to the progressbar through the SetPbValue method
i hope this gives the TS some new input to solve his problem


"Cor Ligthert [MVP]" wrote:
>Michel
application.doevents is overdone in almost all situations you could better
call a refresh on the control
But you see than in my idea in the by the OPr explained sample only the
control, which is AFAIK done by the progressbar itself.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht
news:AC**********************************@microso ft.com...
>
if you want a responsive UI , one that is capable of terminating a hughe
update for instance , and the ability to perform simultanious tasks like
updating a progressbar that gives reall info , and a UI that repainst
itself
after moving the form etc etc well then you can only acomplish this with
multithreading

The dissadvantage of using asynchronous code is that it is usualy slower
as
synchronous code , however how slow is it when the user terminates the
program because he believes it doesn`t run at all because it is
unresponsive

application.doevents is overdone in almost all situations you could better
call a refresh on the control
regards

Michel Posseth [MCP]
"GhostInAK" wrote:

Hello Derek,

The reason the UI becomes unresponsive is that the long database
operations
block the message pump. Instead of spinning off new threads, which are
unneeded,
just massage the message pump immediately after updating the status bar.
(See MSDN documentation for application.DoEvents)

-Boo

On the main thread I have a status bar in a Windows form that I want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that I
need the thread to finish before code execution continues. The main
reason this does not just run on the same thread is that I need to get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up the
UI. Any ideas of how to get this working?

Derek



Jul 5 '06 #9

Hi Cor ,,,

Sorry i was in a bit of a hurry ( on the job when i wrote the previous )
>>Not it shouldn`t be necesary ,
should have been
>>No it shouldn`t be necesary ,
in wich i mend to call the refresh of the control as it should take care of
that itself
as i showed in my example code ( did you tried it )

this code performs 2 loops in 2 seperate methods that should normally block
a ui if it wasn`t written with asynchronous delegates
( even better there is no way you can run these 2 synchronous in the same
time ) but even with the hello world loop in synchronous modus
you cān not get the ui to respond ,,, my example code shows a responsive ui
with a updating progressbar without doevents or a refresh call

as assiging the new value will take care of that as the ui is in a
responsive modus

so this also proves your statement ( that it should not be necesary to call
the refresh method of the control )
regards

Michel



"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
My dear Michel,

I only did mean that
>>application.doevents is overdone in almost all situations you could better
call a refresh on the control
I see that nowhere in your code.

If that is done to refresh and the form as it is hidden by another program,
than it will not help, so it is neither a solution.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht
news:99**********************************@microsof t.com...
Not it shouldn`t be necesary ,

i wrote a nice demo for the TS and for the sceptic people :-)

take a form throw 2 buttons on it and a progressbar

call the progressbar pbmain
now copy paste this :

Imports System.Threading
Public Class Form1
Private Mvar_Cancell As Boolean
Friend Property Cancell() As Boolean
Get
Return Mvar_Cancell
End Get
Set(ByVal value As Boolean)
Mvar_Cancell = value
Me.Button1.Enabled = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Cancell = False
With pbMain
.Value = 0
.Maximum = 100
.Minimum = 0
End With

Dim AsyncProcess As New AsynchMethodInvoker(AddressOf Me.pbLoop)
AsyncProcess.BeginInvoke(Nothing, Nothing)

AsyncProcess = New AsynchMethodInvoker(AddressOf Me.DoSomething)
AsyncProcess.BeginInvoke(Nothing, Nothing)

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Cancell = True
End Sub

Private Sub DoSomething()
Do While Not Me.Cancell
Debug.WriteLine("hello world")
Loop
End Sub
Delegate Sub CrossThreadInvParaMB(ByVal val As Integer)
Private Sub pbLoop()
Dim i As Integer
Do While Not Me.Cancell
Thread.Sleep(50)
i += 1
If i = 101 Then i = 0
SetPbValue(i)
Loop
i = 100
SetPbValue(i)
End Sub
Delegate Sub AsynchMethodInvoker()
Public Sub SetPbValue(ByVal Count As Integer)
If Me.pbMain.InvokeRequired Then
Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)
Try
Me.Invoke(d, New Object() {Count})
Catch ex As Exception

End Try
Else
Me.pbMain.Value = Count
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Cancell = True
End Sub
End Class
i hope this gives you all an idea in what i mean ,,, the method that is
now
updating the progressbar runs in a seperate thread to make it even nicer i
run another worker thread wich prints hello world statements to the debug
window

these threads can comunicate to the progressbar through the SetPbValue
method
i hope this gives the TS some new input to solve his problem


"Cor Ligthert [MVP]" wrote:
>Michel
application.doevents is overdone in almost all situations you could
better
call a refresh on the control
But you see than in my idea in the by the OPr explained sample only the
control, which is AFAIK done by the progressbar itself.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht
news:AC**********************************@microso ft.com...
>
if you want a responsive UI , one that is capable of terminating a
hughe
update for instance , and the ability to perform simultanious tasks
like
updating a progressbar that gives reall info , and a UI that repainst
itself
after moving the form etc etc well then you can only acomplish this
with
multithreading

The dissadvantage of using asynchronous code is that it is usualy
slower
as
synchronous code , however how slow is it when the user terminates the
program because he believes it doesn`t run at all because it is
unresponsive

application.doevents is overdone in almost all situations you could
better
call a refresh on the control
regards

Michel Posseth [MCP]
"GhostInAK" wrote:

Hello Derek,

The reason the UI becomes unresponsive is that the long database
operations
block the message pump. Instead of spinning off new threads, which
are
unneeded,
just massage the message pump immediately after updating the status
bar.
(See MSDN documentation for application.DoEvents)

-Boo

On the main thread I have a status bar in a Windows form that I want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that I
need the thread to finish before code execution continues. The main
reason this does not just run on the same thread is that I need to
get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable
to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up
the
UI. Any ideas of how to get this working?

Derek



Jul 5 '06 #10
Michel,

I have not any doubt if you made it and shows it that it works, and even if
it does not work that it is a typo.

:-)

Cor

"Michel Posseth [MCP]" <MS**@posseth.comschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>
Hi Cor ,,,

Sorry i was in a bit of a hurry ( on the job when i wrote the previous )
>>>Not it shouldn`t be necesary ,
should have been
>>>No it shouldn`t be necesary ,

in wich i mend to call the refresh of the control as it should take care
of that itself
as i showed in my example code ( did you tried it )

this code performs 2 loops in 2 seperate methods that should normally
block a ui if it wasn`t written with asynchronous delegates
( even better there is no way you can run these 2 synchronous in the same
time ) but even with the hello world loop in synchronous modus
you cān not get the ui to respond ,,, my example code shows a responsive
ui with a updating progressbar without doevents or a refresh call

as assiging the new value will take care of that as the ui is in a
responsive modus

so this also proves your statement ( that it should not be necesary to
call the refresh method of the control )
regards

Michel



"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
My dear Michel,

I only did mean that
>>>application.doevents is overdone in almost all situations you could
better call a refresh on the control

I see that nowhere in your code.

If that is done to refresh and the form as it is hidden by another
program, than it will not help, so it is neither a solution.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht
news:99**********************************@microsof t.com...
>Not it shouldn`t be necesary ,

i wrote a nice demo for the TS and for the sceptic people :-)

take a form throw 2 buttons on it and a progressbar

call the progressbar pbmain
now copy paste this :

Imports System.Threading
Public Class Form1
Private Mvar_Cancell As Boolean
Friend Property Cancell() As Boolean
Get
Return Mvar_Cancell
End Get
Set(ByVal value As Boolean)
Mvar_Cancell = value
Me.Button1.Enabled = value
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Cancell = False
With pbMain
.Value = 0
.Maximum = 100
.Minimum = 0
End With

Dim AsyncProcess As New AsynchMethodInvoker(AddressOf Me.pbLoop)
AsyncProcess.BeginInvoke(Nothing, Nothing)

AsyncProcess = New AsynchMethodInvoker(AddressOf Me.DoSomething)
AsyncProcess.BeginInvoke(Nothing, Nothing)

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Cancell = True
End Sub

Private Sub DoSomething()
Do While Not Me.Cancell
Debug.WriteLine("hello world")
Loop
End Sub
Delegate Sub CrossThreadInvParaMB(ByVal val As Integer)
Private Sub pbLoop()
Dim i As Integer
Do While Not Me.Cancell
Thread.Sleep(50)
i += 1
If i = 101 Then i = 0
SetPbValue(i)
Loop
i = 100
SetPbValue(i)
End Sub
Delegate Sub AsynchMethodInvoker()
Public Sub SetPbValue(ByVal Count As Integer)
If Me.pbMain.InvokeRequired Then
Dim d As New CrossThreadInvParaMB(AddressOf SetPbValue)
Try
Me.Invoke(d, New Object() {Count})
Catch ex As Exception

End Try
Else
Me.pbMain.Value = Count
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Cancell = True
End Sub
End Class
i hope this gives you all an idea in what i mean ,,, the method that is
now
updating the progressbar runs in a seperate thread to make it even nicer
i
run another worker thread wich prints hello world statements to the debug
window

these threads can comunicate to the progressbar through the SetPbValue
method
i hope this gives the TS some new input to solve his problem


"Cor Ligthert [MVP]" wrote:
>>Michel

application.doevents is overdone in almost all situations you could
better
call a refresh on the control

But you see than in my idea in the by the OPr explained sample only the
control, which is AFAIK done by the progressbar itself.

:-)

Cor

"M. Posseth" <MP******@discussions.microsoft.comschreef in bericht
news:AC**********************************@micros oft.com...

if you want a responsive UI , one that is capable of terminating a
hughe
update for instance , and the ability to perform simultanious tasks
like
updating a progressbar that gives reall info , and a UI that repainst
itself
after moving the form etc etc well then you can only acomplish this
with
multithreading

The dissadvantage of using asynchronous code is that it is usualy
slower
as
synchronous code , however how slow is it when the user terminates the
program because he believes it doesn`t run at all because it is
unresponsive

application.doevents is overdone in almost all situations you could
better
call a refresh on the control
regards

Michel Posseth [MCP]
"GhostInAK" wrote:

Hello Derek,

The reason the UI becomes unresponsive is that the long database
operations
block the message pump. Instead of spinning off new threads, which
are
unneeded,
just massage the message pump immediately after updating the status
bar.
(See MSDN documentation for application.DoEvents)

-Boo

On the main thread I have a status bar in a Windows form that I
want
to update when a long database process runs. The database process
runs on a separate thread:

Dim t As New Thread(AddressOf SomeDatabaseProcess)

t.Start()

The database process will be called about 10 times in one long
routine, starting a new thread each time. But the problem is that
I
need the thread to finish before code execution continues. The
main
reason this does not just run on the same thread is that I need to
get
the darn status bar updated, but the UI freezes up. I tried using
t.Join() which does wait for the thread to finish, but it does not
allow the UI to be freed up. And I tried setting a global variable
to
detect when the thread is done, but the code after the thread just
runs in a Do Loop until the variable is set, which also freezes up
the
UI. Any ideas of how to get this working?

Derek



Jul 5 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: haptiK | last post by:
Hello, can i use php to multithread mail() or something similar? in my company i need to send multiple copies of email to a few hundred ppl affilated and on my list. instead of calling mail...
4
by: dbmethods | last post by:
Could someone give a hint on how to do multithread programming with PHP scripting here? Thanks
0
by: Alice | last post by:
Hello I have four multithread windows applications(vb.net) interacting and running on the same machine(windows 2000 with .net framework 1.0). All of them start a new thread each time Filewatcher's...
0
by: r_obert | last post by:
Hello, I'm trying to create a worker thread for my VC++ program, and was wondering whether I should be linking with the Multithread /MT or Multithread DLL /MD option? I'm not quite sure, in...
3
by: QQ | last post by:
I am new here and got lost on a multithread C++ system source codes Thanks a lot!
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
2
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting...
0
by: fred | last post by:
I need some help in trying to understand how to make myCollection (inherited from CollectionBase) multithread safe. Taking my implementation of the Add Sub and a readonly property Item. Public...
6
by: jmartin | last post by:
Hi, I have made a multithread version of a program (load a file into database), and with two processors I get the double of time in the multithread than in the process (unithread) version. I...
2
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.