-1

Is there any option to call method after another method ends? I want to call Method2 in Method1, but I want my program to execute Method2 after Method1 ends.

EDIT: To be more specific:

private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
    filePath = e.FullPath;
    DoSomethingWithChangedFile();
}

This doesn't work because FileSystemWatcher_Changed is using file I want to use in DoSomethingWithChangedFile(). Is there any simple solution that I don't see and I am just dumb?

Baczek
  • 17
  • 6
  • 6
    Could you give more information about what bigger problem you're trying to solve? At the moment this feels like an XY problem. – Jon Skeet Jul 16 '19 at 18:32
  • I have one method that is doing something with file and when the method ends I want different method to operate on this file. I am using FileSystemWatcher_Changed method, and after it ends doing what its doing I want it to call different method to do something with file that has changed, but I can't because FileSystemWatcher_Changed() didn't end and file is in use. – Baczek Jul 16 '19 at 18:34
  • What is stopping you from returning the file from method1 and pass it to method2? – feihoa Jul 16 '19 at 18:36
  • 3
    So what's wrong with `Method1();Method2();` ? – Alexei Levenkov Jul 16 '19 at 18:36
  • 3
    This sentence - "But I need to do this different way, without using anything else" - is hard to understand. It seems to say that you don't want to do this the obvious way, but you also don't want to 'use anything else.' Doesn't that rule out everything? – Scott Hannen Jul 16 '19 at 18:37
  • You could put in a wait operation -- but you could also just free the file handle to ensure the file is available for the funciton. – Hogan Jul 16 '19 at 18:44
  • The `Changed` event just notifies you of a change in a file. Most likely, the file that's being changed is still locked by whoever is changing it, and you'll need to wait for it to be available. Note that if you "do something" to the file, the changed event may get triggered again. – Rufus L Jul 16 '19 at 18:46
  • 1
    https://stackoverflow.com/questions/12268067/filesystemwatcher-is-file-ready-to-use is probably what you are looking for... maybe even duplicate – Alexei Levenkov Jul 16 '19 at 18:47
  • You say above that this is an issue because `FileSystemWatcher_Changed` didn't end - that's not true. The issue is whatever OTHER application has an open handle on the file still. Your application is just using the file system to monitor (not touch) associated files. – gravity Jul 16 '19 at 19:15

1 Answers1

1

Okay, I would suggest to change an architecture, but there are a few things you can use. For example, you can use events to notify when something happened and handle this event. Event-based things usually are not easy to debug and they may become a nightmare.

Here is an example:

using System;
namespace ConsoleApp17
{
    public class Summer
    {
        public delegate void EventRaiser();
        public event EventRaiser OnSomethingHappened;

        public int Sum(int a, int b)
        {
            int c = a + b;
            if (a != b) // you sophisticated check here
            {
                OnSomethingHappened();
            }
            return c;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Summer a = new Summer();
            a.OnSomethingHappened += OnSomethingHandler;

            Console.WriteLine($"result = {a.Sum(1, 2)}");
            Console.WriteLine($"result = {a.Sum(2, 2)}");
            Console.ReadKey();
        }

        private static void OnSomethingHandler()
        {
            Console.WriteLine("Something happend");
        }
    }
}

I hope it helps

Pavel Kovalev
  • 7,521
  • 5
  • 45
  • 67