gpt4 book ai didi

c# - 背景 worker 。如何通过在 TreeView 中导航来显示当前进程?

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:02 26 4
gpt4 key购买 nike

<分区>

场景_1.
1. 用户。按下“开始”按钮;
2.程序。在“flowLayoutPanel1”中创建用户元素“ucBackgroundWorker”的实例;
3.程序。在“treeView1”中创建一个节点;

“Scenario_1”可以重复无限次。

场景_2。
有:- 在“flowLayoutPanel1”中启动了几个进程;
- 在“treeView1”中有一棵进程树;

场景:
1. 用户。将光标移动到“treeView1”;
2.程序。在“flowLayoutPanel2”中显示流程(实例
光标所在节点的"ucBackgroundWorker"");

我尝试按照我的理解来实现这个场景 (Scenario_1 + Scenario_2)。我认为可以为用户元素实例分配索引,然后,根据索引,显示在“flowLayoutPanel2”中。
为了测试,这个脚本的按钮做了一个按钮“btShowTheProces”
但是项目不行。
调试时出现错误。
Form3.cs

flowLayoutPanel1.Controls.Add (ucBgWorker [i_ucBWrk]);

错误“对象引用未指向对象的实例。”
ucBgWorker = null;

您的问题
1.如何修复错误?
2. 我是否正确解决了我的问题?
3.还有什么方法可以实现这个场景?
例如,进程没有绑定(bind)到索引,而是绑定(bind)到“字符串”?
例如,像这样的东西

string i_ucBWrk;
i_ucBWrk = "process_1";
flowLayoutPanel1.Controls.Add (ucBgWorker [i_ucBWrk]);

Form3.cs

命名空间 rsh{ 公共(public)部分类 Form3:表格 { //ucBackgroundWorker[] ucBgWorker = null;

    int i_ucBWrk=0;
private ucBackgroundWorker[] ucBgWorker;

int procNumb;

public Form3()
{
InitializeComponent();
}

// после выбора узла дерева
void treeView1_AfterSelect(object sender, TreeViewCancelEventArgs e)
{

}

private void btnStart_Click(object sender, EventArgs e)
{
i_ucBWrk++;

ucBgWorker[i_ucBWrk] = new ucBackgroundWorker();

// ucBgWorker[i_ucBWrk].Done += new Action<string, EventArgs>(Worker_Done);
// ucBgWorker[i_ucBWrk].Cancel += new Action<string, EventArgs>(Worker_Cancel);

flowLayoutPanel1.Controls.Add(ucBgWorker[i_ucBWrk]);

ucBgWorker[i_ucBWrk].Run(1);
// ucBgWorker.Run(Convert.ToInt32(textBox1.Text));
}

void Worker_Done(string arg, EventArgs evtarg)
{
label1.Text = arg + " Done One";
//System.Threading.Thread.Sleep(1000);
}

void Worker_Cancel(string arg, EventArgs evtarg)
{
label1.Text = arg + " Cancel Click";
//System.Threading.Thread.Sleep(1000);
}


// show the process
private void btShowTheProces_Click(object sender, EventArgs e)
{
procNumb = Convert.ToInt32(numericUpDown1.Value);

flowLayoutPanel2.Controls.Clear();
flowLayoutPanel2.Controls.Add(ucBgWorker[procNumb]);
}
}

ucBackgroundWorker.cs

namespace rsh
{
public partial class ucBackgroundWorker : UserControl
{
BackgroundWorker bgWorker = null;
public event Action<string, EventArgs> Done;
public event Action<string, EventArgs> Cancel;

private static bool m_continue = true;

// Уведомляет один или более ожидающих потоков о том, что произошло событие. Этот класс не наследуется.
// https://msdn.microsoft.com/ru-ru/library/system.threading.manualresetevent(v=vs.110).aspx
private ManualResetEvent _resetEvent = new ManualResetEvent(false);
//Semaphore sWaiter = new Semaphore(0, 1);


public ucBackgroundWorker()
{
InitializeComponent();

bgWorker = new BackgroundWorker();

bgWorker.WorkerSupportsCancellation = true;
bgWorker.WorkerReportsProgress = true;

bgWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
bgWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}

public void Run(int counter)
{
if (!bgWorker.IsBusy)
{
bgWorker.RunWorkerAsync(counter);
}
_resetEvent.Set();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int input = int.Parse(e.Argument.ToString());

this.BeginInvoke((MethodInvoker)delegate
{
lblStatus.Text = "Running";
});

for (int i = 1; i <= input; i++)
{
_resetEvent.WaitOne();
Thread.Sleep(1000);
(sender as BackgroundWorker).ReportProgress(i*1); // шаг
// (sender as BackgroundWorker).ReportProgress(i * 10);
if ((sender as BackgroundWorker).CancellationPending)
{
this.BeginInvoke((MethodInvoker)delegate
{
lblStatus.Text = "Cancel";
});

e.Cancel = true;
return;
}
}
Thread.Sleep(1000);
}


// This event handler deals with the results of the
// background operation.
// Этот обработчик событий имеет дело с результатами
// фоновая операция.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
// Сначала обрабатываем случай, когда было создано исключение.
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
if (Cancel != null)
Cancel(this.Name, EventArgs.Empty);
}
else
{
this.BeginInvoke((MethodInvoker)delegate
{
lblStatus.Text = "Done";
});

if (Done != null)
Done(this.Name, EventArgs.Empty);
}
_resetEvent.Reset();
}

// This event handler updates the progress bar.
// Этот обработчик событий обновляет индикатор выполнения.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pBar.Refresh();
pBar.Value = e.ProgressPercentage;
}



// *** *** *** ***
// *** КНОПКИ ***
// *** *** *** ***

// Cancel
private void btnCancel_Click(object sender, EventArgs e)
{
if (bgWorker.IsBusy)
{
bgWorker.CancelAsync();
}
}

// Pause
private void btnPause_Click(object sender, EventArgs e)
{
if (bgWorker.IsBusy)
{
if (btnPause.Text.ToUpper() == "PAUSE")
{
btnPause.Text = "Resume";
m_continue = false;
_resetEvent.Reset();
}
else
if (btnPause.Text.ToUpper() == "RESUME")
{

btnPause.Text = "Pause";
m_continue = true;
_resetEvent.Set();

}
}
}


}
}

项目 - link

enter image description here

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com