views:

41

answers:

2

I am trying to achieve an update form.

I use a library to open a form when there is an updated file and download using edtFTPNet

In the form I pass the FTP object and start download, in FormLoad i handle two events and i use Thread to StartDownload(). My two events never invoking, i use them to set a progress bar.

public partial class UpdateProgressForm : XtraForm
{
    public FTPConnection FtpConn { get; set; }
    public string UpdateFileName { get; set; }

    public UpdateProgressForm()
    {
        InitializeComponent();
    }

    private void OnLoad(object sender, EventArgs e)
    {
        FtpConn.Downloading += FileDownLoading;
        FtpConn.BytesTransferred += FileBytesTransfered;
    }

    private void FileDownLoading(object sender, FTPFileTransferEventArgs e)
    {
        progressBar.Properties.Maximum = (int) e.FileSize;
    }

    private void FileBytesTransfered(object sender, BytesTransferredEventArgs e)
    {
        progressBar.Position = (int) e.ByteCount;
    }

    public void StartDownload()
    {
        FtpConn.DownloadFile(@".\" + UpdateFileName, UpdateFileName);
    }

    private void OnShown(object sender, EventArgs e)
    {
        Thread tt = new Thread(StartDownload) {IsBackground = true};
        tt.Start();
    }
}

Library method calling the Form:

private void DownloadUpdateFile(string updateFileName)
{
    using (ProgressForm = new UpdateProgressForm { FtpConn = FtpConn, UpdateFileName = updateFileName })
    {
        ProgressForm.ShowDialog();
    }
}

Any help? Thank you.

A: 

Are you sure that the event handlers are not invoked? I think your problem rather is that you try to update the progress bar on the worker thread on which the event handlers are invoke (which is not the thread on which the GUI was created). You should make sure that the GUI updates are performed on the correct thread:

private void FileDownLoading(object sender, FTPFileTransferEventArgs e)
{
    progressBar.Invoke((MethodInvoker) delegate 
    { 
        progressBar.Properties.Maximum = (int) e.FileSize;
    });
}
Fredrik Mörk
i changed the code like you show but nothing is happening again. In the library that is the caller of the form, soon as i close the form the IsDownloaded handler which is handled in the library is invoked properly. Inside the form is the problem. I Edit with the library code sample. And i get Exception of TargetInvocation
@gtas: please update the question with the details of the TargetInvocationException.
Fredrik Mörk
Exception occurs while i close the form where the event is invoked once!!! i will regenerate and add
System.Reflection.TargetInvocationException: {"Exception has been thrown by the target of an invocation."}Inner: {"Invoke or BeginInvoke cannot be called on a control until the window handle has been created."}
A: 
  1. Take a look in the designer and make sure you subscribe to those events
  2. Make sure you Instanciate and Show the from from the Main Thread.
Itay
I subscribe to the events in the Load form event as you see. And im sure i instantiate and show the form in the main thread, there is only this Method call i use in a separate thread, because other wise the UI hangs till it is finished (SYNC Transfer as i could call it).
I meant to the OnLoad and OnShown events.
Itay
Seems that the subscribe happens before starting the thread. OnLoad before OnShown
OnLoad run before OnShown - but... are you sure you subscribed to the OnLoad and OnShown events? Are you positive that these functions are being called?
Itay
YES i am 100%, i checked it
Was the file downloaded? Try to set the Thread as foreground thread.besides - you will need Invoke/BeginInvoke on the progress bar updates.
Itay