Click here to Skip to main content
15,886,026 members
Home / Discussions / C#
   

C#

 
QuestionSimple C# question - Begginer's lesson, please aid. Pin
Member 149309634-Sep-20 13:32
Member 149309634-Sep-20 13:32 
AnswerRe: Simple C# question - Begginer's lesson, please aid. Pin
Mycroft Holmes4-Sep-20 15:19
professionalMycroft Holmes4-Sep-20 15:19 
GeneralRe: Simple C# question - Begginer's lesson, please aid. Pin
Member 149309634-Sep-20 15:21
Member 149309634-Sep-20 15:21 
GeneralRe: Simple C# question - Begginer's lesson, please aid. Pin
Mycroft Holmes4-Sep-20 15:57
professionalMycroft Holmes4-Sep-20 15:57 
GeneralRe: Simple C# question - Begginer's lesson, please aid. Pin
Member 149309634-Sep-20 16:56
Member 149309634-Sep-20 16:56 
AnswerRe: Simple C# question - Begginer's lesson, please aid. Pin
OriginalGriff4-Sep-20 20:07
mveOriginalGriff4-Sep-20 20:07 
AnswerRe: Simple C# question - Begginer's lesson, please aid. Pin
Eddy Vluggen5-Sep-20 5:27
professionalEddy Vluggen5-Sep-20 5:27 
QuestionRunning Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor4-Sep-20 12:41
Exoskeletor4-Sep-20 12:41 
I have this function

private async Task<int> SearchForAllNumbers(int num = -1)
       {
           if (num == -1)
               DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle), GetString(Resource.String.Common_SearchMessage));
           dates = DateFunctions.GetSelectedDates(DateFrom, DateTo);

           var list = string.Empty;
           var klirwseis = ", Κληρωσεις: ";
           if (num == -1)
               list = ListTextView.Text;
           else
               list = num.ToString();
           var nums = list.Split(',').Select(Int32.Parse).ToList();
           if (seperateCheckBox.Checked)
           {
               DisplayLoadingMessage(false);
               seperateCheckBox.Checked = false;
               for (int k = 0; k < nums.Count; k++)
               {
                   await SearchForAllNumbers(nums[k]);
               }
               return -1;
           }
           var totalCoutner = 0;
           for (int datesPos = 0; datesPos <= dates.Count; datesPos++)
           {
              ... Do some calculations
               }
               HistoryTextView.Text = "Ημερομηνία: " + dates[datesPos] + ", Λίστα: " + list + ", Κληρώθηκε: " + totalCoutner + " φορές" + HistoryTextView.Text;
               if (showListsCheckBox.Checked)
                   HistoryTextView.Text = klirwseis + HistoryTextView.Text;
           }
           DisplayLoadingMessage(false);
           HistoryTextView.Text = ".\n" + HistoryTextView.Text;
           return totalCoutner;
           return 0;
       }


And i want to be able to cancel it when its running so i have created this function

private async Task SearchFoNumbersAsync()
        {
            ActiveCancellationTokenSource = new CancellationTokenSource();
            DrawResultsTypeEnum searchType = (DrawResultsTypeEnum)ShowResultsSpinnerPosition;

            var task = Task.Run(async () =>
            {
                if (searchType == DrawResultsTypeEnum.AllNumbers)
                    await SearchForAllNumbers();
                else if (searchType == DrawResultsTypeEnum.AnyWinningCombination)
                    await SearchAnyWinningCombination();
                SaveHistory();
            }, ActiveCancellationTokenSource.Token); // Pass same token to Task.Run.

            try
            {
                await task;
            }
            catch (System.OperationCanceledException e)
            {
                Console.WriteLine($"{nameof(System.OperationCanceledException)} thrown with message: {e.Message}");
                DisplayLoadingMessage(false);
            }
            finally
            {
                ActiveCancellationTokenSource.Dispose();
                DisplayLoadingMessage(false);
            }
        }


To be able to run it from Task.Run and to be able to cancel it. The code doesnt work however
I have debug it and when it reach the
try
            {
                await task;
            }

Stays there forever, the
SearchForAllNumbers()
or
SearchAnyWinningCombination()
are not accessed at all.
i'm not sure what i have done wrong.
AnswerRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Gerry Schmitz5-Sep-20 4:31
mveGerry Schmitz5-Sep-20 4:31 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor5-Sep-20 8:25
Exoskeletor5-Sep-20 8:25 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Gerry Schmitz5-Sep-20 9:05
mveGerry Schmitz5-Sep-20 9:05 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor5-Sep-20 10:17
Exoskeletor5-Sep-20 10:17 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Gerry Schmitz5-Sep-20 10:38
mveGerry Schmitz5-Sep-20 10:38 
GeneralRe: Running Function from Task.Run with Cancellation Token gives infinite loop Pin
Exoskeletor5-Sep-20 10:58
Exoskeletor5-Sep-20 10:58 
Question.Net Core Windows App, using database repositories Pin
jkirkerx4-Sep-20 11:53
professionaljkirkerx4-Sep-20 11:53 
AnswerRe: .Net Core Windows App, using database repositories Pin
Mycroft Holmes4-Sep-20 12:07
professionalMycroft Holmes4-Sep-20 12:07 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx4-Sep-20 13:45
professionaljkirkerx4-Sep-20 13:45 
GeneralRe: .Net Core Windows App, using database repositories Pin
Mycroft Holmes4-Sep-20 15:14
professionalMycroft Holmes4-Sep-20 15:14 
GeneralRe: .Net Core Windows App, using database repositories Pin
Gerry Schmitz5-Sep-20 4:40
mveGerry Schmitz5-Sep-20 4:40 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx5-Sep-20 7:32
professionaljkirkerx5-Sep-20 7:32 
GeneralRe: .Net Core Windows App, using database repositories Pin
Gerry Schmitz5-Sep-20 8:46
mveGerry Schmitz5-Sep-20 8:46 
AnswerRe: .Net Core Windows App, using database repositories Pin
Richard Deeming6-Sep-20 22:30
mveRichard Deeming6-Sep-20 22:30 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx7-Sep-20 6:58
professionaljkirkerx7-Sep-20 6:58 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx7-Sep-20 9:14
professionaljkirkerx7-Sep-20 9:14 
GeneralRe: .Net Core Windows App, using database repositories Pin
jkirkerx7-Sep-20 10:29
professionaljkirkerx7-Sep-20 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.