ASP.NET Background Thread blocking page access - c#

I have a (very old but still maintained) website built on .NET WebForms.
Recently I had the need to add a task/operation that, upon click on a button, initiates a background thread that updates a bunch of different stuff:
1 - Integrate with an outside API to fetch information and write it into the database
2 - Update a search index built on Lucene
The code, on what we call the Settings page (Setting.aspx) looks something like this:
protected void btnForcePartialSync_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string logLocation = HostingEnvironment.MapPath(VirtualPathUtility.AppendTrailingSlash(ConfigurationManager.AppSettings["SyncLogsLocation"]) + ConfigurationManager.AppSettings["PartialSyncLogName"]);
if (File.Exists(logLocation))
Renamefile(logLocation);
StreamWriter sw = new StreamWriter(logLocation);
DateTime end = DateTime.UtcNow;
DateTime start;
if (button.Equals(btnDoPartialSyncMonth))
start = end.AddMonths(-1);
else
start = end.AddHours(-24);
HttpContext ctx = HttpContext.Current;
ThreadStart starter = () => { PopulateCv.ForcePartialSync(sw, start, end); };
starter += () => {
HttpContext.Current = ctx;
CacheService.ClearCache(CacheService.WORKS_COUNT_CACHE_KEY);
int CvCount = DataAccess.Cv.Cv.GetActiveCvs().Count;
CacheService.Add(CacheService.WORKS_COUNT_CACHE_KEY, CurriculumCount);
CVSearchIndex.BuildIndex();
FillContentsIndexInfo();
};
Thread t = new Thread(starter) { IsBackground = true };
t.Start();
}
This process, as expected, takes some time to complete since the CVSearchIndex.BuildIndex(); operation is very long (over 10 minutes). Everything is fine up until here.
It seems to work nicely, and I can still use the site and navigate to a different page, e.g. Homepage while the operation completes.
However once I try to navigate back into the Settings page, it simply will not load until the thread ends (when it's done building the search index).
This is not the behaviour I was expecting. How can I solve this without substantial changes to the current code?
Can I make the navigation to this page not block while the search index is being built?
Best Regards.

This is probably blocking due to Session mutex. Try adding the following to the page (aspx) <%# Page EnableSessionState="true|false|ReadOnly" %> (false or ReadOnly)
Alternatively, if it's just a fire and forget operation, you could set it up as a scheduled task, and then fire it off manually from your method, allowing the method to end immediately (running the job in the background)

Related

FreshMVVM and resetting VM before popping Modal

FreshMVVM 3.0.0
Xamarin Forms 4.2
A number of our input pages are loaded modally and when the user presses Save we execute a Command like this
var newTemperature = new Temperature()
{
Date = DateTime.Now,
Value = this.TemperatureValue,
CaptureType = CaptureType.Manual,
IsModified = true,
};
await this.Services.DataService.SaveAsync(newTemperature);
// Save completed, now close modal.
await this.CoreMethods.PopPageModel(data, modal, animate);
If you look at the CoreMethods.PopPageModel call in GitHub you can see that it deals with two processes
Raising the PageWasPopped signal
Calling to the Navigation Service to pop the page off of the navigation stack
The FreshMVVM code that handles the page being popped is in FreshPageModel. Among other things the code unhooks from the Appearing and Disappearing events and sets the BindingContext to null. As you can see from the above order that means the BindingContext on the View is set to null before it is popped off the stack.
The problem with this is that for a short period of between 0.5 and 1.5 seconds the user sees a View that looks like the data has all been reset. This could be quite disconcerting if they have just pressed Save.
If I reverse the order of the logic in PopPageModel and pop from the navigation stack before calling RaisePageWasPopped this issue goes away.
Has nobody else seen this problem before?
Any users of FreshMVVM who want to point out the error of my suggested approach?
Just note that the service is being awaited, holding the UI until service completes,
have you tried removing await and popping the page immediately or displaying a loader while the service is busy?
this.InsertReports(metadata.Reports).ConfigureAwait(false);
Our solution to this issue was to implement our own PopPageModel method which essentially switches the order around so that PopPage is called on the Navigation Stack before a call to RaisePageWasPopped
This is what we call when we want to dismiss the Page
public Task DismissAsync(bool modal = true, bool animate = true)
{
return this.DispatcherService.RunOnUiThreadAsync(
async () =>
{
string navServiceName = this.CurrentNavigationServiceName;
if (this.IsModalFirstChild)
{
await this.CoreMethods.PopModalNavigationService(true);
}
else
{
IFreshNavigationService rootNavigation = FreshIOC.Container.Resolve<IFreshNavigationService>(navServiceName);
await rootNavigation.PopPage(modal, animate);
if (modal)
{
this.RaisePageWasPopped();
}
}
});
}

.Net Session (StateServer mode) not synchronizing if manipulated after request end

I'm getting a bit frustrated with this problem:
I have a web site that manage some files to download, cause these files are very big, and must be organized in folders and then compacted, I build an Ajax structure that do this job in background, and when these files is ready to be downloaded, this job changes the status of an object in the user session (bool isReady = true, simple like that).
To achieve this, when the user clicks "download", a jquery Post is send to an API, and this API starts the "organizer" job and finish the code (main thread, the request scoped one), leaving a background thread doing the magic (it's so beautiful haha).
This "organizer" job is a background thread that receive HttpSessionState (HttpContext.Current.Session) by parameter. It organize and ZIP the files, create a download link and, in the end, change an object in the session using the HttpSessionState that received by param.
This works great when I'm using the session "InProc" mode (I was very happy to deploy this peace of art in production after the tests).
But, my nightmares started when I have deployed the project in production environment, cause we use "StateServer" mode in this environment.
In these environment, the changes is not applied.
What I have noticed, until now, is that in the StateServer, every change I make in the background thread is not "commited" to the session when the changes occurs AFTER the user request ends (the thread that starts the thread).
If i write a thread.join() to wait the thread to finish, the changes made inside the thread is applied.
I'm thinking about use the DB to store these values, but, I will lose some performance :(
[HttpPost]
[Route("startDownloadNow")]
public void StartDownloadNow(DownloadStatusProxy input)
{
//some pieces of code...
...
//add the download request in the user session
Downloads.Add(data);
//pass the session as parameter to the thread
//cause the thread itself don't know the current httpcontext session
HttpSessionState session = HttpContext.Current.Session;
Thread thread = new Thread(() => ProccessDownload(data, session));
thread.Start();
//here, if I put a thread.join(), the changes inside the thread are applied correctly, but I can't do this, otherwise, it ceases to be ajax
}
private void ProccessDownload(DownloadStatus currentDownload, HttpSessionState session)
{
List<DownloadStatus> listDownload = ((List<DownloadStatus>)session["Downloads"]);
try
{
//just make the magic...
string downloadUrl = CartClient.CartDownloadNow(currentDownload.idRegion, currentDownload.idUser, currentDownload.idLanguage, currentDownload.listCartAsset.ToArray(), currentDownload.listCartAssetThumb.ToArray());
listDownload.Find(d => d.hashId == currentDownload.hashId).downloadUrl = downloadUrl;
listDownload.Find(d => d.hashId == currentDownload.hashId).isReady = true;
//in this point, if I inspect the current session, the values are applied but, in the next user request, these values are in the previous state... sad... .net bad dog...
}
catch (Exception e)
{
listDownload.Find(d => d.hashId == currentDownload.hashId).msgError = Utils.GetAllErrors(e);
LogService.Log(e);
}
//this was a desesperated try, I retrieve the object, manipulated and put it back again to the session, but it doesn't works too...
session["Downloads"] = listDownload;
}

Terminating Thread Running an Event

I wrote an API that automates a certain website. However, on the testing stage, I noticed that (not very sure), my thread is not being terminated correctly.
I am using the WebBrowser object to navigate inside a thread, so that it works synchronously with my program:
private void NavigateThroughTread(string url)
{
Console.WriteLine("Defining thread...");
var th = new Thread(() =>
{
_wb = new WebBrowser();
_wb.DocumentCompleted += PageLoaded;
_wb.Visible = true;
_wb.Navigate(url);
Console.WriteLine("Web browser navigated.");
Application.Run();
});
Console.WriteLine("Thread defined.");
th.SetApartmentState(ApartmentState.STA);
Console.WriteLine("Before thread start...");
th.Start();
Console.WriteLine("Thread started.");
while (th.IsAlive) { }
Console.WriteLine("Journey ends.");
}
private void PageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Console.WriteLine("Pages loads...");
.
.
.
switch (_action)
{
.
.
.
case ENUM.FarmActions.Idle:
_wb.Navigate(new Uri("about:blank"));
_action = ENUM.FarmActions.Exit;
return;
case ENUM.FarmActions.Exit:
Console.WriteLine("Disposing wb...");
_wb.DocumentCompleted -= PageLoaded;
_wb.Dispose();
break;
}
Application.ExitThread(); // Stops the thread
}
Here is how I call this function:
public int Attack(int x, int y, ArmyBuilder army)
{
// instruct to attack the village
_action = ENUM.FarmActions.Attack;
//get the army and coordinates
_army = army;
_enemyCoordinates[X] = x;
_enemyCoordinates[Y] = y;
//Place the attack command
_errorFlag = true; // the action is not complated, the flag will set as false once action is complete
_attackFlag = false; // attack is not made yet
Console.WriteLine("Journey starts");
NavigateThroughTread(_url.GetUrl(ENUM.Screens.RallyPoint));
return _errorFlag ? -1 : CalculateDistance();
}
So the problem is, when I call the Attack function, couple times like this:
_command.Attack(509, 355, new ArmyBuilder(testArmy_lc));
_command.Attack(509, 354, new ArmyBuilder(testArmy_lc));
_command.Attack(505, 356, new ArmyBuilder(testArmy_lc));
_command.Attack(504, 356, new ArmyBuilder(testArmy_lc));
_command.Attack(504, 359, new ArmyBuilder(testArmy_lc));
_command.Attack(505, 356, new ArmyBuilder(testArmy_lc));
_command.Attack(504, 356, new ArmyBuilder(testArmy_lc));
_command.Attack(504, 359, new ArmyBuilder(testArmy_lc));
My application most of the times, gets stuck in one of these function (usually happens after the 4th or 5th). When it gets stuck the last log that I see is
Web browser navigated.
I assume it is something to do with termination of my thread. Can someone show me how I can run a thread which runs the DocumentCompleted event ?
I don't see any obvious reason for deadlock, nor did it reproduce at all when testing the code. There are a number of flaws in the code but nothing that yells "here!" loudly. I can only make recommendations:
Consider that you do not need a thread at all. The while (th.IsAlive) { } hot loop blocks your main thread while you wait for the browser code to finish the job. That is not a useful way to use a thread, you might as well use your main thread. This instantly eliminates a large number of potential hang causes.
The state logic in PageLoaded is risky. We cannot see all of it but one glaring issue is that you dispose the WebBrowser twice. If you have a case where you use return without a Navigate() call then you'll hang as described. No need to unsubscribe the event but same story, if you do unsubscribe but don't all Application.Exit() then you'll hang as described. State machines can be hard to debug, thorough logging is necessary. Minimize the risk by moving the Dispose() call and unsubscribing the event out of the logic, it doesn't belong there. And you need to test what happens when any Navigate() call ends up in failure, redirecting to a page you did not expect.
The _wb.Dispose() call is risky. Note that you destroy the WebBrowser while its DocumentCompleted event is in flight. Technically that can return code execution to code that is no longer alive or present. That can trip a race condition in the browser. As well as in the debugger, there is a dedicated MDA that checks for this problem. It is trivially avoided by moving the Dispose() call after the Application.Run() call where it belongs.
The while-loop burns 100% core, potentially starving the worker thread. Not a good enough reason to explain deadlock, but certainly unnecessary. Use Thread.Join() instead.
You create a lot of WebBrowser objects in this code. It is a very heavy object, as you can imagine, you need to keep an eye on memory usage in your program. Especially the unmanaged kind. If the browser leaks, like they so often do, you could technically create a scenario where the WB initializes okay but does not have enough memory left to load the page. Strongly favor using only one WB.
You need to consider that this might well be an environmental problem. On the top of that list is forever anti-malware and firewall, they always have a very good reason to treat a browser specially since that is the most common malware injection vector. You'll need to run your test with anti-malware and firewall disabled to ensure that it is not the cause of the hang.
Another environmental problem is one I noticed while testing this code, Google got sulky about me hitting it so often and started to throttle the requests, greatly slowing down the code. Talk to the web site owner and ask if he's got similar blocking or throttling counter-measures in place, most do. You need to test your state logic to verify that it still works properly when the browser redirects to an error page.
Yet another environmental issue is the WB will display a dialog itself in certain cases. This can deadlock in 3rd party code, very hard to diagnose. You should at least set the WebBrower.ScriptErrorsSuppressed to true but beware of Javascript code in the web page you load that itself creates new windows or displays alert dialogs. Using one WB is the workaround.
Keep in mind that your program can only be as reliable as your Internet connection and the web page server. That's not a terribly good place to be of course, both are quite out of your reach and you don't get nice exceptions to help you diagnose such a failure. And consider that you probably have not yet tested your program well enough yet to check if it can survive such a failure, it doesn't happen enough.
Quite a laundry list, focus first on eliminating the unnecessary thread and temporarily suppressing anti-malware. That's quick, focus next on using only one WebBrowser.
Hans thank you, I was able to fix this issue with one of your ideas. As you spent your time giving me a long answer, I wanted respond in same manner.
2 - I built the state machine structure carefully and with a lot logs (you can see it from my git account) also did a lot of debugs. I am sure that after I'm done navigating, I use Application.ExitThread() and wb.Dispose() only once.
3 - I tried placing the wb.Dispose() outside the event, however I couldn't find any other place where the Thread is still alive. If I try disposing WebBrowser outside the thread which is created inside the thread, the application gives me an error.
4 - I changed the code while (th.IsAlive) { } with th.Join(2000) this is absolutely a better idea but did not change anything. It optimized the code and as you mentioned, it prevented burning 100% core of my CPU.
5 - I tried using a single WebBrowser object which is instantiated in the constructor. However when I tried to navigate inside the thread, the application wouldnt even fire the events anymore. For some reason, I couldn't make it running whit a single WB object.
6,7 - I tested my application with different PC's and diffrent networks(with firewall and non-firewall protection). I changed windows firewall options as well but no travail. On my original code I do have _wb.ScriptErrorsSuppressed = true; so this shouldn't also be the issue.
8,9 - If these are the reasons, I can't do anything about it. But I doubt the real problem is caused because of them.
1 - This one was a good suggestion. I tried implementing my code without using a thread and it is now working fine. Here is how it looks like (still needs a lot optimization)
// Constructer
public FarmActions(string token)
{
// set the urls using the token
_url = new URL(token);
// define web browser properties
_wb = new WebBrowser();
_wb.DocumentCompleted += PageLoaded;
_wb.Visible = true;
_wb.AllowNavigation = true;
_wb.ScriptErrorsSuppressed = true;
}
public int Attack(int x, int y, ArmyBuilder army)
{
// instruct to attack the village
_action = ENUM.FarmActions.Attack;
//get the army and coordinates
_army = army;
_enemyCoordinates[X] = x;
_enemyCoordinates[Y] = y;
//Place the attack command
_errorFlag = true; // the action is not complated, the flag will set as false once action is complete
_attackFlag = false; // attack is not made yet
_isAlive = true;
Console.WriteLine("-------------------------");
Console.WriteLine("Journey starts");
NavigateThroughTread(_url.GetUrl(ENUM.Screens.RallyPoint));
return _errorFlag ? -1 : CalculateDistance();
}
private void NavigateThroughTread(string url)
{
Console.WriteLine("Defining thread...");
_wb.Navigate(url);
while (_isAlive) Application.DoEvents();
}
private void PageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Console.WriteLine("Pages loads...");
.
.
.
switch (_action)
{
.
.
.
case ENUM.FarmActions.Idle:
_wb.Navigate(new Uri("about:blank"));
_action = ENUM.FarmActions.Exit;
return;
case ENUM.FarmActions.Exit:
break;
}
_isAlive = false;
}
This is how I was able to wait without using a thread.
The main problem was probably as you mentioned in number 3 or 5. But I wasn't able to fix the problem as I spent couple of hours.
Anyway thanks for your help it works.

Please help me -- c# web application I need to load 3 user controls at the same time

I have a page that has 3 used controls that are dynamically added to the page. One of these use controls hits a class that pulls a record set from the database (a rather quick request) and the other 2 user controls hit a single class and return a dataset from the database. The 2 that share a single class take an average of 10 seconds to return the data. Now what I want is for a user to hit the page and see 3 loading bars and have each user control load in the background. I want them all to start loading at the same time.
I have tried to call threading on the page containing the user controls like this:
protected void Page_Load(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(Load1));
t.Start();
t.IsBackground = true;
t.Priority = ThreadPriority.Highest;
t.Join();
Thread s = new Thread(new ThreadStart(Load2));
s.Start();
s.IsBackground = true;
s.Priority = ThreadPriority.Highest;
s.Join();
Thread r = new Thread(new ThreadStart(Load3));
r.Start();
r.IsBackground = true;
r.Priority = ThreadPriority.Highest;
r.Join();
}
private void Load1()
{
Control ctrl1 = LoadControl(#"\Controls\ctrl1.ascx");
Pnl1.Controls.Add(ctrlPreSolic);
}
private void Load2()
{
Control ctrl2 = LoadControl(#"\Controls\ctrl2.ascx");
Pnl2.Controls.Add(ctrlActive);
}
private void Load3()
{
Control ctrl3 = LoadControl(#"\Controls\ctrl3.ascx");
Pnl3.Controls.Add(ctrlNonActive);
}
however this is not working, it is always loading the first control, then the second, then the third in a line not all at the same time. Is this the wrong way to approach a web application? Can you thread a web application? I really need help with this if anyone can help me out!
Using threads like you do, is definitely not the way to load web controls asynchronously.
If you insist on your approach, all the threads should join only afterwards, else each will block the execution, and effectively eliminate any multithreading advantages you tried to gain.
Instead of using the server for this, I'd suggest using an AJAX call. You could use a third party library like jQuery and its load method, and it would fire them off as fast as possible.
Still in order, but no one call would be waiting on another, which is what I think you're trying to avoid.
This definitely feels like something that should be on the client side.

C# timer issue

Hello I'm currently having an issue with a timer in a program I'm developing. The timer runs and calls methods which retrieve Windows Management Information from remote PC's after a set period of time and repeat this.
The first time the timer calls these all is well, however the second time, after the timer has completed its task, it loops through itself again and the third time it runs it does it 3 times etc. The for loop in the code below works fine its the timer itself.
So any help would be appareciated and if you require any further details please let me know.
Below is my code:
private void tmrStore_Tick(object sender, EventArgs e)
{
string ipAdd;
ipAdd = "127.0.0.1";
List<TblServer> Server;
WMIInfo localDB = new WMIInfo("Data Source=|DataDirectory|\\WMIInfo.sdf");
Server = localDB.TblServer.ToList();
if (Server.Count == 0)
{
}
else
{
for (int counter = 0; counter < Server.Count; counter++)
{
CPUStore cpu = new CPUStore();
cpu.Store(Server[counter].IpAdd);
HDDStore hdd = new HDDStore();
hdd.Store(Server[counter].IpAdd);
MemStore mem = new MemStore();
mem.Store(Server[counter].IpAdd);
//delete items over 24 hours old
}
}
Try disabling the timer before performing the management task, then reenabling:
tmrStore.Enabled = false;
try{
// do stuff
}finally{
tmrStore.Enabled = true;
}
The cause of the problem is probably that the body of your timer handler takes longer to execute than your Timer.Ticks value, so your timer events start to stack on top of each other.
You might also consider putting this code in a thread instead of a timer, so that it's independent of your user interface.
My first guess is that you are setting your Timer.Tick event in a place that is being executed multiple times. I would try searching for "tmrStore.Tick +=" to see where all the methods are being added to the event.
Right I've resolved the issue its because I had a class I was using to write the retrieved information into text boxes and within that I called a new instance of the form to gain access to the text boxes doh!
Thanks for your help though guys no doubt I'll be back soon for some more lol

Categories

Resources