खोज…


वाक्य - विन्यास

  • Async और प्रतीक्षारत

  • सार्वजनिक कार्य MyTask Async () {doSomething (); }

    MyTaskAsync () का इंतजार करें;

  • सार्वजनिक कार्य <string> MyStringTask Async () {रिटर्न getSomeString (); }

    string MyString = प्रतीक्षा करें MyStringTaskAsync ();

  • कॉल करने वाला जानकारी देता है

  • सार्वजनिक शून्य MyCallerAttributes (स्ट्रिंग MyMessage,

    [CallerMemberName] स्ट्रिंग सदस्य नाम = "",

    [CallerFilePath] स्ट्रिंग SourceFilePath = "",

    [कॉललाइनलाइन] इंट लाइननंबर = 0)

  • Trace.WriteLine ("मेरा संदेश:" + MyMessage);

    Trace.WriteLine ("सदस्य:" + सदस्यनाम);

    Trace.WriteLine ("स्रोत फ़ाइल पथ:" + SourceFilePath);

    Trace.WriteLine ("लाइन नंबर:" + लाइननंबर);

पैरामीटर

पैरामीटर के साथ विधि / संशोधक विवरण
Type<T> T रिटर्न प्रकार है

टिप्पणियों

C # 5.0 को Visual Studio .NET 2012 के साथ युग्मित किया गया है

Async और प्रतीक्षारत

async और await दो ऑपरेटर हैं जिनका उद्देश्य थ्रेड को मुक्त करके और आगे बढ़ने से पहले संचालन के लिए प्रतीक्षा करके प्रदर्शन में सुधार करना है।

यह लंबाई होने से पहले एक स्ट्रिंग प्राप्त करने का एक उदाहरण है:

//This method is async because:
//1. It has async and Task or Task<T> as modifiers
//2. It ends in "Async"
async Task<int> GetStringLengthAsync(string URL){
    HttpClient client = new HttpClient();
    //Sends a GET request and returns the response body as a string
    Task<string> getString = client.GetStringAsync(URL);
    //Waits for getString to complete before returning its length
    string contents = await getString;
    return contents.Length;
}

private async void doProcess(){
    int length = await GetStringLengthAsync("http://example.com/");
    //Waits for all the above to finish before printing the number
    Console.WriteLine(length);
}

यहां एक फ़ाइल डाउनलोड करने और प्रगति होने पर क्या होता है, इसे डाउनलोड करने का एक और उदाहरण है और जब डाउनलोड पूरा हो जाता है (ऐसा करने के दो तरीके हैं):

विधि 1:

//This one using async event handlers, but not async coupled with await
private void DownloadAndUpdateAsync(string uri, string DownloadLocation){
    WebClient web = new WebClient();
    //Assign the event handler
    web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
    web.DownloadFileCompleted += new AsyncCompletedEventHandler(FileCompleted);
    //Download the file asynchronously
    web.DownloadFileAsync(new Uri(uri), DownloadLocation);
}

//event called for when download progress has changed
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e){
    //example code
    int i = 0;
    i++;
    doSomething();
}

//event called for when download has finished
private void FileCompleted(object sender, AsyncCompletedEventArgs e){
    Console.WriteLine("Completed!")
}

विधि 2:

//however, this one does
//Refer to first example on why this method is async
private void DownloadAndUpdateAsync(string uri, string DownloadLocation){
    WebClient web = new WebClient();
    //Assign the event handler
    web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
    //Download the file async
    web.DownloadFileAsync(new Uri(uri), DownloadLocation);
    //Notice how there is no complete event, instead we're using techniques from the first example
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e){
    int i = 0;
    i++;
    doSomething();
}
private void doProcess(){
    //Wait for the download to finish
    await DownloadAndUpdateAsync(new Uri("http://example.com/file"))
    doSomething();
}

कॉल करने वाला जानकारी देता है

CIA को लक्षित पद्धति से जो भी कॉल किया जाता है, उसमें से विशेषता प्राप्त करने का एक सरल तरीका है। उनके उपयोग के लिए वास्तव में केवल 1 तरीका है और केवल 3 विशेषताएँ हैं।

उदाहरण:

//This is the "calling method": the method that is calling the target method
public void doProcess()
{
    GetMessageCallerAttributes("Show my attributes.");
}
//This is the target method
//There are only 3 caller attributes
public void GetMessageCallerAttributes(string message,
    //gets the name of what is calling this method
    [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
    //gets the path of the file in which the "calling method" is in
    [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
    //gets the line number of the "calling method"
    [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
    //Writes lines of all the attributes
    System.Diagnostics.Trace.WriteLine("Message: " + message);
    System.Diagnostics.Trace.WriteLine("Member: " + memberName);
    System.Diagnostics.Trace.WriteLine("Source File Path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("Line Number: " + sourceLineNumber);
}

उदाहरण आउटपुट:

//Message: Show my attributes.
//Member: doProcess
//Source File Path: c:\Path\To\The\File
//Line Number: 13


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow