Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

c# - Get asynchronous loop completion passed

Please tell me how to get asynchronous loop completion passed

Description of the logic you want to implement.

  1. Search for removable storage devices and read specific extensions. [Implemented]
  2. Invoke webapi to upload the extension retrieved from the mobile device. [Implemented]
  3. If the uploaded result is successful, a specific path is retrieved. [Refer to the source]
  4. Return a specific path as a property. -> Here, the loop starts as many as the number of removable storage media. I want to be informed that the loop is over, storing a specific path in an array while looping. What should I do?

button1_Click_1 { // present in class of removable disk //Search for a specific extension. XmlFileEachDry = ClsDriveMethod.FilesSearch(); //Start loop as many as the number of removable disks. for (int index = 0; index <XmlFileEachDry.Count; ++index) { ... //Go around the loop // Call web api.

var result = UploadFileAsync( "", XmlFileEachDry[i], "xml" ).Result; } // If the result of calling web API is successful //It takes a specific path and saves it. // uploadFilePath.Add(result.RsltFileUploadLoc); //->Here I want to receive an event that the loop is over.

public async Task UploadFileAsync(string json, string path, string channels)

        {
            try
            {
                HttpClient client = new HttpClient();

             
                var multiForm = new MultipartFormDataContent();

                UploadFileResponse UploadResponse = new UploadFileResponse();


               var stringContent = new StringContent(JsonConvert.SerializeObject(UploadInfo));
                stringContent.Headers.Add("Content-Disposition", "form-data; name="json"");
                multiForm.Add(stringContent, "json");

                // add file and directly upload it

                if (File.Exists(path))
                {
                    FileStream fs = File.OpenRead(path);
                    multiForm.Add(new StreamContent(fs), "xml", Path.GetFileName(path));
                   
                }

                else
                {
                    UploadResponse.rsltDesc = "File Not Found";
                  
                    return UploadResponse;

                }
                // send request to API
                var url = GlobalVariable.UrlAddress + GlobalVariable.UploadAddress;
                var response = await client.PostAsync(url, multiForm);
            


                // fetch response from API
                var responseJson = await response.Content.ReadAsStringAsync();
              

                // convert JSON response to object
                UploadResponse = JsonConvert.DeserializeObject<UploadFileResponse>(responseJson);

                SqlliteDb.InsertServerTransmissionResponse(UploadResponse);
             
                if (UploadResponse.rsltDesc == "success")
                {
                    SuccessfulTransfer++;


                }
                else
                {
                    FailedTransfer++;
                }

            }

            catch (Exception exception)
            {
              
            }

            finally
            { 
              
            }
           
             return UploadResponse;
            
        }
question from:https://stackoverflow.com/questions/65640704/get-asynchronous-loop-completion-passed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...