locked
Background task class access, which is placed in other project

    Question

  • Hello! I'm creating my first windows 8 store app and I need your help with background tasks :)

    My app is connected with local social media API. User connects his local social media account with Windows 8.1 app. It makes a lot of api calls and received data is saved into application`s local folder.

    This data must be synchronized every 30 minutes (Again, It will make a lot of API calls). It sounds like background tasks feature. I have written an API class, which contains few methods (e.g. syncAccount, apiCall, etc.).

    So.. Where is problem? I have Solution, which contains 2 projects:
    1) BackgroundTasks (Windows Runtime Component)
         -- AutoSync.cs (my task, which runs every 30 minutes)
    2) socialmedia (default)
         -- MainPage.xaml
         -- MyClass.cs (API Class)

    How can I use MyClass.cs in AutoSync.cs? MyClass.cs is used in socialmedia project, but I also need that class access in BackgroundTask - AutoSync. Project socialmedia is referenced to project BackgroundTasks.

    Any suggestions? :)

    Tuesday, June 3, 2014 9:49 PM

Answers

  • Whoah.. I finally find a solution, which really works. So If anybody else have the same question, then what I did is:

    1) Created ANOTHER project (Class library), called e.g. MyLibrary. So at this moment I have 3 projects (BackgroundTasks, socialmedia, MyLibrary)
    2) Now add MyLibrary reference to BackgroundTasks and socialmedia. Right click on project name -> Add -> Reference. From left side menu select Solution and then from the list, make a check in MyLibrary and press Ok. Remember to do for both other projects.
    3) Now put your classes in MyLibrary project. For example, I put there file: MyClass.cs  Make sure your class is public. In my case: public class MyClass {}
    4) Next what to do is open AutoSync.cs (background task class) and add using MyLibrary; If you want to use anywhere else MyLibrary, you also need to add that line. In my case I put this line in AutoSync and MainPage.
    5) Now feel free and use your MyClass

    Everything, that you want to share between background task and Main Project (for me: socialmedia) must put into MyLibrary project.

    I hope, that I helped someone else :)

    Wednesday, June 4, 2014 6:49 AM

All replies

  • What do you mean by accessing the class in BackgroundTask - AutoSync?

    Do you need just the code or do you need the memory access?

    You can get the same functions by linking the class file into both projects or by including it in a library that you call from both. You cannot share the memory, but you can use the local folder or settings to pass data.

    --Rob

    Wednesday, June 4, 2014 1:27 AM
    Owner
  • Thanks for your answer.

    Well. File MyClass contains many methods, which I use in project socialmedia. I want to call those methods from AutoSync too.

    With linking did you meant referencing or MyClass copying into AutoSync project?

    Wednesday, June 4, 2014 5:51 AM
  • Whoah.. I finally find a solution, which really works. So If anybody else have the same question, then what I did is:

    1) Created ANOTHER project (Class library), called e.g. MyLibrary. So at this moment I have 3 projects (BackgroundTasks, socialmedia, MyLibrary)
    2) Now add MyLibrary reference to BackgroundTasks and socialmedia. Right click on project name -> Add -> Reference. From left side menu select Solution and then from the list, make a check in MyLibrary and press Ok. Remember to do for both other projects.
    3) Now put your classes in MyLibrary project. For example, I put there file: MyClass.cs  Make sure your class is public. In my case: public class MyClass {}
    4) Next what to do is open AutoSync.cs (background task class) and add using MyLibrary; If you want to use anywhere else MyLibrary, you also need to add that line. In my case I put this line in AutoSync and MainPage.
    5) Now feel free and use your MyClass

    Everything, that you want to share between background task and Main Project (for me: socialmedia) must put into MyLibrary project.

    I hope, that I helped someone else :)

    Wednesday, June 4, 2014 6:49 AM
  • Yes. That's what I meant by including it in a library.

    You can also add the same cs file to multiple projects by choosing Add.Existing item..., selecting the file, and using the Add as Link button on the file browser. This will link the file so that both projects refer to the same file.

    --Rob

    Wednesday, June 4, 2014 7:31 AM
    Owner