Check if button is clicked from scratched made buttonTrigger a button click with JavaScript on the Enter key in a text box<button> vs. <input type=“button” />. Which to use?How to prevent buttons from submitting formsGet int value from enum in C#Type Checking: typeof, GetType, or is?jQuery disable/enable submit buttonHow to create an HTML button that acts like a link?How to check a radio button with jQuery?How to handle button clicks using the XML onClick within Fragments

Is my router's IP address really public?

Modern approach to radio buttons

Custom allocators as alternatives to vector of smart pointers?

Can a non-EU citizen travel within schengen zone freely without passport?

How to capture more stars?

What is the difference between nullifying your vote and not going to vote at all?

Can a wire having a 610-670 THz (frequency of blue light) AC frequency supply, generate blue light?

Best strategy for UK visa for a school trip (travelling alone or accomanpied child)?

The Passive Wisdom (Perception) score of my character on D&D Beyond seems too high

Is it possible to change original filename of an exe?

Circumcircle bisects the segment connecting the vertices of two regular even-sided polygons

What does it mean when you think without speaking?

How do I subvert the tropes of a train heist?

What is the 中 in ダウンロード中?

Looking after a wayward brother in mother's will

Can I use the Shadow Step feature to effectively teleport into a Darkness spell I cast upon myself?

What are the problems in teaching guitar via Skype?

Ticket sales for Queen at the Live Aid

A Mathematical Discussion: Fill in the Blank

Is this story about US tax office reasonable?

French translation of “only ever”

If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?

How many chess players are over 2500 Elo?

What environment would goblins be best adapted for?



Check if button is clicked from scratched made button


Trigger a button click with JavaScript on the Enter key in a text box<button> vs. <input type=“button” />. Which to use?How to prevent buttons from submitting formsGet int value from enum in C#Type Checking: typeof, GetType, or is?jQuery disable/enable submit buttonHow to create an HTML button that acts like a link?How to check a radio button with jQuery?How to handle button clicks using the XML onClick within Fragments






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have console application which call form like this i put it other class just to make my code organized.



I made form code from scratch like this:



using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Ultra_Script.MessageBoxes

public class MsgBoxes

#region MsgBox k výběru základního SW (stažení z netu nebo Synology)
public static void SynoInternet()

Application.EnableVisualStyles();
Form SynoInternet = new Form();

Button Synology = new Button()

Left = 80,
Width = 90,
Height = 30,
Top = 75,
Text = "Synology"
;

Button Internet = new Button()

Left = 190,
Width = 90,
Height = 30,
Top = 75,
Text = "Internet"
;

Label SynoInternetLabel = new Label()

Left = 60,
Width = 350,
Height = 25,
Top = 30,
Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
;

Label fasterLabel = new Label()

Left = 80,
Width = 60,
Height = 20,
Top = 110,
Text = "(Rychlejší)"
;

SynoInternet.Width = 380;
SynoInternet.Height = 170;
SynoInternet.Controls.Add(fasterLabel);
SynoInternet.Controls.Add(SynoInternetLabel);
SynoInternet.Controls.Add(Synology);
SynoInternet.Controls.Add(Internet);
SynoInternet.ShowDialog();


#endregion






But have no idea how to check (in this class) if button was clicked.



I know i somehow need to create EventHandler but had no succes.



i tried it something like this:



 Synology.Click += (sender, args) =>

MessageBox.Show("You clicked Synology");
;


Im not getting any error with this, but when i click Synology nothing happens.



Any ideas?



Thanks for all answers,



John










share|improve this question




























    0















    I have console application which call form like this i put it other class just to make my code organized.



    I made form code from scratch like this:



    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    namespace Ultra_Script.MessageBoxes

    public class MsgBoxes

    #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
    public static void SynoInternet()

    Application.EnableVisualStyles();
    Form SynoInternet = new Form();

    Button Synology = new Button()

    Left = 80,
    Width = 90,
    Height = 30,
    Top = 75,
    Text = "Synology"
    ;

    Button Internet = new Button()

    Left = 190,
    Width = 90,
    Height = 30,
    Top = 75,
    Text = "Internet"
    ;

    Label SynoInternetLabel = new Label()

    Left = 60,
    Width = 350,
    Height = 25,
    Top = 30,
    Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
    ;

    Label fasterLabel = new Label()

    Left = 80,
    Width = 60,
    Height = 20,
    Top = 110,
    Text = "(Rychlejší)"
    ;

    SynoInternet.Width = 380;
    SynoInternet.Height = 170;
    SynoInternet.Controls.Add(fasterLabel);
    SynoInternet.Controls.Add(SynoInternetLabel);
    SynoInternet.Controls.Add(Synology);
    SynoInternet.Controls.Add(Internet);
    SynoInternet.ShowDialog();


    #endregion






    But have no idea how to check (in this class) if button was clicked.



    I know i somehow need to create EventHandler but had no succes.



    i tried it something like this:



     Synology.Click += (sender, args) =>

    MessageBox.Show("You clicked Synology");
    ;


    Im not getting any error with this, but when i click Synology nothing happens.



    Any ideas?



    Thanks for all answers,



    John










    share|improve this question
























      0












      0








      0








      I have console application which call form like this i put it other class just to make my code organized.



      I made form code from scratch like this:



      using System;
      using System.ComponentModel;
      using System.Drawing;
      using System.Windows.Forms;

      namespace Ultra_Script.MessageBoxes

      public class MsgBoxes

      #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
      public static void SynoInternet()

      Application.EnableVisualStyles();
      Form SynoInternet = new Form();

      Button Synology = new Button()

      Left = 80,
      Width = 90,
      Height = 30,
      Top = 75,
      Text = "Synology"
      ;

      Button Internet = new Button()

      Left = 190,
      Width = 90,
      Height = 30,
      Top = 75,
      Text = "Internet"
      ;

      Label SynoInternetLabel = new Label()

      Left = 60,
      Width = 350,
      Height = 25,
      Top = 30,
      Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
      ;

      Label fasterLabel = new Label()

      Left = 80,
      Width = 60,
      Height = 20,
      Top = 110,
      Text = "(Rychlejší)"
      ;

      SynoInternet.Width = 380;
      SynoInternet.Height = 170;
      SynoInternet.Controls.Add(fasterLabel);
      SynoInternet.Controls.Add(SynoInternetLabel);
      SynoInternet.Controls.Add(Synology);
      SynoInternet.Controls.Add(Internet);
      SynoInternet.ShowDialog();


      #endregion






      But have no idea how to check (in this class) if button was clicked.



      I know i somehow need to create EventHandler but had no succes.



      i tried it something like this:



       Synology.Click += (sender, args) =>

      MessageBox.Show("You clicked Synology");
      ;


      Im not getting any error with this, but when i click Synology nothing happens.



      Any ideas?



      Thanks for all answers,



      John










      share|improve this question














      I have console application which call form like this i put it other class just to make my code organized.



      I made form code from scratch like this:



      using System;
      using System.ComponentModel;
      using System.Drawing;
      using System.Windows.Forms;

      namespace Ultra_Script.MessageBoxes

      public class MsgBoxes

      #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
      public static void SynoInternet()

      Application.EnableVisualStyles();
      Form SynoInternet = new Form();

      Button Synology = new Button()

      Left = 80,
      Width = 90,
      Height = 30,
      Top = 75,
      Text = "Synology"
      ;

      Button Internet = new Button()

      Left = 190,
      Width = 90,
      Height = 30,
      Top = 75,
      Text = "Internet"
      ;

      Label SynoInternetLabel = new Label()

      Left = 60,
      Width = 350,
      Height = 25,
      Top = 30,
      Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
      ;

      Label fasterLabel = new Label()

      Left = 80,
      Width = 60,
      Height = 20,
      Top = 110,
      Text = "(Rychlejší)"
      ;

      SynoInternet.Width = 380;
      SynoInternet.Height = 170;
      SynoInternet.Controls.Add(fasterLabel);
      SynoInternet.Controls.Add(SynoInternetLabel);
      SynoInternet.Controls.Add(Synology);
      SynoInternet.Controls.Add(Internet);
      SynoInternet.ShowDialog();


      #endregion






      But have no idea how to check (in this class) if button was clicked.



      I know i somehow need to create EventHandler but had no succes.



      i tried it something like this:



       Synology.Click += (sender, args) =>

      MessageBox.Show("You clicked Synology");
      ;


      Im not getting any error with this, but when i click Synology nothing happens.



      Any ideas?



      Thanks for all answers,



      John







      c# forms button






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 8:27









      Johnyn CorbieJohnyn Corbie

      54




      54






















          2 Answers
          2






          active

          oldest

          votes


















          0














          you need only to make this event



           Synology.Click += (sender, args) =>

          MessageBox.Show("You clicked Synology");
          ;


          before showDialog() method;



          the lines after showDialog event are not running until the showDialog Method returns a value



          Also I think you need to redesign the class so you'll be able to dispose it correctly.



          I hope this works for you.






          share|improve this answer

























          • Thanks stranger. Saved me. Its working.

            – Johnyn Corbie
            Mar 24 at 8:48


















          0














          So i have another problem.



          I have class like this now:



          using System;
          using System.ComponentModel;
          using System.Drawing;
          using System.Windows.Forms;

          namespace Ultra_Script.MessageBoxes

          public class MsgBoxes

          #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
          public static void SynoInternet()

          Application.EnableVisualStyles();
          Form SynoInternet = new Form();

          Button Synology = new Button()

          Left = 80,
          Width = 90,
          Height = 30,
          Top = 75,
          Text = "Synology"
          ;

          Button Internet = new Button()

          Left = 190,
          Width = 90,
          Height = 30,
          Top = 75,
          Text = "Internet"
          ;

          Label SynoInternetLabel = new Label()

          Left = 60,
          Width = 350,
          Height = 25,
          Top = 30,
          Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
          ;

          Label fasterLabel = new Label()

          Left = 80,
          Width = 60,
          Height = 20,
          Top = 110,
          Text = "(Rychlejší)"
          ;

          SynoInternet.Width = 380;
          SynoInternet.Height = 170;
          SynoInternet.Controls.Add(fasterLabel);
          SynoInternet.Controls.Add(SynoInternetLabel);
          SynoInternet.Controls.Add(Synology);
          SynoInternet.Controls.Add(Internet);

          Synology.Click += (sender, args) =>

          SynoInternet.Dispose();
          Installation_Functions.InstallBasicSW();
          ;

          Internet.Click += (sender, args) =>

          MessageBox.Show("You clicked Internet");
          ;

          SynoInternet.ShowDialog();


          #endregion






          InstallFunctions class just call this filedownloader:



          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading;
          using System.Threading.Tasks;
          using System.ComponentModel;
          using System.IO;
          using System.Net;
          using System.Threading;

          namespace Ultra_Script

          class FileDownloader


          private readonly string _url;
          private readonly string _fullPathWheretoSave;
          private bool _result = false;
          private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

          public FileDownloader(string url, string fullPathWheretoSave)


          if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
          if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");

          this._url = url;
          this._fullPathWheretoSave = fullPathWheretoSave;



          public bool StartDownload(int timeout)


          try

          System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));

          if (File.Exists(_fullPathWheretoSave))

          File.Delete(_fullPathWheretoSave);

          using (WebClient client = new WebClient())

          var ur = new Uri(_url);
          //client.Credentials = new NetworkCredential("username", "password");
          client.DownloadProgressChanged += WebClientDownloadProgressChanged;
          client.DownloadFileCompleted += WebClientDownloadCompleted;
          Console.WriteLine(@"Stahuji potrebne soubory:");
          client.DownloadFileAsync(ur, _fullPathWheretoSave);
          _semaphore.Wait(timeout);
          return _result && File.Exists(_fullPathWheretoSave);



          catch (Exception e)

          Console.WriteLine("Cant download file");
          Console.Write(e);
          return false;

          finally

          this._semaphore.Dispose();




          private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

          Console.Write($"/r --> e.ProgressPercentage%");


          private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)

          _result = !args.Cancelled;
          if (!_result)

          Console.Write(args.Error.ToString());

          Console.WriteLine(Environment.NewLine + "Download Finished!");
          _semaphore.Release();


          public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)

          return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);









          What is doing its closing the form and then run back function in console. But it only says: "Start downloading" and after timeout it goes: Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll in visual studio



          When i run step by step debugging i think it stopped on that Semaphore method, but its not working even if i delete this method in filedownloader.



          When i call function from console its working normally problem is only when i call it through that button in messagebox.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321924%2fcheck-if-button-is-clicked-from-scratched-made-button%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            you need only to make this event



             Synology.Click += (sender, args) =>

            MessageBox.Show("You clicked Synology");
            ;


            before showDialog() method;



            the lines after showDialog event are not running until the showDialog Method returns a value



            Also I think you need to redesign the class so you'll be able to dispose it correctly.



            I hope this works for you.






            share|improve this answer

























            • Thanks stranger. Saved me. Its working.

              – Johnyn Corbie
              Mar 24 at 8:48















            0














            you need only to make this event



             Synology.Click += (sender, args) =>

            MessageBox.Show("You clicked Synology");
            ;


            before showDialog() method;



            the lines after showDialog event are not running until the showDialog Method returns a value



            Also I think you need to redesign the class so you'll be able to dispose it correctly.



            I hope this works for you.






            share|improve this answer

























            • Thanks stranger. Saved me. Its working.

              – Johnyn Corbie
              Mar 24 at 8:48













            0












            0








            0







            you need only to make this event



             Synology.Click += (sender, args) =>

            MessageBox.Show("You clicked Synology");
            ;


            before showDialog() method;



            the lines after showDialog event are not running until the showDialog Method returns a value



            Also I think you need to redesign the class so you'll be able to dispose it correctly.



            I hope this works for you.






            share|improve this answer















            you need only to make this event



             Synology.Click += (sender, args) =>

            MessageBox.Show("You clicked Synology");
            ;


            before showDialog() method;



            the lines after showDialog event are not running until the showDialog Method returns a value



            Also I think you need to redesign the class so you'll be able to dispose it correctly.



            I hope this works for you.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 24 at 8:44

























            answered Mar 24 at 8:38









            Shehab MekhlafyShehab Mekhlafy

            135




            135












            • Thanks stranger. Saved me. Its working.

              – Johnyn Corbie
              Mar 24 at 8:48

















            • Thanks stranger. Saved me. Its working.

              – Johnyn Corbie
              Mar 24 at 8:48
















            Thanks stranger. Saved me. Its working.

            – Johnyn Corbie
            Mar 24 at 8:48





            Thanks stranger. Saved me. Its working.

            – Johnyn Corbie
            Mar 24 at 8:48













            0














            So i have another problem.



            I have class like this now:



            using System;
            using System.ComponentModel;
            using System.Drawing;
            using System.Windows.Forms;

            namespace Ultra_Script.MessageBoxes

            public class MsgBoxes

            #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
            public static void SynoInternet()

            Application.EnableVisualStyles();
            Form SynoInternet = new Form();

            Button Synology = new Button()

            Left = 80,
            Width = 90,
            Height = 30,
            Top = 75,
            Text = "Synology"
            ;

            Button Internet = new Button()

            Left = 190,
            Width = 90,
            Height = 30,
            Top = 75,
            Text = "Internet"
            ;

            Label SynoInternetLabel = new Label()

            Left = 60,
            Width = 350,
            Height = 25,
            Top = 30,
            Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
            ;

            Label fasterLabel = new Label()

            Left = 80,
            Width = 60,
            Height = 20,
            Top = 110,
            Text = "(Rychlejší)"
            ;

            SynoInternet.Width = 380;
            SynoInternet.Height = 170;
            SynoInternet.Controls.Add(fasterLabel);
            SynoInternet.Controls.Add(SynoInternetLabel);
            SynoInternet.Controls.Add(Synology);
            SynoInternet.Controls.Add(Internet);

            Synology.Click += (sender, args) =>

            SynoInternet.Dispose();
            Installation_Functions.InstallBasicSW();
            ;

            Internet.Click += (sender, args) =>

            MessageBox.Show("You clicked Internet");
            ;

            SynoInternet.ShowDialog();


            #endregion






            InstallFunctions class just call this filedownloader:



            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Threading;
            using System.Threading.Tasks;
            using System.ComponentModel;
            using System.IO;
            using System.Net;
            using System.Threading;

            namespace Ultra_Script

            class FileDownloader


            private readonly string _url;
            private readonly string _fullPathWheretoSave;
            private bool _result = false;
            private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

            public FileDownloader(string url, string fullPathWheretoSave)


            if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
            if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");

            this._url = url;
            this._fullPathWheretoSave = fullPathWheretoSave;



            public bool StartDownload(int timeout)


            try

            System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));

            if (File.Exists(_fullPathWheretoSave))

            File.Delete(_fullPathWheretoSave);

            using (WebClient client = new WebClient())

            var ur = new Uri(_url);
            //client.Credentials = new NetworkCredential("username", "password");
            client.DownloadProgressChanged += WebClientDownloadProgressChanged;
            client.DownloadFileCompleted += WebClientDownloadCompleted;
            Console.WriteLine(@"Stahuji potrebne soubory:");
            client.DownloadFileAsync(ur, _fullPathWheretoSave);
            _semaphore.Wait(timeout);
            return _result && File.Exists(_fullPathWheretoSave);



            catch (Exception e)

            Console.WriteLine("Cant download file");
            Console.Write(e);
            return false;

            finally

            this._semaphore.Dispose();




            private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

            Console.Write($"/r --> e.ProgressPercentage%");


            private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)

            _result = !args.Cancelled;
            if (!_result)

            Console.Write(args.Error.ToString());

            Console.WriteLine(Environment.NewLine + "Download Finished!");
            _semaphore.Release();


            public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)

            return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);









            What is doing its closing the form and then run back function in console. But it only says: "Start downloading" and after timeout it goes: Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll in visual studio



            When i run step by step debugging i think it stopped on that Semaphore method, but its not working even if i delete this method in filedownloader.



            When i call function from console its working normally problem is only when i call it through that button in messagebox.






            share|improve this answer



























              0














              So i have another problem.



              I have class like this now:



              using System;
              using System.ComponentModel;
              using System.Drawing;
              using System.Windows.Forms;

              namespace Ultra_Script.MessageBoxes

              public class MsgBoxes

              #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
              public static void SynoInternet()

              Application.EnableVisualStyles();
              Form SynoInternet = new Form();

              Button Synology = new Button()

              Left = 80,
              Width = 90,
              Height = 30,
              Top = 75,
              Text = "Synology"
              ;

              Button Internet = new Button()

              Left = 190,
              Width = 90,
              Height = 30,
              Top = 75,
              Text = "Internet"
              ;

              Label SynoInternetLabel = new Label()

              Left = 60,
              Width = 350,
              Height = 25,
              Top = 30,
              Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
              ;

              Label fasterLabel = new Label()

              Left = 80,
              Width = 60,
              Height = 20,
              Top = 110,
              Text = "(Rychlejší)"
              ;

              SynoInternet.Width = 380;
              SynoInternet.Height = 170;
              SynoInternet.Controls.Add(fasterLabel);
              SynoInternet.Controls.Add(SynoInternetLabel);
              SynoInternet.Controls.Add(Synology);
              SynoInternet.Controls.Add(Internet);

              Synology.Click += (sender, args) =>

              SynoInternet.Dispose();
              Installation_Functions.InstallBasicSW();
              ;

              Internet.Click += (sender, args) =>

              MessageBox.Show("You clicked Internet");
              ;

              SynoInternet.ShowDialog();


              #endregion






              InstallFunctions class just call this filedownloader:



              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Threading;
              using System.Threading.Tasks;
              using System.ComponentModel;
              using System.IO;
              using System.Net;
              using System.Threading;

              namespace Ultra_Script

              class FileDownloader


              private readonly string _url;
              private readonly string _fullPathWheretoSave;
              private bool _result = false;
              private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

              public FileDownloader(string url, string fullPathWheretoSave)


              if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
              if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");

              this._url = url;
              this._fullPathWheretoSave = fullPathWheretoSave;



              public bool StartDownload(int timeout)


              try

              System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));

              if (File.Exists(_fullPathWheretoSave))

              File.Delete(_fullPathWheretoSave);

              using (WebClient client = new WebClient())

              var ur = new Uri(_url);
              //client.Credentials = new NetworkCredential("username", "password");
              client.DownloadProgressChanged += WebClientDownloadProgressChanged;
              client.DownloadFileCompleted += WebClientDownloadCompleted;
              Console.WriteLine(@"Stahuji potrebne soubory:");
              client.DownloadFileAsync(ur, _fullPathWheretoSave);
              _semaphore.Wait(timeout);
              return _result && File.Exists(_fullPathWheretoSave);



              catch (Exception e)

              Console.WriteLine("Cant download file");
              Console.Write(e);
              return false;

              finally

              this._semaphore.Dispose();




              private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

              Console.Write($"/r --> e.ProgressPercentage%");


              private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)

              _result = !args.Cancelled;
              if (!_result)

              Console.Write(args.Error.ToString());

              Console.WriteLine(Environment.NewLine + "Download Finished!");
              _semaphore.Release();


              public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)

              return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);









              What is doing its closing the form and then run back function in console. But it only says: "Start downloading" and after timeout it goes: Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll in visual studio



              When i run step by step debugging i think it stopped on that Semaphore method, but its not working even if i delete this method in filedownloader.



              When i call function from console its working normally problem is only when i call it through that button in messagebox.






              share|improve this answer

























                0












                0








                0







                So i have another problem.



                I have class like this now:



                using System;
                using System.ComponentModel;
                using System.Drawing;
                using System.Windows.Forms;

                namespace Ultra_Script.MessageBoxes

                public class MsgBoxes

                #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
                public static void SynoInternet()

                Application.EnableVisualStyles();
                Form SynoInternet = new Form();

                Button Synology = new Button()

                Left = 80,
                Width = 90,
                Height = 30,
                Top = 75,
                Text = "Synology"
                ;

                Button Internet = new Button()

                Left = 190,
                Width = 90,
                Height = 30,
                Top = 75,
                Text = "Internet"
                ;

                Label SynoInternetLabel = new Label()

                Left = 60,
                Width = 350,
                Height = 25,
                Top = 30,
                Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
                ;

                Label fasterLabel = new Label()

                Left = 80,
                Width = 60,
                Height = 20,
                Top = 110,
                Text = "(Rychlejší)"
                ;

                SynoInternet.Width = 380;
                SynoInternet.Height = 170;
                SynoInternet.Controls.Add(fasterLabel);
                SynoInternet.Controls.Add(SynoInternetLabel);
                SynoInternet.Controls.Add(Synology);
                SynoInternet.Controls.Add(Internet);

                Synology.Click += (sender, args) =>

                SynoInternet.Dispose();
                Installation_Functions.InstallBasicSW();
                ;

                Internet.Click += (sender, args) =>

                MessageBox.Show("You clicked Internet");
                ;

                SynoInternet.ShowDialog();


                #endregion






                InstallFunctions class just call this filedownloader:



                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading;
                using System.Threading.Tasks;
                using System.ComponentModel;
                using System.IO;
                using System.Net;
                using System.Threading;

                namespace Ultra_Script

                class FileDownloader


                private readonly string _url;
                private readonly string _fullPathWheretoSave;
                private bool _result = false;
                private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

                public FileDownloader(string url, string fullPathWheretoSave)


                if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
                if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");

                this._url = url;
                this._fullPathWheretoSave = fullPathWheretoSave;



                public bool StartDownload(int timeout)


                try

                System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));

                if (File.Exists(_fullPathWheretoSave))

                File.Delete(_fullPathWheretoSave);

                using (WebClient client = new WebClient())

                var ur = new Uri(_url);
                //client.Credentials = new NetworkCredential("username", "password");
                client.DownloadProgressChanged += WebClientDownloadProgressChanged;
                client.DownloadFileCompleted += WebClientDownloadCompleted;
                Console.WriteLine(@"Stahuji potrebne soubory:");
                client.DownloadFileAsync(ur, _fullPathWheretoSave);
                _semaphore.Wait(timeout);
                return _result && File.Exists(_fullPathWheretoSave);



                catch (Exception e)

                Console.WriteLine("Cant download file");
                Console.Write(e);
                return false;

                finally

                this._semaphore.Dispose();




                private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

                Console.Write($"/r --> e.ProgressPercentage%");


                private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)

                _result = !args.Cancelled;
                if (!_result)

                Console.Write(args.Error.ToString());

                Console.WriteLine(Environment.NewLine + "Download Finished!");
                _semaphore.Release();


                public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)

                return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);









                What is doing its closing the form and then run back function in console. But it only says: "Start downloading" and after timeout it goes: Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll in visual studio



                When i run step by step debugging i think it stopped on that Semaphore method, but its not working even if i delete this method in filedownloader.



                When i call function from console its working normally problem is only when i call it through that button in messagebox.






                share|improve this answer













                So i have another problem.



                I have class like this now:



                using System;
                using System.ComponentModel;
                using System.Drawing;
                using System.Windows.Forms;

                namespace Ultra_Script.MessageBoxes

                public class MsgBoxes

                #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
                public static void SynoInternet()

                Application.EnableVisualStyles();
                Form SynoInternet = new Form();

                Button Synology = new Button()

                Left = 80,
                Width = 90,
                Height = 30,
                Top = 75,
                Text = "Synology"
                ;

                Button Internet = new Button()

                Left = 190,
                Width = 90,
                Height = 30,
                Top = 75,
                Text = "Internet"
                ;

                Label SynoInternetLabel = new Label()

                Left = 60,
                Width = 350,
                Height = 25,
                Top = 30,
                Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
                ;

                Label fasterLabel = new Label()

                Left = 80,
                Width = 60,
                Height = 20,
                Top = 110,
                Text = "(Rychlejší)"
                ;

                SynoInternet.Width = 380;
                SynoInternet.Height = 170;
                SynoInternet.Controls.Add(fasterLabel);
                SynoInternet.Controls.Add(SynoInternetLabel);
                SynoInternet.Controls.Add(Synology);
                SynoInternet.Controls.Add(Internet);

                Synology.Click += (sender, args) =>

                SynoInternet.Dispose();
                Installation_Functions.InstallBasicSW();
                ;

                Internet.Click += (sender, args) =>

                MessageBox.Show("You clicked Internet");
                ;

                SynoInternet.ShowDialog();


                #endregion






                InstallFunctions class just call this filedownloader:



                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading;
                using System.Threading.Tasks;
                using System.ComponentModel;
                using System.IO;
                using System.Net;
                using System.Threading;

                namespace Ultra_Script

                class FileDownloader


                private readonly string _url;
                private readonly string _fullPathWheretoSave;
                private bool _result = false;
                private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

                public FileDownloader(string url, string fullPathWheretoSave)


                if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
                if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");

                this._url = url;
                this._fullPathWheretoSave = fullPathWheretoSave;



                public bool StartDownload(int timeout)


                try

                System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));

                if (File.Exists(_fullPathWheretoSave))

                File.Delete(_fullPathWheretoSave);

                using (WebClient client = new WebClient())

                var ur = new Uri(_url);
                //client.Credentials = new NetworkCredential("username", "password");
                client.DownloadProgressChanged += WebClientDownloadProgressChanged;
                client.DownloadFileCompleted += WebClientDownloadCompleted;
                Console.WriteLine(@"Stahuji potrebne soubory:");
                client.DownloadFileAsync(ur, _fullPathWheretoSave);
                _semaphore.Wait(timeout);
                return _result && File.Exists(_fullPathWheretoSave);



                catch (Exception e)

                Console.WriteLine("Cant download file");
                Console.Write(e);
                return false;

                finally

                this._semaphore.Dispose();




                private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)

                Console.Write($"/r --> e.ProgressPercentage%");


                private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)

                _result = !args.Cancelled;
                if (!_result)

                Console.Write(args.Error.ToString());

                Console.WriteLine(Environment.NewLine + "Download Finished!");
                _semaphore.Release();


                public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)

                return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);









                What is doing its closing the form and then run back function in console. But it only says: "Start downloading" and after timeout it goes: Exception thrown: 'System.ComponentModel.Win32Exception' in System.dll in visual studio



                When i run step by step debugging i think it stopped on that Semaphore method, but its not working even if i delete this method in filedownloader.



                When i call function from console its working normally problem is only when i call it through that button in messagebox.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 at 9:51









                Johnyn CorbieJohnyn Corbie

                54




                54



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid …


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55321924%2fcheck-if-button-is-clicked-from-scratched-made-button%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?