473,770 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Long Process

I have a long process I run that needs to be complete before the user can continue. I can change the cursor to an hourglass, but I want to update the Status Strip on the bottom during the process. I set the statusstrip label to "Downloading... ", but it will not show up on the form. I need to display this message before it starts the process. I can put a thread.sleep for 1 second after I do the initial process, but that seems sloppy to me. Any suggestions? Here is my code launched from a button click:
'update the label--This does not show because the process starts before the UI updates
slDBList.Text = "Retrieving Databases..."Tr yMe.Cursor = Cursors.WaitCur sorDim lstServers As List(Of String)'set to false in case of an error boolServersLoad ed = FalseboolDBLoad ed = False'clear the servers list cbServers.DataS ource = Nothing'clear the databases list dbDatabases.Dat aSource = NothingIf chkSort.Checked = True ThenlstServers = GetSqlServers(T rue)ElselstServ ers = GetSqlServers(F alse)End IfcbServers.Dat aSource = lstServers'set to true to enabled the selected index change on the dropdown boolServersLoad ed = TrueslDBList.Te xt = ""Catch ex As ExceptionMessag eBox.Show("Erro r loading server list. Error: " & ex.ToString, "Data Error", MessageBoxButto ns.OK, MessageBoxIcon. Error)boolServe rsLoaded = FalseslDBList.T ext = ""FinallyMe.Cur sor = Cursors.ArrowEn d Try
Jun 27 '08
32 1707
When I do this I get a cross thread error. Any ideas?

John
"kimiraikko nen" <ki************ *@gmail.comwrot e in message
news:9a******** *************** ***********@r66 g2000hsg.google groups.com...
On May 21, 9:09 pm, "John Wright" <riley_wri...@h otmail.comwrote :
I have a long process I run that needs to be complete before the user can
continue. I can change the cursor to an hourglass, but I want to update
the Status Strip on the bottom during the process. I set the statusstrip
label to "Downloading... ", but it will not show up on the form. I need to
display this message before it starts the process. I can put a
thread.sleep for 1 second after I do the initial process, but that seems
sloppy to me. Any suggestions? Here is my code launched from a button
click:

'update the label--This does not show because the process starts before
the UI updates
slDBList.Text = "Retrieving Databases..."Tr yMe.Cursor =
Cursors.WaitCur sorDim lstServers As List(Of String)'set to false in case
of an error boolServersLoad ed = FalseboolDBLoad ed = False'clear the
servers list cbServers.DataS ource = Nothing'clear the databases list
dbDatabases.Dat aSource = NothingIf chkSort.Checked = True ThenlstServers =
GetSqlServers(T rue)ElselstServ ers = GetSqlServers(F alse)End
IfcbServers.Dat aSource = lstServers'set to true to enabled the selected
index change on the dropdown boolServersLoad ed = TrueslDBList.Te xt =
""Catch ex As ExceptionMessag eBox.Show("Erro r loading server list. Error:
" & ex.ToString, "Data Error", MessageBoxButto ns.OK,
MessageBoxIcon. Error)boolServe rsLoaded = FalseslDBList.T ext =
""FinallyMe.Cur sor = Cursors.ArrowEn d Try
Hi,
Could you put the code above into BackgroundWorke r's Do_Work event sub
and run it using RunWorkerAsync method?
And just set statusStrip text in button_click event.

Like this:
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

StatusStrip1.te xt = "Downloading... "
BackgroundWorke r1.RunWorketAsy nc()

End Sub

Private Sub BackgroundWorke r1_DoWork(ByVal sender As System.Object,
ByVal e As System.Componen tModel.DoWorkEv entArgs) Handles
Backgroundworke r1.DoWork

' Here is the code that you sent in your original post

End Sub
....should work fine.

Thanks,

Onur Güzel
Jun 27 '08 #11
Joergen,

Be aware that you never should do what you advice as it is a critical
process that has to be completed, and the user can end that in the middle
without knowing what is the effect.

Think by instance in a paying process by banks, were steps have to be done,
I would never be the one who would give an advice as yours.

Just my idea,

Cor

"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNO SPAMschreef in bericht
news:7m******** *************** *********@4ax.c om...
>
Run the task on another thread. If you are using .Net 2.0 or later
(VS2005/VS2008), this is fairly easy with the BackgroundWorke r class.

http://msdn.microsoft.com/en-us/libr...undworker.aspx

Do not run the task on the UI thread or you will get the results you
are currently seeing.

/Joergen Bech

On Wed, 21 May 2008 12:09:09 -0600, "John Wright"
<ri**********@h otmail.comwrote :
>>I have a long process I run that needs to be complete before the user can
continue. I can change the cursor to an hourglass, but I want to update
the Status Strip on the bottom during the process. I set the statusstrip
label to "Downloading... ", but it will not show up on the form. I need to
display this message before it starts the process. I can put a
thread.slee p for 1 second after I do the initial process, but that seems
sloppy to me. Any suggestions? Here is my code launched from a button
click:
'update the label--This does not show because the process starts before
the UI updates
slDBList.Te xt = "Retrieving Databases..."Tr yMe.Cursor =
Cursors.WaitC ursorDim lstServers As List(Of String)'set to false in case
of an error boolServersLoad ed = FalseboolDBLoad ed = False'clear the
servers list cbServers.DataS ource = Nothing'clear the databases list
dbDatabases.D ataSource = NothingIf chkSort.Checked = True ThenlstServers =
GetSqlServers (True)ElselstSe rvers = GetSqlServers(F alse)End
IfcbServers.D ataSource = lstServers'set to true to enabled the selected
index change on the dropdown boolServersLoad ed = TrueslDBList.Te xt =
""Catch ex As ExceptionMessag eBox.Show("Erro r loading server list. Error:
" & ex.ToString, "Data Error", MessageBoxButto ns.OK,
MessageBoxIco n.Error)boolSer versLoaded = FalseslDBList.T ext =
""FinallyMe.C ursor = Cursors.ArrowEn d Try
Jun 27 '08 #12
John,

Mostly it helps, when you set it in a seperate method.

Cor

"John Wright" <ri**********@h otmail.comschre ef in bericht news:eT******** ******@TK2MSFTN GP06.phx.gbl...
I have a long process I run that needs to be complete before the user can continue. I can change the cursor to an hourglass, but I want to update the Status Strip on the bottom during the process. I set the statusstrip label to "Downloading... ", but it will not show up on the form. I need to display this message before it starts the process. I can put a thread.sleep for 1 second after I do the initial process, but that seems sloppy to me. Any suggestions? Here is my code launched from a button click:
'update the label--This does not show because the process starts before the UI updates
slDBList.Text = "Retrieving Databases..."Tr yMe.Cursor = Cursors.WaitCur sorDim lstServers As List(Of String)'set to false in case of an error boolServersLoad ed = FalseboolDBLoad ed = False'clear the servers list cbServers.DataS ource = Nothing'clear the databases list dbDatabases.Dat aSource = NothingIf chkSort.Checked = True ThenlstServers = GetSqlServers(T rue)ElselstServ ers = GetSqlServers(F alse)End IfcbServers.Dat aSource = lstServers'set to true to enabled the selected index change on the dropdown boolServersLoad ed = TrueslDBList.Te xt = ""Catch ex As ExceptionMessag eBox.Show("Erro r loading server list. Error: " & ex.ToString, "Data Error", MessageBoxButto ns.OK, MessageBoxIcon. Error)boolServe rsLoaded = FalseslDBList.T ext = ""FinallyMe.Cur sor = Cursors.ArrowEn d Try
Jun 27 '08 #13
Sorry,

I had not yet readed Mayurs answer,

That is most probably your answer,
As that does not work, combine his and mine.

Cor
"Cor Ligthert[MVP]" <no************ @planet.nlschre ef in bericht news:BF******** *************** ***********@mic rosoft.com...
John,

Mostly it helps, when you set it in a seperate method.

Cor

"John Wright" <ri**********@h otmail.comschre ef in bericht news:eT******** ******@TK2MSFTN GP06.phx.gbl...
I have a long process I run that needs to be complete before the user can continue. I can change the cursor to an hourglass, but I want to update the Status Strip on the bottom during the process. I set the statusstrip label to "Downloading... ", but it will not show up on the form. I need to display this message before it starts the process. I can put a thread.sleep for 1 second after I do the initial process, but that seems sloppy to me. Any suggestions? Here is my code launched from a button click:
'update the label--This does not show because the process starts before the UI updates
slDBList.Text = "Retrieving Databases..."Tr yMe.Cursor = Cursors.WaitCur sorDim lstServers As List(Of String)'set to false in case of an error boolServersLoad ed = FalseboolDBLoad ed = False'clear the servers list cbServers.DataS ource = Nothing'clear the databases list dbDatabases.Dat aSource = NothingIf chkSort.Checked = True ThenlstServers = GetSqlServers(T rue)ElselstServ ers = GetSqlServers(F alse)End IfcbServers.Dat aSource = lstServers'set to true to enabled the selected index change on the dropdown boolServersLoad ed = TrueslDBList.Te xt = ""Catch ex As ExceptionMessag eBox.Show("Erro r loading server list. Error: " & ex.ToString, "Data Error", MessageBoxButto ns.OK, MessageBoxIcon. Error)boolServe rsLoaded = FalseslDBList.T ext = ""FinallyMe.Cur sor = Cursors.ArrowEn d Try
Jun 27 '08 #14

From what I understand, we were talking about a heavy, long-running
task with an unresponsive interface. In the case of a bank
transaction, we would be dealing with an optimistic or pessimistic
locking problem - not a threading one, as multithreading would be
entirely inappropriate in such a scenario.

The BackgroundWorke r class is there for a reason and I was not
the only one suggesting it.

The class provides properties, events, and methods for controlling
how an asynchronous task could or should be aborted.

It is up to the coder to use these properly to ensure that the ACID
properties of the operation are maintained.

The form that initiated the process should not just kill it directly
at the user's request: The pattern is to send a signal to the task,
asking it to cancel itself. The long-running task should periodically
check this flag and either ignore it or do whatever is necessary to
back out of whatever it is doing.

I have used BackgroundWorke r myself in a similar scenario and I
can assure you that I took steps to ensure that the only thing the
user could do was watch the progress or cancel the operation
before being able to do anything else.

As for DoEvents as an alternative, I would like to direct you and
the original poster to these articles:
http://www.codinghorror.com/blog/archives/000159.html
http://www.codinghorror.com/blog/archives/000370.html

Bear in mind that the first article before BackgroundWorke r came
onto the scene and made life much easier in this regard.
The important thing is the comments section, which contains
information and anecdotes that could be said to be of more
value than the articles themselves.

For my own part, I have seen applications where DoEvents
statements were littered all over the place to improve responsiveness.
Those apps were a mess of reentrancy problems, which only goes
to show that if you do not know what you are doing in the first place,
you should not touch DoEvents, let alone BackgroundWorke r.
Though DoEvents code is "easier" to debug than BackgroundWorke r
ditto, the issues are the same.

Better to have an unresponsive application that works than a
responsive one that doesn't.

Getting back to BackgroundWorke r: The code example in the
online documentation should be enough to getting started with.

Regards,

Joergen Bech

On Thu, 22 May 2008 05:45:30 +0200, "Cor Ligthert[MVP]"
<no************ @planet.nlwrote :
>Joergen,

Be aware that you never should do what you advice as it is a critical
process that has to be completed, and the user can end that in the middle
without knowing what is the effect.

Think by instance in a paying process by banks, were steps have to be done,
I would never be the one who would give an advice as yours.

Just my idea,

Cor

"Joergen Bech @ post1.tele.dk>" <jbech<NOSPAMNO SPAMschreef in bericht
news:7m******* *************** **********@4ax. com...
>>
Run the task on another thread. If you are using .Net 2.0 or later
(VS2005/VS2008), this is fairly easy with the BackgroundWorke r class.

http://msdn.microsoft.com/en-us/libr...undworker.aspx

Do not run the task on the UI thread or you will get the results you
are currently seeing.

/Joergen Bech

On Wed, 21 May 2008 12:09:09 -0600, "John Wright"
<ri**********@ hotmail.comwrot e:
>>>I have a long process I run that needs to be complete before the user can
continue. I can change the cursor to an hourglass, but I want to update
the Status Strip on the bottom during the process. I set the statusstrip
label to "Downloading... ", but it will not show up on the form. I need to
display this message before it starts the process. I can put a
thread.sle ep for 1 second after I do the initial process, but that seems
sloppy to me. Any suggestions? Here is my code launched from a button
click:
'update the label--This does not show because the process starts before
the UI updates
slDBList.Tex t = "Retrieving Databases..."Tr yMe.Cursor =
Cursors.Wait CursorDim lstServers As List(Of String)'set to false in case
of an error boolServersLoad ed = FalseboolDBLoad ed = False'clear the
servers list cbServers.DataS ource = Nothing'clear the databases list
dbDatabases. DataSource = NothingIf chkSort.Checked = True ThenlstServers =
GetSqlServer s(True)ElselstS ervers = GetSqlServers(F alse)End
IfcbServers. DataSource = lstServers'set to true to enabled the selected
index change on the dropdown boolServersLoad ed = TrueslDBList.Te xt =
""Catch ex As ExceptionMessag eBox.Show("Erro r loading server list. Error:
" & ex.ToString, "Data Error", MessageBoxButto ns.OK,
MessageBoxIc on.Error)boolSe rversLoaded = FalseslDBList.T ext =
""FinallyMe. Cursor = Cursors.ArrowEn d Try
Jun 27 '08 #15
"Mayur H Chauhan" <ma***@orioninc .comschrieb
How about Application.DoE vents ? This might help for processing
pending thread.
Doevents should not be used anymore. It was valid with previous VB
versions where multi threading was (appart from appartment threading)
almost impossible. Doevents creates more problems than necessary. First,
the classification of UI tasks and independent other tasks would have to
be removed. Not every task has an abstract and regular event/callback
mechanism that can be used to update the UI, especially when talking
about long running database activities. In addition, you'd have
to be aware of code reentrance. Moreover, why create new code to process
messages even if there is already a message loop that is made for it? My
experience is that almost every time when I was using Doevents, 5 LOC
later I'd wished to go the "clean" way and had used a separate thread
instead.
Armin

Jun 27 '08 #16
"Cor Ligthert[MVP]" <no************ @planet.nlschri eb
Joergen,

Be aware that you never should do what you advice as it is a
critical process that has to be completed, and the user can end that
in the middle without knowing what is the effect.

Think by instance in a paying process by banks, were steps have to
be done, I would never be the one who would give an advice as yours.

Just my idea,

Cor
Like Joergen, I don't understand this plea. Using another thread to
avoid the UI thread being blocked is the (only) right action, IMO. But
maybe you meant that the UI thread also have to be in an appropriate
state depending on the whole situation, where I would agree.
Armin

Jun 27 '08 #17
On Thu, 22 May 2008 11:09:12 +0200, "Armin Zingler"
<az*******@free net.dewrote:
>"Mayur H Chauhan" <ma***@orioninc .comschrieb
>How about Application.DoE vents ? This might help for processing
pending thread.

Doevents should not be used anymore. It was valid with previous VB
versions where multi threading was (appart from appartment threading)
almost impossible. Doevents creates more problems than necessary. First,
the classification of UI tasks and independent other tasks would have to
be removed. Not every task has an abstract and regular event/callback
mechanism that can be used to update the UI, especially when talking
about long running database activities. In addition, you'd have
to be aware of code reentrance. Moreover, why create new code to process
messages even if there is already a message loop that is made for it? My
experience is that almost every time when I was using Doevents, 5 LOC
later I'd wished to go the "clean" way and had used a separate thread
instead.
Armin
I would like to add that as a general tool for UI responsiveness,
DoEvents is not all it is cracked up to be, as opposed to
BackgroundWorke r:

If we are just running a homogenous loop, sure, we can call DoEvents
every nn iterations, giving the interface a chance to refresh itself,
but if the long-running tasks consists of a number of different
subtasks, we cannot depend on all tasks to be able to call DoEvents
at the same, regular intervals. Suppose the task is waiting for a
response or timeout from a database server?

Using BackgroundWorke r (or threads), on the other hand, the
scenario is like this:

1. User initiates task.
2. UI displays "Processing ..."
3. User clicks Cancel button.
4. UI displays "Cancelling ..."
5. Message is sent to thread, asking it to stop whatever it is doing
6. When the task, which is dutifully monitoring the Cancel flag when
it can, has finished, it informs the thread about its status.
7. UI displays "Cancelled" or "Completed" .

So in the case of subtasks where it is not possible to call DoEvents
for a few seconds, the DoEvents approach would still result in a
- sometimes - unresponsive interface, whereas the BackgroundWorke r
approach would allow the user to click the Cancel button immediately
and then see a "Cancelling ..." message for a couple of seconds until
the task had a chance to respond.

Regards,

Joergen Bech

Jun 27 '08 #18
but if the long-running tasks consists of a number of different
subtasks, we cannot depend on all tasks to be able to call DoEvents
at the same, regular intervals.
Agree. That's what I meant with "Not every task has an abstract and
regular event/callback mechanism that can be used to update the UI,
especially when talking about long running database activities."

(But I see it as your agreement supplemented by the BGW)
Armin

Jun 27 '08 #19
On Thu, 22 May 2008 13:06:46 +0200, "Armin Zingler"
<az*******@free net.dewrote:
>but if the long-running tasks consists of a number of different
subtasks, we cannot depend on all tasks to be able to call DoEvents
at the same, regular intervals.

Agree. That's what I meant with "Not every task has an abstract and
regular event/callback mechanism that can be used to update the UI,
especially when talking about long running database activities."
So you did. And so much more nicely put than my long-winded
explanation. Do you know the type of person who reads half a
post, and, blinded to the details, thinks he has an idea what
the whole thing contains, proceeds to write a follow-up?

Well, that was a good example, however much I usually try to avoid
being such a person.
>(But I see it as your agreement supplemented by the BGW)
I'll pretend that additional information was my excuse for
posting it and saves some face.

Regards,

Joergen Bech

Jun 27 '08 #20

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

Similar topics

5
3372
by: PontiMax | last post by:
Hi, when I press the OK button of my dialog box a long-running task is initiated. Therefore I would like to make visible a div section right after clicking the button where a user-friendly message is displayed... How can this be done? Can I make the section visible via server-side code? Via Response.Write()? Any examples would be appreciated!
18
2210
by: Larry Herbinaux | last post by:
I'm having issues with garbage collection with my long-standing service process. If you could review and point me in the right direction it would be of great help. If there are any helpful documents that you could point me to help me control the GC, then that would be great also. The .Net GC does not cleanup memory of our service process unless it is forced to by another process that hogs memory. · GC Algorithm - This is an issue...
4
1968
by: ImSoLost | last post by:
I'm running a really long process from ASP.NET and need some help... I am making a method call when the user presses a button from my webpage, which goes into a database and parses a file. This process could exceed an hour, so the user is expected to press the button to start the process and minimize the IE window and do other work while the application is processing. Anyway, after a certain period in time, the page becomes "Page could...
1
6025
by: Anonieko | last post by:
Query: How to display progress bar for long running page Answer: Yet another solution. REFERENCE: http://www.eggheadcafe.com/articles/20050108.asp My only regret is that when click the browser back button, it loads the progress bar again. Any solution to this?
14
23170
by: lmttag | last post by:
Hello. We're developing an ASP.NET 2.0 (C#) application and we're trying to AJAX-enable it. We're having problem with a page not showing the page while a long-running process is executing. So, we're looking for a way to display the page with a "please wait..." message while the process is running, and then, when the process is done, update the page with the actual results/page content. We have a page that opens another browser/page...
1
2435
by: walterbyrd | last post by:
I understand that Python has them, but PHP doesn't. I think that is because mod_php is built into apache, but mod_python is not usually in apache. If mod_python was built into apache, would python still have long running processes (LRP)? Do LRPs have to do with a Python interpreter running all the time? Or is it something else? I also understand that LRPs are the reason that shared hosting is less
4
2852
by: commander_coder | last post by:
Hello, I write a lot of CGI scripts, in Python of course. Now I need to convert some to long-running processes. I'm having trouble finding resources about the best practices to do that. I've found a lot of email discussions that say something like, "You need to educate yourself about the differences when you have long- running processes" but I've not had a lot of luck with finding things that explain the differences. I've seen some...
2
1634
by: =?Utf-8?B?QWxwaGFwYWdl?= | last post by:
Hello, I have a class MyWorker. Each time I create a new instance of MyWorker, I queue it to the ThreadPool. So, 1 MyWorker object is pooled and belongs to its thread (there can't have 2 MyWorker in 1 thread from the ThreadPool). When MyWorker is initialized or instanciate, I use an asynchronous delegate to execute a long running process (depending on users, the job can be quicker). So, this async delegate will use or create a new...
2
1799
by: =?Utf-8?B?d2R1ZGVr?= | last post by:
I have a website using windows integrated security, with anonymous access turned off. The site is used to query orders from a database and when the search takes a long time, a windows login box appears. Regardless of what login the user enters into this, it does not accept it and the user is locked out of the system. Our network team and myself have been unable to find out why this is occurring, has anyone else had a similiar problem?...
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.