Click here to Skip to main content
15,121,641 members
Home / Discussions / C#
   

C#

 
GeneralRe: Creating An Async Method Pin
Kevin Marois9-Oct-21 8:03
professionalKevin Marois9-Oct-21 8:03 
GeneralRe: Creating An Async Method Pin
lmoelleb10-Oct-21 9:56
Memberlmoelleb10-Oct-21 9:56 
Question(xaml, wpf) ScrollViewer Binding confusion (beginner) (ANSWERED) Pin
Maximilien6-Oct-21 10:50
MemberMaximilien6-Oct-21 10:50 
SuggestionRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Richard MacCutchan6-Oct-21 22:30
mveRichard MacCutchan6-Oct-21 22:30 
GeneralRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Maximilien7-Oct-21 4:49
MemberMaximilien7-Oct-21 4:49 
AnswerRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Richard Deeming6-Oct-21 22:45
mveRichard Deeming6-Oct-21 22:45 
GeneralRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Maximilien8-Oct-21 4:03
MemberMaximilien8-Oct-21 4:03 
QuestionCan't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor2-Oct-21 0:03
MemberExoskeletor2-Oct-21 0:03 
Hello fellow members. Im trying to understand why my code is not catching NullReferenceException and i cant figure it out.

I'm using this code to run async function

private async Task SearchMostLessViewsAsync(DateTime dateFrom, DateTime dateTo, int amountOfNumbersSelected, int frequencyOption, int fromDrawNumber, int toDrawNumber)
        {
            Microsoft.AppCenter.Analytics.Analytics.TrackEvent($"{typeof(FragmentDrawsNumbersFrequency).Name}.{nameof(SearchMostLessViewsAsync)}",
                new Dictionary<string, string>()
                    {
                        {nameof(dateFrom), dateFrom.ToString()},
                        {nameof(dateTo), dateTo.ToString()},
                        {nameof(amountOfNumbersSelected), amountOfNumbersSelected.ToString()},
                        {nameof(frequencyOption), frequencyOption.ToString()},
                        {nameof(fromDrawNumber), fromDrawNumber.ToString()},
                        {nameof(toDrawNumber), toDrawNumber.ToString()},
                    }
                );
            ((MainActivity)Activity).DisplayLoadingMessage(true, GetString(Resource.String.Common_SearchTitle),
                GetString(Resource.String.Common_SearchMessage));

            //ApplicationState.ChangeCancellationTokenSource(new CancellationTokenSource());

            var task = Task.Run(async () =>
            {
                var textResult = await SearchLeast(dateFrom, dateTo, amountOfNumbersSelected, frequencyOption, fromDrawNumber, toDrawNumber).ConfigureAwait(false);

                if (!string.IsNullOrEmpty(textResult))
                {
                    UpdateHistoryList(textResult);
                    DatabaseFunctions.SaveHistoryList(HistoryList, Settings.DrawsNumbersFrequencyHistoryListViewKey);

                    ((MainActivity)Activity).ShowSearchResults(textResult);
                }
            }, ApplicationState.GetCancellationToken()); // Pass same token to Task.Run.

            try
            {
                await task.ConfigureAwait(false);
            }
            catch (TaskCanceledException tce)
            {
                Console.WriteLine($"{nameof(TaskCanceledException)} thrown with message: {tce.Message}");
            }
            catch (System.ObjectDisposedException ode)
            {
                Console.WriteLine($"{nameof(System.ObjectDisposedException)} thrown with message: {ode.Message}");
            }
            catch (System.OperationCanceledException e)
            {
                Console.WriteLine($"{nameof(System.OperationCanceledException)} thrown with message: {e.Message}");
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine($"{nameof(NullReferenceException)} thrown with message: {nre.Message}");
            }
            finally
            {
                ApplicationState.DisposeCancellationTokenSource();
                ((MainActivity)Activity).DisplayLoadingMessage(false);
            }

            ((MainActivity)Activity).DisplayLoadingMessage(false);
        }

But sometimes i get this exception

FragmentDrawsNumbersFrequency.SearchMostLessViewsAsync (System.DateTime dateFrom, System.DateTime dateTo, System.Int32 amountOfNumbersSelected, System.Int32 frequencyOption, System.Int32 fromDrawNumber, System.Int32 toDrawNumber)
FragmentDrawsNumbersFrequency.ShowMostLessViews ()
FragmentDrawsNumbersFrequency.<OnCreateView>b__50_8 (System.Object sender, System.EventArgs e)
AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state)
SyncContext+<>c__DisplayClass2_0.<Post>b__0 ()
Thread+RunnableImplementor.Run ()
IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.61(intptr,intptr)

I'm not sure why. Shouldn't the try catch wrap catch nullreference exceptions? Something tells me i miss something else here

modified 2-Oct-21 8:56am.

AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon2-Oct-21 0:38
mvePete O'Hanlon2-Oct-21 0:38 
AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 1:33
mveOriginalGriff2-Oct-21 1:33 
JokeRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon2-Oct-21 2:32
mvePete O'Hanlon2-Oct-21 2:32 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 3:14
mveOriginalGriff2-Oct-21 3:14 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor2-Oct-21 4:01
MemberExoskeletor2-Oct-21 4:01 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon2-Oct-21 4:51
mvePete O'Hanlon2-Oct-21 4:51 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 4:55
mveOriginalGriff2-Oct-21 4:55 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor2-Oct-21 4:58
MemberExoskeletor2-Oct-21 4:58 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor2-Oct-21 3:58
MemberExoskeletor2-Oct-21 3:58 
AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon3-Oct-21 21:22
mvePete O'Hanlon3-Oct-21 21:22 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor4-Oct-21 1:21
MemberExoskeletor4-Oct-21 1:21 
Questionhow read file from Open with in C# Pin
Member 1522978130-Sep-21 7:24
MemberMember 1522978130-Sep-21 7:24 
AnswerRe: how read file from Open with in C# Pin
Dave Kreskowiak30-Sep-21 9:08
mveDave Kreskowiak30-Sep-21 9:08 
AnswerRe: how read file from Open with in C# Pin
Luc Pattyn30-Sep-21 9:30
sitebuilderLuc Pattyn30-Sep-21 9:30 
QuestionQuestion About Async Pin
Kevin Marois29-Sep-21 11:21
professionalKevin Marois29-Sep-21 11:21 
AnswerRe: Question About Async Pin
Pete O'Hanlon29-Sep-21 21:22
mvePete O'Hanlon29-Sep-21 21:22 
GeneralRe: Question About Async Pin
Kevin Marois30-Sep-21 5:24
professionalKevin Marois30-Sep-21 5:24 

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.