How to create a Thread in windows phone 8.1 - c#

Hi, I've tried to create a Thread in windows phone 8.1 without success.
Althought in msdn documentation is written that Thread is supported, actually do not works (https://msdn.microsoft.com/en-us/library/274eh01d(v=vs.110).aspx)
so I can't figureout how can I create a Thread, I want to create a Syncronization context to execute async on a single thread (not current thread), but just to achieve what event loop do
thanks
update for further information:
I would be more specific:
platform is universal app, so I mean windows 8.1 & windows phone 8.1, no silverlight ecc.
I need to create a thread because I want to create a syncronization context to execute some async code, here is an example about what I mean http://blogs.msdn.com/b/pfxteam/archive/2012/01/20/10259049.aspx
the difference from this link is I would use a specific thread for all async code instead current thread.
thanks again

If you just want to run some code asynchronously, look into using Task.Run. It provides a much simpler mechanism than managing threads yourself.
If you are building an Universal Windows Phone 8.1 app, you can't use the .NET Thread type. If you need more control over the execution, you can use the WinRT ThreadPool.
Finally, if you really really need lots of control, there's always CreateThread if you want to write native code (this has recently been added to the allowed list of APIs for Store Apps, although MSDN isn't updated yet).

Related

Get thread information in Xamarin PCL

I start some parallel tasks from a PCL with
Parallel.Invoke(() => ExecuteTaskAAsync(), () => ExecuteTaskBAsync());
Now I want to know on which thread these tasks are running.
Thread.CurrentThread.ManagedThreadId seems to be only available in the iOS
and Droid project but not in the PCL or UWP project. What are my possibilities? If I would use DependencyService I also won't know on which thread TaskA is running, because there is not relationship between the started task and DependencyService.
How can I get thread information (id, name, ...) in a PCL?
When you set a breakpoint you can get into the Tasks Window and get some more information. Also you have a drop down on the top to select a process, choose between different threads and get some info about the stack frame. So one should be able to also get this information in code?
Edit:
Seems that the informations in the Tasks Window is only available when running on UWP.
The concept of threads is completely absent in Windows Universal Apps and the use of tasks is recommended instead of threads.
I haven't found official documentation on the reasons behind it, but there's an ongoing discussion at the CoreFX's github repo.
So it looks like what you are looking for isn't currently possible if you want to support UWP projects.
You mal alternatively look for the TaskScheduler that manages the currently running task:
var poolId = TaskScheduler.Current.Id;
This way you can at least find out, if your method is running on the same ThreadPool (eg: They are executed on any background threads, or the UI thread).
As far as I know, to find the TaskScheduler for the UI, you have to call TaskScheduler.Current.Id while you are on the UI Thread and just store it in a variable.
This works for me
Environment.CurrentManagedThreadId

How to programmatically close the print UI on a Windows 10 application?

I am designing a Windows 10 Universal application using this https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing/cs repository as a guide. I currently have fully functional printing by calling:
await Printmanager.ShowPrintUIAsync();
In my application there is also an activity timer that logs the user out after a certain amount of time inactive. This part is working fine, but I am unable to close the print ui upon logout.
Note: Normally to close a windows async operations, you can do something similar to:
IAsyncOperation<bool> printOperation = Printmanager.ShowPrintUIAsync();
printOperation.Cancel();
This works for other AsyncOperation occurences, but I cannot get it to work for the print UI, as the print UI is not a child process of the app, but is a seperate process itself
Thanks in advance!
Also, it seems there was a solution to kill processes in Windows 8 which is no longer supported in Windows 10 applications (Process.GetProcessByName .... or FindWindow)
Perhaps there is someway to kill a Windows 10 process by name?
You can't do that.
All the Metro-style applications work in the highly sandboxed environment and there is no way to directly start an external application.
Taken from this article
maybe the solution is creating a proxy.
How to Start a external Program from Metro App
This could help you, because you can kill the process indirectly.
I hope this helps.

background task in windows phone 8.1

I am using a 3rd party rest api to query data and display it in my app. I have to perform a task like at night 12 approx. it will perform a background task to query data from rest api and update live tile and generate notification. I would like to use only C# only for this task. I don't know what will be best approach to do this task. But I using below code to perform background task to do this which is not working. Not sure why?
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = taskName;
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.TaskEntryPoint = typeof(BackgroundTask.BackgroundTask).FullName;
taskBuilder.Register();
and from background task I am querying data and generating toast notification.
Any help why this code is not working or when this task will fire. Is there any better approach to do above task?
Thanks
Regarding the code you have not working...
For Windows Phone 8.1 unlike Windows 8\8.1, you are required to call BackgroundExecutionManager.RequestAccessAsync() (search MSDN\internet) for ANY background task before registering task(s) whereas in Windows this is only required for some tasks. Make sure your code calls this and validate the returned value before registering your background task(s).
Regarding knowing if your task "worked"...
It's a good idea to have the background task implementation run (IBackgroundTask::Run()) independent of the trigger\conditions you've set to ensure it performs without issue by debugging it. See instructions in the following link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/jj542416.aspx.
Regarding your use of SystemConditionType.InternetAvailable...
I'm not 100% about this but I'm pretty certain this will be redundant given you already have a SystemTriggerType.InternetAvailable. I don't know of a situation where the trigger would fire but the condition wouldn't be true.
Regarding the requirement you've mentioned...
If I understand your requirement correctly you have different options here:
If your app is a Windows Phone XAML app that need to run based on time, I would recommend either TimeTrigger or MaintenanceTrigger triggers (as opposed to the SystemTrigger). These are both Background Tasks. For general info on Background Tasks and links to the TimeTrigger and MaintenanceTrigger documentation see this MSDN link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh977056.aspx.
If your app is a Windows Phone Silverlight 8.0 app you can use Background Agents, specifically either PeriodicTask or ResourceIntensiveTask. See the links posted by others or search the MSDN\internet for more info.
If your app is a Windows Phone Silverlight 8.1 app you can use the option in either 1 or 2 above.
I think you should try using PeriodicTask. Also consider the constraints mentioned in the link.
create one class with output Type :Windows Runtime Component
and put your Class that inheritance from IBackroundTask so this work
if you use from emulator for launching app, i think your app for register task not active in emulator.

Windows Phone 8: access UI Dispatcher in C++ code

I am working on an Windows Phone 8 Application that uses mixed C# for UI and C++ for the underlying backed. I have some instances where I need to run some C++ code on the UI dispatcher thread (IE GetGeopositionAsync()).
I have tried a method of making a callback from C++ to C# that runs in the UI thread and then calls a C++ method and runs:
auto window = Windows::UI::Core::CoreWindow::GetForCurrentThread();
However this is returning null. Is there any way for me to accomplish this?
I had a similiar problem some days ago, as it has always returned null. I ended up using in WinRT. According to MSDN (1, 2) you should be able to use this in WP8 as well.
CoreApplication.MainView.CoreWindow

Dispatcher is not coming on C# Windows Forms Application

I have a C# Windows Forms Application with a progress bar. I want to progress that dynamically based on some method status. I have that method in a separate class, and am creating a new thread for that method, with a delegate to pass the status of the method to the parent thread.
The same situation I am able solve in a WPF application using progressbar.Dispatcher but in a Windows Forms application there is no Dispatcher, even if I use the System.Threading namespace.
progressbar.Dispatcher seems to be available only in a WPF Application.
In winforms you can use the ProgressBar.Invoke or ProgressBar.BeginInvoke to update the control from another thread.
In Windows Form application BackgroundWorker should fit perfectly for your task. In particular, it has ReportProgress method to send progress depending on your calculations and ProgressChanged to track changes from UI thread. See MSDN article for full details
In WinForms the Invoke/BeginInvoke methods are directly on the control objects as you can see from the docs of System.Windows.Forms.Control. So you'd have progressBar.BeginInvoke(...) for example.

Categories

Resources