I am new at c# so i am not even sure if this is possible.

This application i am making is a win app and its purpose is to make xor encrypted file.

when i try to encrypt large file it takes long time so i am trying to make the program to ask user if he or she wants to continue when they click anywhere on the program.

this is the function which i want to pause when someone click anywhere on the program:

private bool encryptt()
       {
           int let = 0;
           long length;
           fl.go = true;
           FileStream open = new FileStream(fl.open, FileMode.Open);
           FileStream save = new FileStream(fl.save, FileMode.OpenOrCreate);
           length = open.Length;
           this.enc_progressbar.Maximum = Convert.ToInt32(open.Length);
           while(length > open.Position && fl.go == true)
           {
               if (fl.key.Length != let)
               {
                   save.WriteByte(Convert.ToByte(open.ReadByte() ^ fl.key[let]));
                   this.enc_progressbar.Value++;
                   let++;
               }
               else
               {
                   let = 0;
               }
               while (fl.pause == true) ;
           }
           open.Close();
           open.Dispose();
           save.Close();
           save.Dispose();
           if(fl.go == true)
           {
               return true;
           }
           else
           {
               File.Delete(fl.save);
               return false;
           }
       }

basically i want this code to be activated while the function above is active:

if (fl.go == true)
           {
               fl.pause = true;
               DialogResult d = MessageBox.Show("Did you want to cancel this process.", "Stop Process", MessageBoxButtons.YesNo);
               if (d == DialogResult.Yes)
               {
                   fl.go = false;
               }
           }

if u need any information on what the code above is doing please ask me.

Recommended Answers

All 5 Replies

>i am trying to make the program to ask user if he or she wants to continue
>when they click anywhere on the program.
That's extremely unconventional behavior. If I may offer a recommendation, how about using a progress bar and a cancel button? Naturally the encryption should be done in a separate thread so that the GUI is still usable. Not only is that easier that what you're asking, it's also what users will expect to see.

Thanks for the quick reply. I will take your advice make a cancel button instead of clicking anywhere but, I have no idea on how to use Thread. I was researching on how to use them but I only got confused.

This is a really newb quest but how exactly do threads work.

>This is a really newb quest but how exactly do threads work.
It's not a newb question at all, so no worries. A thread is a thread of execution, which is a fancy way of saying that CPU time is divided between the running threads and each one is worked on in chunks. The net effect is that it appears as if multiple threads are executing simultaneously.

The nice thing about C# and .NET in general is that threads are dead easy to work with. Here is a tutorial introduction, and also a whole website that you should bookmark when working with .NET. :)

That helps a lot. I think i can make this program even better if I start to use threads.

The nice thing about C# and .NET in general is that threads are dead easy to work with. Here is a tutorial introduction, and also a whole website that you should bookmark when working with .NET. :)

thank you very much for that link. it is an incredibly helpful link and may actually speed up my work on my project!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.