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
Related
I have a C++ application (calling functions of an SDK for a specific hardware component), and want to display its data in a C# GUI. The C# part is a DLL which the C++ calls. (This is by request from the customer so I don't have much choice about it.)
I'm not very well versed in C#, so might be missing something obvious, but I'm running into problems both displaying the GUI and updating it.
I access the C# code using this method, with code roughly like this (ptr is a class variable):
// Initialize COM.
CoInitialize(NULL);
ptr = new IPtr(__uuidof(ManagedClass));
(*ptr)->ShowForm();
then in another thread:
if (updating) (*ptr)->Update(data)
On the C# side we have:
FormClass myForm;
void ShowForm()
{
myForm = new FormClass();
Application.Run(myForm);
}
void Update(Data data)
{
myForm.Update(data)
}
When I use Application.Run or ShowDialog to show my GUI, the form shows nicely but the update makes the application crash. Using Show has the GUI get stuck. Using BeginInvoke resulted in the GUI never appearing.
Is there any recommended way for me to start the GUI given this setup? Would it help to somehow use Invoke/BackgroundWorker in Update rather than calling myForm's method directly?
You must update C# GUI on the the UI thread. See this answer for how to synchronize from another thread to the GUI thread.
C# Windows Forms Application - Updating GUI from another thread AND class?
You might also find the Debug Location toolbar handy to determine what thread you are currently in when debugging in Visual Studio
https://blogs.msdn.microsoft.com/davedev/2012/07/18/where-is-the-suspend-resume-and-terminate-toolbar-in-visual-studio-2012-ultimate/
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).
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.
My application is derived from a Windows Phone 8 Direct3D Xaml sample, where a C# program instantiates a WinPRT component that contains a Direct3D device. The C# code runs on the UI thread and the component code runs on a separate application thread. The WinPRT component works with a C++ pre-compiled library that contains variables declared per-thread, static __declspec(thread).
The variables get initialized when needed in the application thread, but the shutdown (caused by the lifecycle Closing event) is called on the UI thread. The shutdown process uses the wrong set of per-thread variables which haven't been initialized and causes a crash.
I haven't found any method on the WinPRT component that gets called on the application thread during shutdown. I was hoping to find one to hook my stuff into but there doesn't seem to be any.
So, how do I call the shutdown code to be run on the application thread from the UI thread?
WinPRT severely limits the threading functionality available to me. I haven't been able to find a workable solution. I looked at the Dispatcher class, but on WinPRT it's only used to run code on the UI thread from a worker thread and not vice versa. What other options might there be?
I am creating a video player application with a UI in C# and the video decoding and display (DirectX) in C++.
The C++ code is compiled as a DLL and interfaced with the C# UI.
To maintain the correct display frame rate I need to create a secondary thread either in C++ DLL or C# which can do accurate timing and call the display function at right intervals.
However, creating a secondary thread which posts display to the window created by the primary thread (from C# GUI) creates access violation and results in a crash.
However, if I use a timer object in C# to display, the player works but I am unable to maintain the right frame rate due to it's coarse granularity.
What would be a good solution to handle this?
I think the crashes you experience are caused by the fact that you can't access Windows Forms controls from outside the main thread.
Consider using Control.Invoke() to invoke the execution you need on the main thread.
Bear in mind though that Control.Invoke() uses Windows' message queue to pass the request through, so expect some inaccuracies.