YogI
YogI

Reputation: 63

Pass Parameter or Arguments to a Background task in Uwp

I am creating a uwp app in which I want to take some data from user say from a textbox and then pass it to a background task. But when I am trying to add project reference to the background task I am getting a circular reference error. So is there any way to pass arguments maybe a overload of run function or anything else. Thanks in advance.

Upvotes: 6

Views: 2060

Answers (2)

Shubham
Shubham

Reputation: 199

Romasz has explained it perfectly but in your case you can get the user data from the textbox by doing these steps:

1.Declare this in MainPage.xaml.cs(or your xaml page)

var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["user"] = User.Text;

2.Now get the data in BackgroundTask.cs using these

var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
string user = localSettings.Values["user"].ToString();

Here the variable user has the texbox data you wanted from the mainpage.

Note:I have assumed that you have named your textbox "User"

Upvotes: 7

Romasz
Romasz

Reputation: 29792

There is no direct way to do what you want, each process (for example BTask and UI) has its own memory and it cannot be accessed just like that.

You will need some kind of a broker for this purpose - for short communication I think you can use LocalSettings, which should work fine - though in case of complex objects you may need to serialize them first. For more advanced communication you may also think of using a file in LocalFolder or other place that is accessible for all processes.

In some cases there may be a problem of synchronization of processes (or accessing shared resources, like files) - for this, there are designed objects for global synchronization, eg. Mutex, EventWaitHanle.

Upvotes: 4

Related Questions