0

プログレスバーを使用してユーザーにプログラムの開発を表示するアプリケーションを開発しています。私が書いているプログラムは、プログレスバーを使用して、実行中に送信されたデータのパケット数をユーザーに示します。基盤となるライブラリに、別のパケットが送信されるとトリガーされるイベントを作成しました。私が設定したイベントラインは次のとおりです。

public delegate void ChangedEventHandler(object sender, EventArgs e);
public event ChangedEventHandler PercentageUpdate;

protected internal virtual void OnPercentageChanged(EventArgs e)
{
        if (PercentageUpdate != null) PercentageUpdate(this, e);
}

上記のコードは、基礎となるライブラリに設定されていることに注意してください。このライブラリで次のようにイベントをトリガーしました。

//reinitializing is extraneous, as Receive() calls are overwriting
try
{
    fsa.rnd = fsa.TransferSocket.Receive(fsa.File_Buffer, 0, FSArgs.BlockSize, SocketFlags.None);
}
catch (Exception e)
{
    fsa.Dispose();
    throw new GeneralNetworkingException("FileSocket receive() failed!", e);
}

fsa.tot += Convert.ToInt64(fsa.rnd);
fsa.TransferPercentage = (fsa.tot/fsa.FileSize) * 100;
fsa.OnPercentageChanged(EventArgs.Empty);   //throw event for form

最後に、アプリケーションで、次のようにイベントを初期化します。

public delegate void UpdatePerc(int index);



f_list[f_list.Count-1].fsa.PercentageUpdate += (sender, e) => filesocket_percentage_updated(sender, e, sel_index);



private void filesocket_percentage_updated(object sender, EventArgs e, int index)
{   
        UpdateP(index);
}



private void UpdateP(int index)
{
    if (progressBar1.InvokeRequired)
    {
        this.BeginInvoke(new UpdatePerc(UpdateP), new object[] { index });
    }
    else
    {
        if (sel_index == index)     //then redraw progressbar
        {
            if (x_list[index] > 1)
            {
                y_list[index]++;
                if (((int)(y_list[index] % x_list[index])) == 0)
                {
                    z_list[index]++;
                    progressBar1.Increment(1);
                    progressBar1.Update();
                }
            }
            else
            {   
                z_list[index]++;
                progressBar1.Increment(1);
                progressBar1.Update();
                richTextBox1.Text += (z_list[index]+ '\n');
            }
        }
        else
        {
            if (x_list[index] > 1)
            {
                y_list[index]++;
                if (((int)(y_list[index] % x_list[index])) == 0)
                {
                    z_list[index]++;
                }
            }   
            else
            {
                z_list[index]++;
            }
        }
    }
}

ご覧のとおり、プログレスバーを呼び出して並列スレッドで更新しようとしています。しかし、何も起こりません。どんな助けでも大歓迎です!

ありがとう。

4

0 に答える 0