在C#中同时运行两个函数。 [英] Running Two Functions at the Same Time in C#.

查看:512
本文介绍了在C#中同时运行两个函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中创建了一个类库,用于扫描一系列文件。在我使用此DLL作为参考的主项目中,我想创建一个进度条,显示到目前为止已扫描了多少文件。



dll类使用foreach循环,因为它的目的是将每个文件转换为哈希码。 foreach循环已用于将文件的每个转换段连接在一起,这样我就可以拥有512位长的代码,然后进入下一个文件。



我在dll类中定义了一个变量 - >

public static int value_ = 0;

此变量在foreach循环结束时更新。



现在在我的主项目中,我想在我的类库项目中的函数运行的同时使用此变量来更新我的进度条。



例如:



DLL.function();

pb .value = value_;



我将不胜感激如何实现这一点。

解决方案

< blockquote>你听说过线程吗?

线程教程 [ ^ ]

如何创建主题 [ ^ ]

如何:创建和终止线程(C#编程指南) [ ^
有人建议你进行多线程吗? :-)

Windows窗体中安全,简单的多线程,第1部分 [ ^ ]。

1。在单独的线程上运行DLL.function()。



2.每次foreach循环完成后,让DLL.function()触发事件并增加值。



3.您的主项目是否收听此事件。每次触发事件时都会更新进度条。您可以在事件参数中传递进度条的值。


I have created a class library in my project which scans a series of files. In my main project which uses this DLL as a reference, I''d like to create a progress bar which shows how many file have been scanned so far.

The dll class makes use of a foreach loop since it''s purpose is to turn each file into a hash code. The foreach loop has been used to join each converted segment of a file together so that I can have a 512 bit long code, and then it proceeds to the next file.

I have defined a variable in the dll class which is -->
public static int value_ = 0;
This variable is updated at the the end of the foreach loop.

Now in my main project, I''d like to use this variable to update my progress bar at the same time the function in my class library project is running.

For example :

DLL.function();
pb.value = value_;

I''d appreciate any hint on how I can implement this.

解决方案

Did you hear about threads?
Threading tutorial[^]
How to create threads[^]
How to: Create and Terminate Threads (C# Programming Guide)[^]


Did anyone suggest you multithreading yet? :-)
"Safe, Simple Multithreading in Windows Forms, Part 1"[^].


1. Run your DLL.function() on a separate thread.

2. Have the DLL.function() fire an event every time a foreach loop has completed and increment the value.

3. Have you main project listen for this event. Every time the event is triggered update your progress bar. You can pass the value for the progress bar inside the event parameters.


这篇关于在C#中同时运行两个函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆