locked
Update variable in a running for loop from another namespace RRS feed

  • Question

  • Hi,

    I hope you can find the solution to my problem. I spent several hours trying to solve it without any good result.

    I have the "choose" variable defined as follows:

    namespace Schedule
    {
        using System;
        using System.Diagnostics;
        using System.Globalization;
        using System.Linq;
        using Windows.Data.Xml.Dom;
        using Windows.System.UserProfile;
        using Windows.UI.Notifications;
        using Windows.Storage;
    
        public static class Scheduler123
        {
            static volatile bool choose;
    
            public static bool Choose
            {
                get { return choose; }
                set { choose = value; }
            }
    
            public static void CreateSchedule()
            {
                var tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();
                var plannedUpdated = tileUpdater.GetScheduledTileNotifications();
    
                DateTime now = DateTime.Now;
                DateTime planTill = now.AddHours(4);
    
                DateTime updateTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0).AddMinutes(1);
                if (plannedUpdated.Count > 0)
                    updateTime = plannedUpdated.Select(x => x.DeliveryTime.DateTime).Union(new[] { updateTime }).Max();
    
                for (var startPlanning = updateTime; startPlanning < planTill; startPlanning = startPlanning.AddMinutes(1))
                {
    
                    try
                    {
                        
                        if (choose == true)
                        {
                            Debug.WriteLine("Choose is true");
                           
                        }
                        else
                        {
                            Debug.WriteLine("Choose is false");
                        }
    
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("The exception is: " + e.Message);
                    }
                }
            }
        }
    }

    "choose" is used inside a for loop. I need to change the value of this variable, while the for loop is running, clicking a button in the UI. The codebehind of the UI is placed in a different namespace and I call the variable as follows:

    private void button_Click(object sender, RoutedEventArgs e)
            {
                Scheduler123.Choose = true;
            }

    Visual Studio does not detect any error or exception but the value of the variable is always false.

    Thank you in advance.


    Gabriele

    Sunday, December 1, 2013 3:39 PM

All replies

  • this is probally becasue the loop finishes before you set choose on true. your depending on context switches and this make very unpredictable result of your code. what you try to do in first place?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Sunday, December 1, 2013 6:55 PM
  • this is probally becasue the loop finishes before you set choose on true. your depending on context switches and this make very unpredictable result of your code. what you try to do in first place?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Hi Dave and thanks for your attention. I took this code from the Internet some months ago. It has your name inside so, of course, you are the best person to help me (if you want and if you can). I'm trying to update it to allow the user to choose the tile layout between 2 different possibilities pressing a button.

    The "if-else" code inside that "for" loop includes the 2 xml codes that describe the 2 layouts.


    Gabriele

    Monday, December 2, 2013 3:45 PM
  • Hi,

    There is not enough information to test why the problem occur. Please upload your project to SkyDrive and share a link in the forum.

    Best wishes


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    • Edited by Anne Jing Wednesday, December 4, 2013 7:49 AM edit
    Wednesday, December 4, 2013 7:48 AM
  • try this as choose implementation:

    public static bool Choose
            {
                get
                {
                    if (ApplicationData.Current.LocalSettings.Values.ContainsKey("ChooseValue"))
                        return (bool)ApplicationData.Current.LocalSettings.Values["ChooseValue"];
                    return false;
                }
                set { ApplicationData.Current.LocalSettings.Values["ChooseValue"] = value; }
            }


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, December 4, 2013 8:53 AM
  • Sorry for the delay but I tried to implement Dave's suggestion. Unfortunately I wasn't able to solve.

    I updated my complete solution here on skydrive https://skydrive.live.com/?cid=1B721133DC03E67C&id=1B721133DC03E67C%21131

    as suggested by Anne, hoping you can solve it.

    When you run the app, you'll see a clock tile in the start screen. Go to the main page of the app -> Right click everywhere -> The bottom app bar appears, click on Settings -> You should be able to change the layout of the tile in the start screen clicking on one of the "Tile Style" radio buttons. The problem is that the tile remains unchanged. Instead, the tile must change when the user clicks on the radio buttons and must remain unchanged until the user changes his selection.

    The code of the 2 layouts for the tile are in ClockTileScheduler.cs, while the code behind the 2 radio buttons is in SettingsPage.xaml.cs

    Thanks very much for your help.


    Gabriele

    Saturday, December 7, 2013 3:45 PM