topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 10:48 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: My Application...  (Read 16712 times)

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
My Application...
« on: January 07, 2008, 10:25 PM »
Im using BCB 2007 to code my application for windows vista... My app connects to a declared ftp server just fine, but when it begins to download files it locks the application up until the download is done... Is there some kind of coding that I can add to allow my program to run fine?

Any help is greatly appreciated... thnx :)
CodeByter.com - http://www.codebyter.com

jazper

  • Coding Snacks Author
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 92
    • View Profile
    • Jazper's Software
    • Donate to Member
Re: My Application...
« Reply #1 on: January 07, 2008, 10:32 PM »
Are you using Indy's FTP client component to achieve this?  You could run all your FTP work in a background thread thus freeing up the UI processing.



Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #2 on: January 08, 2008, 07:50 AM »
Yes, Im using Indy's FTP client and thats a wonderful idea... I have no idea how to go about doing this though, anyway you could write a quick example or have a tutorial I could check out? Any time would greatly be appreciated alot :)
CodeByter.com - http://www.codebyter.com

jazper

  • Coding Snacks Author
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 92
    • View Profile
    • Jazper's Software
    • Donate to Member
Re: My Application...
« Reply #3 on: January 08, 2008, 08:44 AM »
Unfortunately, I'm not a BCB programmer. I asked about Indy because I am a Delphi programmer. I don't think a Delphi example would be of much help to you.  Maybe another BCB programmer on the forums can provide a small example on threading in BCB.






f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: My Application...
« Reply #4 on: January 08, 2008, 09:01 AM »
Unfortunately, I'm not a BCB programmer. I asked about Indy because I am a Delphi programmer. I don't think a Delphi example would be of much help to you.  Maybe another BCB programmer on the forums can provide a small example on threading in BCB.
Will probably help more than you think - Delphi/ObjectPascal and C++ aren't that different, syntax-wise, and afaik BCB and Deplhi use the exact same VCL (in fact, I recall BCB shipping with a command-line Delphi compiler, probably to facilitate compiling the VCL or third-party components).
- carpe noctem

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #5 on: January 08, 2008, 09:12 AM »
BCB does come with a command-line Delphi compiler for 3rd party components and such... If you have a delphi example, I might be able to make out the idea of how to do this, or at least I can try. ty again guys! this is helping a great deal! :)
CodeByter.com - http://www.codebyter.com

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: My Application...
« Reply #6 on: January 08, 2008, 09:18 AM »
Threading is simple, anyway.

Right until you need to synchronize access to global data. Then it becomes potentially hellish.
- carpe noctem

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #7 on: January 08, 2008, 09:24 AM »
lol, i have no idea how to sychronize access to global data... :( is running FTP in a background thread classified as access to global data? this is entirely new to me
CodeByter.com - http://www.codebyter.com

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: My Application...
« Reply #8 on: January 08, 2008, 09:38 AM »
lol, i have no idea how to sychronize access to global data... :( is running FTP in a background thread classified as access to global data? this is entirely new to me
Not necessarily... but if, for instance, your GUI can add a filename to a queue, and the FTP thread grabs filenames from the queue, you will need to synchronize access... check out EnterCriticalSection + LeaveCriticalSection API calls.
- carpe noctem

jazper

  • Coding Snacks Author
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 92
    • View Profile
    • Jazper's Software
    • Donate to Member
Re: My Application...
« Reply #9 on: January 08, 2008, 09:56 AM »
Here you go, this is Delphi.


Form1
  - 3 Edit boxes, named: edServer, edUsername, edPassword
  - 1 Memo box named: edOutput

Code: Delphi [Select]
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, Unit2;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     btnDisconnect: TButton;
  12.     btnConnect: TButton;
  13.     edUsername: TEdit;
  14.     edPassword: TEdit;
  15.     edServer: TEdit;
  16.     edOutput: TMemo;
  17.     procedure btnConnectClick(Sender: TObject);
  18.   private
  19.     { Private declarations }
  20.     ThreadFTP: TidFTPThread;
  21.   public
  22.     { Public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.dfm}
  31.  
  32. procedure TForm1.btnConnectClick(Sender: TObject);
  33. begin
  34.   ThreadFTP := TidFTPThread.Create(true);
  35.   ThreadFTP.LoginInfo(edUsername.Text, edPassword.Text, edServer.Text);
  36.   ThreadFTP.Resume;
  37.  
  38. end;
  39.  
  40. end.


here is the thread class
Code: Delphi [Select]
  1. unit Unit2;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, SysUtils,IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  7.   IdExplicitTLSClientServerBase, IdFTP;
  8.  
  9. type
  10.   TidFTPThread = class(TThread)
  11.   private
  12.     { Private declarations }
  13.     Fftp: TidFTP;
  14.     FUser: string;
  15.     FPass: string;
  16.     FServer: string;
  17.     FMemoMessage: string;
  18.   public
  19.     procedure LoginInfo(User, Pass, Server: string);
  20.   protected
  21.     procedure Execute; override;
  22.     procedure UpdateMemo;
  23.   end;
  24.  
  25. implementation
  26.  
  27. uses Unit1;
  28.  
  29. { TidFTPThread }
  30.  
  31. procedure TidFTPThread.LoginInfo(User, Pass, Server: string);
  32. begin
  33.   FUser := User;
  34.   FPass := Pass;
  35.   FServer := Server;
  36.  
  37.  
  38.  
  39. end;
  40.  
  41. procedure TidFTPThread.UpdateMemo;
  42. begin
  43.   Form1.edOutput.Lines.Add(FMemoMessage);
  44. end;
  45.  
  46. procedure TidFTPThread.Execute;
  47. begin
  48.   FMemoMessage := 'Connecting to ' + FServer;
  49.   Synchronize(UpdateMemo);
  50.   Fftp := TidFTP.Create(nil);
  51.   Fftp.Username := FUser;
  52.   Fftp.Password := FPass;
  53.   Fftp.Host := FServer;
  54.   try
  55.     try
  56.       Fftp.Connect;
  57.  
  58.       FMemoMessage := 'Connected to ' + FServer + #13#10 + 'Changing directory';
  59.       Synchronize(UpdateMemo);
  60.  
  61.       Fftp.ChangeDir('<CHANGE TO DIR WHERE YOU NEED FILE>');
  62.  
  63.       FMemoMessage := 'Getting file...';
  64.       Synchronize(UpdateMemo);
  65.  
  66.       Fftp.Get('<SOURCE FILENAME>','<DEST FILENAME>', true, false);
  67.  
  68.       Fftp.Disconnect;
  69.  
  70.       FMemoMessage := 'Disconnecting from ' + FServer;
  71.       Synchronize(UpdateMemo);
  72.  
  73.     except on E:Exception do
  74.       begin
  75.         FMemoMessage := E.Message;
  76.         Synchronize(UpdateMemo);
  77.       end;
  78.     end;
  79.   finally
  80.     Fftp.Free;
  81.  
  82.   end;
  83. end;
  84.  
  85. end.




Hopefully this helps.

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #10 on: January 08, 2008, 09:59 AM »
wow, tyvm! this is awesome and im understanding this for the most part! way cool, ty again!
CodeByter.com - http://www.codebyter.com

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #11 on: January 08, 2008, 03:43 PM »
Alright, so status update... I wrote my first threaded program that uses 2 threads, 1 thread that accesses global variables and another thread that downloads files from a list of "Files to be downloaded" lol... This coding helped alot! I do have a question however, what did you use to drop in that coding for me to see? I wanted to upload a sample for people to see how it works in Borland C++ builder...
CodeByter.com - http://www.codebyter.com

jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: My Application...
« Reply #12 on: January 08, 2008, 04:02 PM »
It's simple, CodeByte!
Just copy the code from your editor, then paste it on the reply box. Then select all the code, and press the # button, which will enclose the code in [code] tags.

PS: if you'd like to have syntax highlighting, press the # button, and then add =cpp to the [code], so that is looks like this:

[code=cpp]
int main void(){
whatever
}
[/code]

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #13 on: January 08, 2008, 07:09 PM »
thats way cool! ill upload the coding once I finish a basic template for multithreading in BCB 2007. ty for all the help guys!
CodeByter.com - http://www.codebyter.com

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #14 on: January 09, 2008, 07:39 AM »
Here is a general idea of Threading for someone new to the concept...

Create a new project and add 2 Buttons, named Suspend and Resume... These will control the Thread using the OnClick() function.

Here is the coding I used along with BCB RAD Studio 2007:
(Keep in mind this is a sample template I wrote...)

Code: C++ [Select]
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "IdExplicitTLSClientServerBase"
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. HANDLE Thread; //THIS IS FOR THE THREAD
  12. //---------------------------------------------------------------------------
  13. DWORD WINAPI ThreadFunc(LPVOID Param)
  14. {
  15.         //Tell the Thread what to do here
  16. }
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19.         : TForm(Owner)
  20. {
  21.         DWORD Id;
  22.         Thread = CreateThread(0, 0, ThreadFunc, Form1->Handle, CREATE_SUSPENDED, &Id);
  23.         if(!Thread)
  24.         {
  25.                 ShowMessage("Cannot Create Thread!");
  26.                 Application->Terminate();
  27.         }
  28. }
  29. //---------------------------------------------------------------------------
  30. void __fastcall TForm1::ResumeClick(TObject *Sender)
  31. {
  32.    ResumeThread(Thread);
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TForm1::SuspendClick(TObject *Sender)
  36. {
  37.    SuspendThread(Thread);      
  38. }
  39. //---------------------------------------------------------------------------

Ill have a sample of how to set priorities between threads soon, once I fully embrace the concept.
CodeByter.com - http://www.codebyter.com
« Last Edit: January 09, 2008, 07:42 AM by Codebyte »

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: My Application...
« Reply #15 on: January 09, 2008, 08:22 AM »
Notice that if your thread function exits, the thread is destroyed - generally, a thread will keep looping, often doing "wait for event; process; repeat;".

BCB doesn't have it's own support functions for threading? If it does, it might be best to use those (there can be some "advanced issues" involved that requires per-thread data and other nastiness).

Also, again I must state that if you need to access global variables in read/write fashion, you need synchronization; synchronization bugs can be very hard to spot, and many of them will not show unless you have a dualcore (or quadcore or...) machine, but will blow up at the least expected time :)
- carpe noctem

jazper

  • Coding Snacks Author
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 92
    • View Profile
    • Jazper's Software
    • Donate to Member
Re: My Application...
« Reply #16 on: January 09, 2008, 08:27 AM »
I wanted to share this link as well, it's VERY useful for Delphi developers and it might be useful to BCB developers as well.

"Multithreading - The Delphi Way"
http://codecentral.b...m/Item.aspx?id=14809






Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #17 on: January 09, 2008, 10:35 AM »
f0dder, i think you're right, although Im still learning more and more about threads between my college classes lol... I think I remember reading about it though...

btwn: Great Tutorial, Jazper! I really liked it! Everywhere I look, I cant seem to find any useful information and both of the things you have showed me helped alot... I might possibly do a full write-up/tutorial on multithreading from my point of view, someone who is new to the entire concept lol...
CodeByter.com - http://www.codebyter.com

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #18 on: January 10, 2008, 03:02 PM »
Bad news: I am getting this error now:
------------------------------------------------
Project F:\Borland Programs\Project1\Debug\VSU.exe faulted with message: 'application-define exception (code 0x0eedfade) at 0x75ecb09e.' Process Stopped. Use Step or Run to continue.

[OK Button]
------------------------------------------------
This is the error I get right after I compile the program. If I goto run the program It gets to the part right before it downloads at says, "VSU.exe has stopped working..." I have no idea what this is, but if anyone could help that would be awesome... Also, if you need anything further, let me know.
CodeByter.com - http://www.codebyter.com

jazper

  • Coding Snacks Author
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 92
    • View Profile
    • Jazper's Software
    • Donate to Member
Re: My Application...
« Reply #19 on: January 10, 2008, 04:17 PM »
Show us some code.  Particularly the code that starts the download, and the code that actually does the downloading.  You may have 2 threads stepping on one another.

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #20 on: January 10, 2008, 08:26 PM »
Alright, so to be completely honest... I dont know how this coding will look but please dont kill me... Im a novice... lol! Here is the complete coding... Also, keep in mind that it's no where near finished... I also tried to learn the coding for the threading so bear with me...

Code: C++ [Select]
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. #include "registry.hpp"
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. #pragma link "IdExplicitTLSClientServerBase"
  11. #pragma resource "*.dfm"
  12. TForm1 *Form1;
  13. HANDLE Thread;
  14. int CurrentVersion, counter = 0, ServerVersionOnWeb;
  15. int downloadrate = 0, currentdownloadtotal = 0, previousdownloadtotal = 0, downloadtime = 0, totalrate = 0;
  16. int totaldownloadtotal = 0;
  17. String filename;
  18. bool required = false;
  19. bool start_install = false;
  20. //---------------------------------------------------------------------------
  21. DWORD WINAPI ThreadFunction(LPVOID Param)
  22. {
  23.    //if Memo2 has anything in it, start downloading
  24.    do
  25.    {
  26.            try
  27.            {
  28.                    filename = Form1->Memo2->Lines->Strings[0];
  29.                    //get size and update progressbar
  30.                    Form1->ProgressBar2->Max = Form1->FTP1->Size(filename);
  31.                    Form1->ProgressBar2->Position = 0;
  32.                    Form1->FTP1->Get(filename, filename, true, false);
  33.            }
  34.            catch(...)
  35.            {
  36.                    Form1->FTP1->Abort();
  37.                    Form1->FTP1->Disconnect();
  38.                    Application->Terminate();
  39.            };
  40.    }
  41.    while(Form1->Memo2->Lines->Strings[0] != "");
  42.    if(Form1->Memo2->Lines->Strings[0] == "")
  43.    {
  44.            required = false;
  45.            Form1->Label1->Caption = "Overall Progress: Download Complete!";
  46.            Form1->ProgressBar1->Position += 1;
  47.            Form1->Timer1->Enabled = false;
  48.            Form1->Label2->Caption = "File Progress: ";
  49.            //start next process
  50.        start_install = true;
  51.            SuspendThread(Thread);
  52.    }
  53. }
  54. //---------------------------------------------------------------------------
  55. void ExitApp(void)
  56. {
  57.         try
  58.         {
  59.            //Free all open resources, then exit the app
  60.            Form1->FTP1->Abort();
  61.            Form1->FTP1->Disconnect();
  62.            Application->Terminate();
  63.         }
  64.         catch(...){};
  65. }
  66. //---------------------------------------------------------------------------
  67. void FilterMemo12(void)
  68. {
  69.         //filter Memo1
  70.         int x = 0;
  71.         do
  72.         {
  73.                 if(Form1->Memo1->Lines->Strings[x] == "")
  74.                 {
  75.                         Form1->Memo1->Lines->Delete(x);
  76.                 }
  77.                 else
  78.                 {
  79.             x++;
  80.                 }
  81.         }
  82.         while(x < Form1->Memo1->Lines->Count);
  83.         //repeat the process for Memo2
  84.         x = 0;
  85.         do
  86.         {
  87.                 if(Form1->Memo2->Lines->Strings[x] == "")
  88.                 {
  89.                         Form1->Memo2->Lines->Delete(x);
  90.                 }
  91.                 else
  92.                 {
  93.             x++;
  94.                 }
  95.         }
  96.         while(x < Form1->Memo2->Lines->Count);
  97.         //now allow Threading Process to begin
  98.         Form1->Label1->Caption = "Overall Progress: Downloading Updates...";
  99.         Form1->ProgressBar1->Position += 1;
  100.         Form1->Timer1->Enabled = true;
  101.         Form1->ProgressBar1->Max += Form1->Memo2->Lines->Count;
  102.         required = true;
  103.         //Set Cursor to beginning of Memo1, so user can see top of Memo
  104.         ResumeThread(Thread);
  105. }
  106. //---------------------------------------------------------------------------
  107. void CompareVersions(void)
  108. {
  109.         //Compare CurrentVersion and ServerVersionOnWeb
  110.         if(CurrentVersion >= ServerVersionOnWeb)
  111.         {
  112.                 //close program and launch vsu
  113.                 ExitApp();
  114.                 ShellExecute(GetDesktopWindow(), "open", "VSU.exe", NULL, NULL, SW_SHOWNORMAL);
  115.         }
  116.         else if(CurrentVersion < ServerVersionOnWeb)
  117.         {
  118.                 //VSU needs to be updated, download nufls.vor, swn.vor
  119.                 //after these files are downloaded, then begin to download the files
  120.                 //contained in "nufls.vor"
  121.                 try
  122.                 {
  123.                         Form1->Label1->Caption = "Overall Progress: Preparing Information...";
  124.                         Form1->ProgressBar1->Position += 1;
  125.                         Form1->ProgressBar2->Max = Form1->FTP1->Size("nufls.vor");
  126.                         required = false;
  127.                         Form1->FTP1->Get("nufls.vor", "nufls.vor", true, false);
  128.                         Form1->ProgressBar2->Max = Form1->FTP1->Size("swn.vor");
  129.                         Form1->FTP1->Get("swn.vor", "swn.vor", true, false);
  130.                         //done downloading important updates, now begin showing them and
  131.                         //preparing data for final update process
  132.                         Form1->Memo2->Clear();
  133.                         Form1->Memo2->Lines->LoadFromFile("nufls.vor");
  134.                         Form1->Memo1->Clear();
  135.                         Form1->Memo1->Lines->LoadFromFile("swn.vor");
  136.                         //done displaying content, now filter spaces in memo1, memo2
  137.                         FilterMemo12();
  138.                 }
  139.                 catch(...)
  140.                 {
  141.             ExitApp();
  142.         }
  143.     }
  144. }
  145. //---------------------------------------------------------------------------
  146. void FTPConnect(void)
  147. {
  148.         //Connect to FTP
  149.         try
  150.         {
  151.                 Form1->FTP1->Connect();
  152.         }
  153.         catch(...)
  154.         {
  155.         ExitApp();
  156.     }  
  157. }
  158. //---------------------------------------------------------------------------
  159. void CheckRegistry(void)
  160. {
  161.    //Check the registry for the current server version installed on the
  162.    //system, so we can come back and check to see if the current version is
  163.    //more recent than the latest update or not.
  164.    Form1->Label1->Caption = "Overall Progress: Checking Registry...";
  165.    Form1->ProgressBar1->Position += 1;
  166.    TRegistry *Reg = new TRegistry();
  167.    Reg->RootKey = HKEY_LOCAL_MACHINE;
  168.    AnsiString temp;
  169.    char gSerial[40];
  170.    if(Reg->KeyExists("SOFTWARE\\VSU"))
  171.    {
  172.            //try to open the key and read from it
  173.            try
  174.            {
  175.                    //
  176.                    if(Reg->OpenKey("SOFTWARE\\VSU", FALSE))
  177.                    {
  178.                            //key has been opened now begin reading
  179.                            temp = Reg->ReadString("ServerVersion");
  180.                            strcpy(gSerial, temp.c_str());
  181.                            Reg->CloseKey();
  182.                            CurrentVersion = StrToInt(gSerial);
  183.                            //RETRIEVED THE KEY, NOW CONTINUE WITH PROCESS
  184.                            Form1->Show();
  185.                            Form1->FTPConnectTimer->Enabled = true;
  186.                    }
  187.                    else
  188.                    {
  189.                            //could not open the key for 2 reasons
  190.                            //1) Vista needs to run the program with administrative rights
  191.                            //2) no idea lol
  192.                            /*                     close or no??                       */
  193.                            ExitApp();
  194.                    }
  195.            }
  196.            catch(...)
  197.            {
  198.                    //could not open the key and read from it, close the program
  199.                    ExitApp();
  200.            };
  201.    }
  202.    else
  203.    {
  204.            //could not open the key, because it probably does not exist
  205.            CurrentVersion = 0;
  206.            Form1->Show();
  207.            Form1->FTPConnectTimer->Enabled = true;
  208.    }
  209. }
  210. //---------------------------------------------------------------------------
  211. __fastcall TForm1::TForm1(TComponent* Owner)
  212.         : TForm(Owner)
  213. {
  214.    DWORD Id;
  215.    Thread = CreateThread(0, 0, ThreadFunction, Form1->Handle, CREATE_SUSPENDED, &Id);
  216.    if(!Thread)
  217.    {
  218.                 //ADD CODING FOR WHAT TO DO WHEN THREAD CANNOT BE CREATED
  219.                 Application->Terminate();
  220.    }
  221.    else
  222.    {
  223.                 //BEGIN PROCESSING
  224.                 CheckRegistry();
  225.    }
  226. }
  227. //---------------------------------------------------------------------------
  228. void __fastcall TForm1::FTP1AfterClientLogin(TObject *Sender)
  229. {
  230.    Form1->Label1->Caption = "Overall Progress: Connected to the VSU Network!";
  231.    ProgressBar1->Position += 1;
  232.    //Client has connected, begin the process of file downloads
  233.    try
  234.    {
  235.            //try to get the csv.vor file
  236.            ProgressBar2->Max = FTP1->Size("csv.vor");
  237.            //tell the FTP component that this is not a required file
  238.            required = false;
  239.            Form1->Show();
  240.            FTP1->Get("csv.vor", "csv.vor", true, false);
  241.            Memo3->Clear();
  242.            Memo3->Lines->LoadFromFile("csv.vor");
  243.            ServerVersionOnWeb = StrToInt(Memo3->Lines->Strings[0]);
  244.            CompareVersions();
  245.    }
  246.    catch(...)
  247.    {
  248.        ExitApp();
  249.    };
  250. }
  251. //---------------------------------------------------------------------------
  252.  
  253. void __fastcall TForm1::FTP1WorkEnd(TObject *ASender, TWorkMode AWorkMode)
  254. {
  255.    //check to see if the file that was downloaded was a required file for the updater
  256.    if(required == true)
  257.    {
  258.            Memo2->Lines->Delete(0);
  259.            totaldownloadtotal += ProgressBar2->Max;
  260.            ProgressBar1->Position += 1;
  261.    }
  262. }
  263. //---------------------------------------------------------------------------
  264.  
  265. void __fastcall TForm1::FTP1Work(TObject *ASender, TWorkMode AWorkMode,
  266.       int AWorkCount)
  267. {
  268.    ProgressBar2->Position = AWorkCount;
  269. }
  270. //---------------------------------------------------------------------------
  271. void __fastcall TForm1::FTPConnectTimerTimer(TObject *Sender)
  272. {
  273.    FTPConnectTimer->Enabled = false;
  274.    FTPConnect();
  275. }
  276. //---------------------------------------------------------------------------
  277.  
  278. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  279. {
  280.   try
  281.   {
  282.         downloadtime += 1;
  283.         currentdownloadtotal = ProgressBar2->Position;
  284.         totalrate += (totaldownloadtotal + currentdownloadtotal) - previousdownloadtotal;
  285.         previousdownloadtotal = totaldownloadtotal + currentdownloadtotal;
  286.         downloadrate = totalrate / downloadtime;
  287.         Form1->Label2->Caption = "File Progress: "+filename+"... "+IntToStr(downloadrate/1024)+" KB/Second...";
  288.   }
  289.   catch(...){};
  290. }
  291. //---------------------------------------------------------------------------
  292.  
  293. void __fastcall TForm1::Timer2Timer(TObject *Sender)
  294. {
  295.         if(start_install == true)
  296.         {
  297.                 //
  298.                 Timer2->Enabled = false;
  299.                 Label1->Caption = "Overall Progress: Installing Files...";
  300.                 //begin the installation process by calling 'sinsall.bat'
  301.                 ShellExecute(GetDesktopWindow(), "open", "sinstall.bat", NULL, NULL, SW_HIDE);
  302.                 //begin timer
  303.                 Timer3->Enabled = true;
  304.     }  
  305. }
  306. //---------------------------------------------------------------------------
  307.  
  308. void __fastcall TForm1::Timer3Timer(TObject *Sender)
  309. {
  310.    Timer3->Enabled = false;
  311.    //installation done, now close and restart 'VSU.exe'
  312.    Label1->Caption = "Overall Progress: Installation Complete! Launching VSU Server...";
  313.    ExitApp();
  314. }
  315. //---------------------------------------------------------------------------
CodeByter.com - http://www.codebyter.com

Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #21 on: January 11, 2008, 12:16 PM »
lol, any ideas as to why that error occurs?
CodeByter.com - http://www.codebyter.com

jazper

  • Coding Snacks Author
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 92
    • View Profile
    • Jazper's Software
    • Donate to Member
Re: My Application...
« Reply #22 on: January 12, 2008, 11:39 AM »
You need to do some research on thread synchronization now.   In my example above I used Synchronize() to safely set values in the Main thread from my background thread.  Synchronize is basically a wrapper around Critical sections.  You'll need to use Critical Sections to access objects/variables in the Main thread. 




Codebyte

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 160
  • "Premature Optimization is the root of all evil."
    • View Profile
    • CodeByter.com
    • Donate to Member
Re: My Application...
« Reply #23 on: January 22, 2008, 11:06 AM »
ok, looking into that now. Thank you for all the time spent and sorry for the late response. Things got crazy with college.
CodeByter.com - http://www.codebyter.com