BackgroundWorker task blocks UI thread in my web application - asp.net

Okay, I've been banging on this problem for close to 10 work hours now with no real progress to speak of. After going through multiple "solutions" offered on the web, I find myself still unable to accomplish what should be a really simple task -- pop up a modal dialog from my web application while I complete a long process I'm forcing the user to wait upon.
I've ripped out all the non-multi-threading-control aspects and am left with this bare bones structure and it still doesn't cut it:
In my ASPX file, I have two "please wait" displays, one is a DIV set up for use with bPopup, the other a DIV runat=server following the examples on BackgroundWorker. The bPopup dialog works great when used independently of any background work. But, when the background worker fires, the UI freezes until it completes, then both opens & closes the bPopup dialog as fast as it can, basically flashing the screen.
This is my code behind: I've cut it to the bare minimum to see a dialog pop up and go away again after the background task completes.
Protected Sub EditBtn_Click(sender As Object, e As EventArgs)
ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), "javascript: bPopup = $('#element_to_pop_up').bPopup({ modal: true, modalClose: false});", True)
MenusBeingGenerated.Visible = True
bw = New BackgroundWorker()
bw.WorkerReportsProgress = True
bw.WorkerSupportsCancellation = True
bw.RunWorkerAsync()
End Sub
The javascript fires the bPopup, while MenusBeingGenerated is the name of the server-side DIV. The background task itself has been ripped down to just a do-nothing time-consumer.
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
For i = 1 To 40
Threading.Thread.Sleep(250)
bw.ReportProgress(i, "Running...")
If bw.CancellationPending Then
bw.ReportProgress(i, "Cancelling...")
Exit For
End If
Next
' Cleanup
If bw.CancellationPending Then
e.Cancel = True
bw.ReportProgress(100, "Cancelled.")
End If
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bw.ProgressChanged
StatusLabel.Text = e.ProgressPercentage.ToString & "% complete."
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted
StatusLabel.Text = "Completed successfully"
ScriptManager.RegisterStartupScript(Page, Page.GetType, Guid.NewGuid().ToString(), "javascript: if (bPopup) { bPopup.close() };", True)
End Sub
When I run this code, and click the button that triggers it all there is a long pause then the screen blinks grey (the same behavior can be seen by opening and closing the bPopup from within the same button handler).
Adding breakpoints to walk through the code, I see the background task get launched via the button handler, it cycles through and the BackgroundWorker1_ProgressChanged handler gets called every quarter second, finally the BackgroundWorker1_RunWorkerCompleted method runs, and when that finishes the only page refresh finally happens (with the blink as bPopup fires & clears).
The DIV id=MenusBeingGenerated never displays, despite having been set as visible, nor does the label id=StatusLabel that resides within it ever get rendered.
Any clues what I've missed???
[ BTW, there are several questions along these lines on this and other web sites, none of the current answers apply. I've cookbooked this off several sites, thrown out multiple versions, and this is the closest I've gotten to something that works -- and it's no different than where I started: the user clicks the go button, a lot of nothing visible happens while the server churns out a new file, then the page refreshes when it's all done. ]

Related

Button.Enabled and checkbox.Checked status erased when redirecting on event ASP.NET VB

In my register page I have a checkbox (chkAgree) next to the terms of agreement. The register (btnRegister) button is disabled until chkAgree.Checked = True. Since the registration section is toward the bottom of the page I want to redirect the user to an anchor tag during the event. I can't seem to accomplish this without one of the two objects in question losing their status. I tried creating a Session for each object and then calling them on the page load event with no luck. I am obviously doing it wrong, but could someone please point me in the right direction? btnRegister's default status is set to Enabled=False. Here are the two methods I am using
CheckedChanged:
Protected Sub chkAgree_CheckedChanged(sender As Object, e As EventArgs) Handles chkAgree.CheckedChanged
Session("Agree") = chkAgree.Checked
Response.Redirect("~/ExteriorStudentTestimonials.aspx#register", False)
End Sub
And Page_Load:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Session("Agree") = True Then
btnRegister.Enabled = True
chkAgree.Checked = True
Else
btnRegister.Enabled = False
chkAgree.Checked = False
End If
End Sub
I have tried several different combinations of If statements but cannot figure it out without taking Response.Redirect("~/ExteriorStudentTestimonials.aspx#register", False) out.
This might be related to the fact that you're reloading the page. You might want to take a look at the following question for some ideas of how to jump to an anchor without a full redirect: Programmatically scroll to an Anchor Tag
On a different note, jumping to an anchor when the user clicks a check box doesn't sound like a very good user experience. You might consider making your form shorter.
Got it using this method similar to the ones described in the link you provided
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"navigate",
"document.getElementById('register').scrollIntoView();",
True)

Trying to read MIRC logs in ASP.net

I am trying to make a page that lets my admins read the MIRC Log files from our bot...I am having the hardest time trying to get this to work for some reason...i have taken bits and pieces of code to get what i have working...but it still either doesn't display at all OR after about 200 lines, it stops reading each line but instead make it one big mess of text...here is what i have for code
Protected Sub bView_Click(sender As Object, e As EventArgs) Handles bView.Click
Response.Write(Server.MapPath("~/mirc/logs/" & lbfiles.SelectedItem.Text))
lFileOut.Text = ""
Try
Dim FILENAME As String = Server.MapPath("~/mirc/logs/" & lbfiles.SelectedItem.Text)
Dim objStreamReader As StreamReader = New StreamReader(FILENAME, Encoding.UTF8)
Dim cont As String
Do
cont = objStreamReader.ReadLine()
lFileOut.Text = lFileOut.Text & cont & "<br>"
'Response.Write(cont)
Loop Until cont Is Nothing
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Sub
Here is a test file i have been testing everything on...it just doesn't do anything nor does it throw any error...i am completely stumped on this one.
test file
Do this: go into your server, in your root folder make a new folder called whatever you want then go into your mIRC script goto tools/options/logging change directory to the new folder you made click save.. that way you or anyone you want to view all logs from your mIRC script can view by going to www.yoursite.net/foldername and it will show you the list of all logged things, status and room window conversations...
If you want to know how to code it so your opers can just have a clickable link in their remotes I can do that for you as well.... hope this helps

Run loop after page load VB.NET

I would like the page to load, then I will display a GIF that's LOADING DATA and then I want this loop (see below) to run until the row count is not 0.
(An SQL job has been triggered in the previous page which takes about 1 - 2 minutes to run and so I want to display the new data automatically when it appears in the database.)
I can't figure out how to programmatically get this to work, I understand that because I have this in the page_load it will run before the page loads but I'm not sure how to run it after the page has loaded
Any help, advice or tips are very welcome
Thank you
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Do While GridView1.Rows.Count = 0 And GridView2.Rows.Count = 0
GridView1.DataBind()
GridView2.DataBind()
Loop
End Sub
If using Ajax/JQuery, can you not use a timer control that posts back every seconds and as soon as there is data in both tables then disable the timer.

log in to a webpage and capture screenshot in vb6

I have a visual basic 6 application that needs to get pictures from a particular website but the problem is that the users have to open the webpage on the browser and log in to the webpage then download the picture and upload it in the vb6 app. Is it possible to have the vb6 go to that webpage and log in and capture the screenshot and save it in a particular folder without opening the browser?
The url opens the login page by default and you have to log in first, to access the picture page, which we just have to take a screen shot and crop it.
Is this possible in pure VB6?
Here is some VERY generic code that will log you into a website. It is basically a matter of finding the controls in the browser document and filling in the correct values. Since you have provided no code to build on it's up to you to fill in all the correct values. This is using Microsoft Internet Controls to add a browser control to a form.
Private Sub Form_Load()
Dim i As Integer
WebBrowser1.Navigate ("http://URL of the page you want to go to")
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
If InStr(WebBrowser1.LocationURL, "http://targetwebsite/login.aspx") Then
On Error Resume Next
For i = 0 To WebBrowser1.Document.Forms(0).length - 1
' Uncommenting the MsgBox method will display the control names and help find the controls you are looking for
'MsgBox WebBrowser1.Document.Forms(0)(i).Type & ", " & WebBrowser1.Document.Forms(0)(i).Name
If WebBrowser1.Document.Forms(0)(i).Type = "text" Then
WebBrowser1.Document.Forms(0)(i).Value = "user name"
End If
If WebBrowser1.Document.Forms(0)(i).Type = "password" Then
WebBrowser1.Document.Forms(0)(i).Value = "user password"
End If
Next i
' now find and click the submit button
For i = 0 To WebBrowser1.Document.Forms(0).length - 1
If WebBrowser1.Document.Forms(0)(i).Type = "submit" Then
WebBrowser1.Document.Forms(0)(i).Click
End If
Next i
End If
' You should now be logged in and loading the page you want
End Sub

WEIRDEST ERROR EVER: ASP.NET and Firefox?

I've always thought that server-side code is unaffected by the browser requesting it, but I've just come across an exception:
I have a button that when clicked, changes the button's CSS class (and those relative to it) and rebinds a GridView with new data. The function is this:
Private Sub lbViewUnUsed_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbViewUnUsed.Click
lbViewUsed.CssClass = "button small"
lbViewUnUsed.CssClass = "button small selected"
BindUsage(UsageBinding.UnUsed)
End Sub
In IE, this works perfectly. However, in Firefox, the BindUsage() function stops firing midway and nothing gets accomplished at all! In my frustration, I inserted Debug.Assert() statements after every line of code in the BindUsage() to see what was going on, and sure enough, the function would not go all the way through in Firefox ONLY.
The BindUsage() function is this (with the Debug assertions still in):
Private Sub BindUsage(ByVal bindWhat As UsageBinding)
Debug.Assert(False, 1)
If bindWhat = UsageBinding.Used Then
gvUsage.DataSource = sUser.GetWordsMarkedWithUsage(True)
gvUsage.Columns(0).Visible = False 'hide button column'
Else
Debug.Assert(False, 2)
gvUsage.DataSource = sUser.GetWordsMarkedWithUsage(False)
Debug.Assert(False, 3)
Dim userInfo As UserAccount.LoginInfo = UserAccount.GetUserLoginInfo
Debug.Assert(False, 4)
Dim showUsedButton As Boolean
Debug.Assert(False, 5)
showUsedButton = (userInfo.UserName.ToLowerInvariant = sUser.UserName.ToLowerInvariant)
Debug.Assert(False, 6)
gvUsage.Columns(0).Visible = showUsedButton 'show button column'
Debug.Assert(False, 7)
End If
Debug.Assert(False, 8)
gvUsage.DataBind()
Debug.Assert(False, 9)
End Sub
The above function does not make it past 5 in Firefox. I'm guessing there's some sort of problem with the assignment of the showUsedButton variable, but I can't figure out what it would be. Why would this fail only in Firefox, but not in any other browser especially since this is occurring on the server???
It's happening on the server, but you are retrieving data that was generated by the client:
UserAccount.GetUserLoginInfo
I would review your stored data and see what's different in it based on client application and see how it's handled.
Well, it's a bit of a guess, but...
What exactly does UserAccount.GetUserLoginInfo do? If I had to guess, it tries to do something browser-specific. For example, it could try and read a cookie from the client? If it does, then my next guess is that userInfo or either userInfo.UserName is null, thus throwing a NullReferenceException.
It could be that IE accepts cookies, and firefox does not. Hence no error in IE, but only in Firefox.
Again, this is guesswork, I'm not strong in VB.NET and have no idea what that UserAccount class is. Maybe if you'd give use the errormessage, it could mean some more.
Would you definitely spot if any exceptions were being thrown? If not, fix that before you do anything else.
As for why it might be browser-specific:
If it depends on what data is sent by the browser and how, then clearly the browser can change the behaviour
I believe ASP.NET renders in a browser-specific way; if your code is touching the rendering logic, it may be behaving differently depending on user agent.

Resources