IsolatedStorageSettings freeze UI thread WP8 - multithreading

in my app when a user logs in to skydrive and the status changes I am changing 4 settings in IsolatedStorageSettings.
It is done the same as http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx
When I modify these 4 values though my UI thread is freezing for a second or two as my progress bar gets choppy. I tried running it in a thread like so but still the same issue.
Thread ContactThread = new Thread(new ThreadStart(UpdateContact));
ContactThread.Start();

Related

Lightswitch task on a background thread

I have a lightswitch app which sends an email when a new job is added. I added a sendemail helper class which is called when the record is inserted.
This works however the interface hangs on save waiting for the email to be sent.
I would like to perform this asychronously so that the user can go on and do his thing while the email sends in the background.
I've tried creating a new thread in the inserted part, hoping it would spin off the thread and then return to the user, but it doesn't work, it is still waiting for the thread to finish.
Thread caseemail = new Thread(new ParameterizedThreadStart(newSendmail.generateCaseEmail));
string[] paramsToPass = new String[] { entity.ProjectNumber, entity.CreatedBy, entity.TheProjectClientManagerFixed, entity.ProjectName };
caseemail.Start(paramsToPass);
How should I be doing this?
So in the end this code works, the errors were from problems access dataworkspaces from the other thread, which is obviously not allowed.
I will leave this here as an example of how to spin off a task into the background from lightswitch, leaving the interface responsive while lightswitch goes away and does something else.

Windows phone map broken multitasking

When user opens map, my app starts loading much data from DB to show on map (when it's loaded).
But what i see is that map control stops loading/showing tiles when BG thread is loaded with hard work.
AFAIK WP7 doesn't support thread priorities.
This is really weird. Simple while(true) on BG thread stops map from showing new geodata on zoom/pan.
Maybe any ideas ?
Repro project: https://www.dropbox.com/s/21fmgepcdzf3u1n/Map_bug_Repro.zip
If you start it - map won't load. If you edit MainPage.xaml.cs and comment thread creation - it will work fine.
Thanks!

Application.GetResourceStream in non-ui thread

I have a problem with Silverlight application.
Suppose I have an xml file in resources stream. I get it as usual with something like this:
StreamResourceInfo sr =
Application.GetResourceStream(new Uri("uri goes there", UriKind.Relative));
var xml = XElement.Load(sr.Stream, LoadOptions.SetBaseUri);
And everything works just fine. But if the same code runs in the background thread (via async/await or, to be simple, in background worker) it always returns null.
I’ve heard about a bug in VS with similar problems (returning null) so I’ve tried to clean solution, delete obj folders etc. but nothing works — in background thread this code always return null for resources stream.
You can't access UI resources in background thread. Ideally you should access it in UI thread and pass it to background thread.

Is Crystal Report with Output = toWindow from Delphi background thread possible?

is it possible to execute a crystal report (TCrpe component) from a Delphi non VCL main thread when Output = toWindow?
I execute reports from a background thread, and when Output is toPrinter or toExport, everything is fine.
I know that creating forms in a
Delphi non VCL main thread generally is a
bad idea.
When a Crystal
Report is executed, and Output=toWindow, the component creates the output
window on its own. So I cannot prevent that the window is created by the background thread.
So is there a clean way to
execute the report in a background
thread, and display the result in a
cleanly created form?
Version: Crystal11VCL7
The following code does not work:
procedure TMyThread.Execute;
var
cr: TCrpe;
begin
inherited;
cr:= TCrpe.Create(nil);
cr.ReportName:= 'C:\TestReport.rpt';
cr.Output:= toWindow;
cr.WindowParent:= Form1; //This is the main form
cr.Execute;
end;
It seems like the report will be created and immediately destroyed afterwards.
When I enter a message loop right after cr.Execute (while true do Application.ProcessMessages; - which is obviously a very bad idea), the report window is shown nicely.
Any idea, how to do it right? Or is it simply not possible? Thanks!
I haven't had any experience with Crystal for many years (thank goodness) but in the absence of any other reply I would consider approaching the problem from a different angle.
What I do is let the report form be created in the main thread - because, as you've said, it's not a good idea to do the VCL stuff in the background - but I generate all report /data/ in a background thread. Once the data is loaded then the thread signals the main form (or whatever) via a windows message and the report goes and connects up to the dataset/datasource.
Initially I display a blank label over the client area of the report window with a message saying something like 'Report loading...' then once the message is received it hides the label and attaches the data. Works really well here and keeps the UI responsive.

Sheet and thread memory problem

recently I started a project which can export some precalculated Grafix/Audio to files, for after processing.
All I was doing is to put a new Window (with progressindicator and an Abort Button) in my main xib and opened it using the following code:
[NSApp beginSheet: REC_Sheet modalForWindow: MOTHER_WINDOW modalDelegate: self didEndSelector: nil contextInfo: nil];
NSModalSession session=[NSApp beginModalSessionForWindow:REC_Sheet];
RECISNOTDONE=YES;
while (RECISNOTDONE) {
if ([NSApp runModalSession:session]!=NSRunContinuesResponse)
break;
usleep(100);
}
[NSApp endModalSession:session];
A Background Thread (pthread) was started earlier, to actually perform the work and save all the targas/wave file. Which worked great, but after an amount of time, it turned out that the main thread was not responding anymore and my memory footprint raised unstoppable. I tried to debug it with Instruments, and saw a lot of CFHash etc stuff growing to infinity.
By accident i clicked below the sheet, and temporary it helped, the main thread (AppKit ?) was releasing it's stuff, but just for a little time.
I can't explain it to me, first of all I thought it was the access from my thread to the Progressbar to update the Progress (intervalled at 0,5sec), so I cut it out. But even if I'm not updating anything and did nothing with the Progressbar, my Application eat up all the Memory, because of not releasing it's "Main-Event" or whatsoever Stuff.
Is there any possibility to "drain" this Main thread Memory stuff (Runloop / NSApp call?). And why the heck doesn't the Main thread respond anymore (after this simple task) ???
I don't have a clou anymore, please help !
Thanks in advance !
P.S. How do you guys implement "threaded long task" Stuff and updating your gui ???
while (RECISNOTDONE) {
if ([NSApp runModalSession:session]!=NSRunContinuesResponse)
break;
usleep(100);
}
Is there a reason you're doing that? A sheet will block its parent window without you having to do anything like this. You can prevent quit within your app delegate.
If you really do need the above code for something, try creating an autorelease pool before sending the runModalSession: message, then draining it after (but before you compare to NSRunContinuesResponse and break).

Resources