Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 12:37

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Async & Await

Discussion on Async & Await within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Jul 2010
Posts: 283
Received Thanks: 16
Async & Await

Mahlzeit Com,

ich hätte da mal eine Frage bezüglich Async und Await.

Ich selbst "lerne" erst seit 3 Wochen C# und verstehe noch nicht genau wann ich was aufrufen muss.

Folgende Aufgabenstellung hab ich:
Ich muss ein Ordner überwachen in dem wird alle 5 sec. reingeschaut ob sich dort neue Dateien befinden. (ohne events!) Sollte dies der Fall sein möchte ich mit einer Schleife die Dateien selektieren verarbeiten und wegschieben.
Dies soll nebenbei laufen.

Mein erstes Ansatz ist:

Code:
        private bool runParser;
        private bool lookFileSystem;

        // constructor
        public DirectoryDeamon2()  {
            RunParser = false;
            lookFileSystem = true;
            LookFileSystem();
        }

        public static bool RunParser { get; set; }

        public async void LookFileSystem() {
            await Task.Run(async () => {
                var systemConfig = MainClass.systemConfig;
                while (lookFileSystem) {
                    // Time to wait in ms
                    Thread.Sleep(5000);

                    // Parser is running: continue
                    if (RunParser)
                        continue;

                    // Search mails
                    var listOfMail = Directory.GetFiles($"{ systemConfig.GetConfig("incomingMailDir") }");

                    // Found some mails
                    if (listOfMail.Count() > 0) {
                        RunParser = true;
                        // Parser
                        await FetchMails();
                    }
                }
            });
        }

       public async Task FetchMails() {
            Task.Run(() => {
                // Datei verarbeiten
                RunParser = false;
            });
        }
Wie schaff ich es nun das die Funktion "LookFileSystem" die Funktion "FetchMails" aufruft und darauf wartet das er fertig ist? Die beide Funktionen müssen auch im Hintergrund laufen damit der User weiterhin die Console verwenden kann.
Oder hat wer eine leicht verständliche Doku zu dem Thema?

Mfg Masterkroko
Masterkroko1 is offline  
Old 07/26/2016, 17:10   #2
 
elite*gold: 0
Join Date: Aug 2012
Posts: 236
Received Thanks: 94
LookFileSystem wartet schon auf FetchMails, dafür ist das await da.

Warum ist RunParser statisch, runParser aber nicht? Verwendest du letzteres überhaupt?

Wegen Thread.Sleep(5000) relevant: . Mit anderen Worten, so nicht Thread.Sleep verwenden, wenn es nicht zeitlich "verrutschen" darf.
Tasiro is offline  
Old 07/28/2016, 19:49   #3
 
elite*gold: 67
Join Date: Aug 2014
Posts: 1,323
Received Thanks: 928
ich mach mal den beginn mit dem ersten tipp:

statt Thread.Sleep machst du await Task.Delay

Edit:

Deinem code zufolge verstehst du async await noch ueberhaupt nicht bzw hast es komplett falsch gelernt.

schau dir das mal an

Xio. is offline  
Reply


Similar Threads Similar Threads
[C#]Async Socket
01/09/2014 - CO2 PServer Guides & Releases - 2 Replies
Hey , this is my first test of coding a socket :D ServerSocket - iPiraTe - Pastebin.com Notice : i didn't finished it yet .. i wanted to know ur opinion first
A look at async/await and its applications
08/08/2013 - CO2 Programming - 6 Replies
I'm pretty sure most programmers here are still stuck using the legacy async methods (Begin..., End...) instead of the new keywords, so here's a small overview of using them. You can quickly see how applicable they are to development and how useful it can be. Emphasis on "small overview". It also becomes extremely apparent how much cleaner your code becomes using async/await rather than the legacy methods which forced you to write spaghetti-like code.
Re: † • Metin 2 Kalipso • † ~ Dangerous battles await you!
05/31/2012 - Metin2 PServer Advertising - 3 Replies
Metin2Kalipso Dedicated Server 24/7 Core i7-24 gb ddr3 3.0 disk 1 gb 2 tb band. Rates: 450% Exp Yang 750%
New Adventures Await! NAGC Sea3son Coming soon!
04/26/2010 - Grand Chase - 15 Replies
http://image.ntreev.net/gc/eventImages/2010/0420/t lwmstka/01.jpg



All times are GMT +1. The time now is 12:39.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.