c# - How can i calculate percentages and report them to backgroundworker? -


now i'm reporting each file name i'm searching in , it's working fine. want report percentages progress progressbar1 have in designer. calculation should include if program enter while loop. added counterfiles variable not sure how it.

i have method:

public list<string> findlines(string dirname, string texttosearch) {     int countfiles = 0;     int counter = 0;     list<string> findlines = new list<string>();     directoryinfo di = new directoryinfo(dirname);     if (di != null && di.exists)    {         if (checkfileforaccess(dirname) == true)         {             foreach (fileinfo fi in di.enumeratefiles("*", searchoption.alldirectories))             {                 if (string.compare(fi.extension, ".cs", true) == 0)                 {                     countfiles++;                    //countfiles /                      backgroundworker1.reportprogress(0, fi.name);                     system.threading.thread.sleep(200);                     using (streamreader sr = fi.opentext())                     {                         string s = "";                         while ((s = sr.readline()) != null)                         {                             if (s.contains(texttosearch))                             {                                 counter++;                                 findlines.add(s);                             }                         }                     }                 }             }         }     }     return findlines; } 

in backgroundworker dowork , progresschanged events

private void backgroundworker1_dowork(object sender, doworkeventargs e) {         findlines(@"d:\c-sharp", "string s1 = treeview1.selectednode.tag string;"); }  private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) {     label2.text = e.userstate.tostring(); } 

you need use ajax in jquery this.

in cs file:

declare:

    public static int downloadpercent { get; set; } 

then use upload method, progress changed method, , complete methods:

private void startdownload(string downloadsource, string savelocation)     {         try         {             thread thread = new thread(() =>             {                 webclient client = new webclient();                 client.downloadprogresschanged += new downloadprogresschangedeventhandler(client_downloadprogresschanged);                 client.downloadfilecompleted += new asynccompletedeventhandler(client_downloadfilecompleted);                 client.downloadfileasync(new uri(downloadsource), savelocation);             });             thread.name = "afielddownload";             thread.start();         }         catch (exception err)         {             downloadpercent = 999;         }     }     void client_downloadprogresschanged(object sender, downloadprogresschangedeventargs e)     {         bytehasbeendownloaded = true;         try         {             double bytesin = double.parse(e.bytesreceived.tostring());             double totalbytes = double.parse(e.totalbytestoreceive.tostring());             double percentage = bytesin / totalbytes * 100;             downloadpercent = int.parse(math.truncate(percentage).tostring());         }         catch (exception err)         {             downloadpercent = 999;         }     }     void client_downloadfilecompleted(object sender, asynccompletedeventargs e)     {         if (bytehasbeendownloaded)         {             downloadfinishtime = datetime.now;             updateactionduration(false);         }     } 

then add method call using ajax update percent:

    [webmethod]     public static int getdownloadpercentage()     {         return downloadpercent;     } 

for javascript, use document loads:

$(document).ready(function () {     settimeout(updatedownprogress, 100); }); 

use javascript function updater:

function updatedownprogress() { $.ajax({     type: "post",     url: "home.aspx/getdownloadpercentage",     data: "{}",     contenttype: "application/json; charset=utf-8",     datatype: "json",     async: true,     success: function (msg) {         $("#cp_main_content_downpercentage").text(msg.d + "% downloaded");         $("#cp_main_content_progressbardown").val(msg.d);         if (msg.d < 100) {             settimeout(updatedownprogress, 100);             if (msg.d > 0) { $(".startdownloadbutton").prop("disabled", true); }             else { $(".startdownloadbutton").prop("disabled", false); }         }         else if (msg.d > 100) {             //use error codes         }     } }); } 

and finally... use html progress bar:

<asp:panel id="pdownloading" cssclass="loading" runat="server">             <progress id="progressbardown" runat="server" value="0" max="100"></progress> <asp:label id="downpercentage" runat="server"></asp:label>         </asp:panel> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -