How to create an asynchronous method

前端 未结 7 2106
误落风尘
误落风尘 2020-12-22 20:18

I have simple method in my C# app, it picks file from FTP server and parses it and stores the data in DB. I want it to be asynchronous, so that user perform other operations

7条回答
  •  执念已碎
    2020-12-22 20:46

    You need to use delegates and the BeginInvoke method that they contain to run another method asynchronously. A the end of the method being run by the delegate, you can notify the user. For example:

    class MyClass
    {
        private delegate void SomeFunctionDelegate(int param1, bool param2);
        private SomeFunctionDelegate sfd;
    
        public MyClass()
        {
            sfd = new SomeFunctionDelegate(this.SomeFunction);
        }
    
        private void SomeFunction(int param1, bool param2)
        {
            // Do stuff
    
            // Notify user
        }
    
        public void GetData()
        {
            // Do stuff
    
            sfd.BeginInvoke(34, true, null, null);
        }
    }
    

    Read up at http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx

提交回复
热议问题