我正在创建包含某些数据循环的DLL
我如何显示进度栏
我试图创建新的Windows窗体并在for循环中显示相同的窗口
但是它要求我每次关闭表格
答案 0 :(得分:0)
SO的人们不是在这里为您编写代码,而是在解决问题。无论如何,我将告诉您如何做到这一点,并基于此编写自己的代码。
首先,DLL是动态链接库,您可以将其附加到您拥有的任何项目中(winform或unity 3d游戏,不,您不会这样做,但是可以在两种情况下使用它)如果您已经在编写DLL,请使其在很多情况下都可用,并让使用它的程序员有很多可能的操纵。
因此,您的任务分为两部分。
对于此任务,我们将使用事件和简单的for
循环向您展示其工作原理。
首先,让我们创建一个EventArgs
类,该类将存储程序员在其他代码捕获事件中想要传递的所有数据
public class CustomEventArgs
{
public int OldResult { get; set; }
public int NewResult { get; set; }
}
现在,当我们有了事件类时,让我们在代码中实现它。
public class YourDllCalculation
{
// In the .NET Framework class library, events are based on the EventHandler delegate and the EventArgs base class.
// So we create delegate using our newly created class to represents it like EventHandler
public delegate void ResultChangeEventHandler(object sender, CustomEventArgs e);
// Now we create our event
public event IzborRobeEventHandler ResultChanged;
// Local storing variable
private int Result = 0;
// This is method from which you inform event something changed and user listening to event catch EventArgs passed (in our case our CustomEventArgs)
protected virtual void OnResultChange(CustomEventArgs e)
{
ResultChangeEventHandler h = ResultChanged;
if (h != null)
h(this, e);
}
// We will use this method from new code to start calculation;
public void StartCalculation()
{
// Calculation will be done in separate thread so your code could proceed further if needed
Thread t1 = new Thread(Calculate);
t1.Start();
}
private void Calculate()
{
for(int i = 0; i < 100; i++)
{
OnResultChange(new CustomEventArgs() { OldResult = i, NewResult = i + 1 });
Result = i;
Thread.Sleep(1000); // Pause thread from running for 1 sec
}
}
}
现在,当我们有代码时,可以在winform中像这样使用它:
// Add at top
using YourDllNamespace;
public YourForm()
{
// Creating our class for calculation
YourDllCalculation calc = new YourDllCalculation();
calc += CalculationResultChanged;
calc.Calculate();
}
private void CalculationResultChanged(object sender, CustomEventArgs e)
{
// Here do whatever you want with passed values
// e.OldResult;
// e.NewResult;
// it will fire each second
}