How to run the timer tasks asynchronously? - c#-4.0

Hi am developing a windows phone 8 app using C# and xaml.
My previous team has developed some code, they have used many methods in timer control.
when it is updating all the methods are calling and its blocking the UI.
Is there any another way to use the timers asynchronously so that the UI can not be blocked.
Thanks in advance

you should use the "xaml binding" to updating the UI

There are several Timer classes in Windows Phone.
System.Windows.Threading.DispatcherTimer: runs on UI thread and makes UI unresponsive when executed.
System.Threading.Timer: executes on a ThreadPool thread, therefore can not update UI directly. For an example how to make a cross-thread call to update UI, see the example in the link.
There is also a ThreadPoolTimer, seems it works like a System.Threading.Timer - runs on ThreadPool thread, just has different methods. But I have not used it.
So to answer your question, if a timer event blocks UI, then it is likely a DispatcherTimer, replace it with System.Threading.Timer, reference the code sample in the previous link.

Did you try using the DispatcherTimer?
How to Increment timer asynchronously ?
Or else you could go with the CountdownTimer.
How do you run a synchronous timer in C#?
as #kennyzx said there are many ways to do it, but the choice is yours.
a sample on CountdownTimer from the Toolkit

Related

The JavaFX Concurrency | When to use it, how to use it right?

Maybe it's a simple question, but I don't get it. When should I use concureency in my javafx project? Is it right that I should use for every task, which do some action in the background, the Concurrency API? So every action in my controller class, which has nothing to do with the UI should be executed in a single task?
I really don't get it how to use this right....
Whenever you have a task that may take sometime to get executed or there is a possibility of delayed response, you do not want your JavaFX Application thread to wait for it, because, as long as the JavaFX Application thread waits for the response, the UI becomes unresponsive.
A few examples where you may want to use a background thread is :
An I/O operation
A web service call
From the JavaFX documentation :
Implementing long-running tasks on the JavaFX Application thread inevitably makes an application UI unresponsive.
On the other hand, if you have minor calculations or some task which can be completed in a jiffy (I am not sure if this is the correct word, but I hope you can relate to what I want to say) and will not put the JavaFX Application thread on wait, you can execute them on the same thread.

Asynchronous call of inprocess COM-DLL from VSTO Excel Addin?

I am developing an application level VSTO 4 Addin for Microsoft Excel 2007 / 2010.
The result is a windows forms based DLL using .Net 4 Client Profile.
Now I have to use a legacy COM-DLL. It is no problem to set the reference and access the COM-Methods via COM-Interop from .Net.
But the the (synchronous) method I need to call can take a minute or longer to get back.
I know your answer:
Use a worker thread...
I have used The Task Parallel Library to put the long lasting operation in a worker task and keep the GUI (Excel) responding.
But: The inprocess COM-Call (in the worker task/thread) still seems to block my GUI-Thread.
Why? Is it because Excel is always running as STA (Single Thread
Apartment)?
How can I keep the Excel GUI responding?
Is there a way to make it really asynchronous?
Thanks for any answers,
Jörg
Finally, I've found an answer to this topic:
I've readed a lot about COM Threading Models and then spoke to the developer of the COM-DLL I am calling as an InProc-Server.
Together we changed the threading model of the COM-DLL:
OLD (blocking): Single-Threaded Apartment (STA), (ThreadingModel=Apartment)
NEW (working): Multi-Threaded Apartment (MTA), (ThreadingModel=Free)
Since we have our own synchronization mechanisms in the COM-DLL, there are no problems caused by the missing synchronization via the standard Windows message queue.
Problem was, that even the UI Thread was idle and even if it did DoEvents, the important windows messages (WM_Paint, etc.) were not delivered.
Now they are. The UI is responding at every time and the call to the COM-DLL is still done in a worker thread (as mentioned above, it's a ThreadPool thread which is used by the Task Parallel Library).

Is there a way to run NSUrlConnection in a background thread using monotouch?

I know what you are think, why would he want to run an Asynchronous method in a background thread? The answer is that I am downloading some files locally and the local write process takes a lot of time hanging the UI. This is the case even if I use the async methods of the FileStream class, and creating a new thread every time that ReceivedData is called is to resource intensive, not to mention it opens a lot of problems securing the integrity of the file. All these would of course not be a problem when using the .net classes WebClient or HTTPWebRequest. Only that as mentioned elsewhere questions/6282278 these classes doesn't work reliably.
I have tried running the NSUrlConnection from a BackgroundWorker, a ThreadPool.QueueUserWorkItem, a NSOperationQueue, and for some reason NSUrlConnection only seems to work when called from the Main Thread. Any ideas would be appreciated, thanks.
I'd just use the standard .Net classes: HttpWebRequest or WebClient.
I'd avoid any NS-Apple APIs if possible.

Multithread Form Application (.NET 4.0)

I'm currently working on a solution that has two projects, a console and a form application. The console application is the main entry point to my application, and from the console the user would run the form application.
The problem is, when the user boots the form application the rest of the business logic (from the console app) won't run until the form is closed. My first thought was to use a background worker for the form, but the business logic in the form project already uses a background worker (and I only have two CPUs...). Perhaps this could be my ignorance for multithreading, but is there a way to do this?
Any thoughts are much appreciated!
Cheers
Well, this is pretty unusual. In general, it doesn't make a lot of sense to provide the user with a nice GUI and still leave a console window up and interactive.
But yes, calling Application.Run() or Form.ShowDialog() is going to block the thread. It has to, the message loop needs to be running to keep the GUI alive. If you do this, be sure to put the [STAThread] attribute on the Main() method.
The only other decent alternative is to start a thread. This isn't a problem, a UI thread doesn't burn any CPU cycles. Code only ever runs when the user does something, it's otherwise idle 99% of the time. Be sure to call the thread's SetApartmentState() method before you start it, STA is required.

BackgroundWorker Thread in IIS7 - FAIL!

Just wondering if anyone has had any trouble using a BackgroundWorker Thread in a site running under IIS 7 in Integrated Pipeline mode?
I am trying to use such a beast to update the database schema (admin function, obviously), and it works perfectly in Cassini, but when I deploy to IIS 7, the thread gets about one line of code in and silently ends. Is there a way to tell why a thread ended?
Thanks in advance.
It is probably an exception. Perhaps you're running with different set of rights between the two environments.
I don't know but I have to ask: why do you use BackgroundWorker in first place? Its main purpose is to ease the threading on UI elements.
Instead why don't you use a "normal" thread or any other non-UI mechanism?
You do not want to use BackgroundWorker, Thomas from ASP.NET team just posted some information about executing code in separate threads in ASP.NET: http://blogs.msdn.com/tmarq/archive/2010/04/14/performing-asynchronous-work-or-tasks-in-asp-net-applications.aspx

Resources