如何使两个功能同时工作 [英] how to make two functions working simultaneously

查看:59
本文介绍了如何使两个功能同时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中有一个名为fetchData()的函数。这是使用api从互联网获取一些数据

。但有时需要很长时间才能取决于互联网的连接速度。所以

我想在我的应用程序获取数据时显示一个选取框进度条。对于一个循环我可以使用

application.DoEvent()来保持winform活动。但这是没有循环。只需要一个webresponse的代码需要很长时间。那么怎么做呢?我已经阅读了很多背景工作者的文章但是

概念并不清楚。他们都想睡一个帖子。但是我想让两个线程同时工作。请帮忙。





string webUrl =http://api.worldweatheronline.com/free /v1/weather.ashx?q=+ city +&format = xml&num_of_days = 2&key = my key;

string [,] retData = fetchProcess(webUrl);



这个代码写在按钮点击事件上。fetchProcess()是字符串数组函数。在这个函数中webresponse co de写的,需要很长时间才能执行。现在在哪里以及如何应用这个来保持我的表单活动。

I have a function in c# which has name "fetchData()".This is to fetch some data from internet
using api.But sometimes it takes long time depending on internet connection speed.So
I want to show a marquee progress bar when my application is fetching data.For a loop I can use
application.DoEvent() to keep winform active.But this is not loop.Just a code with webresponse takes long time.So how to do this?I have read so many articles of backgroundworker but
concept is not clear.They all want to sleep a thread.But I want to make two threads working simultaneously.Please help.


string webUrl = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" + city + "&format=xml&num_of_days=2&key=my key";
string[,] retData = fetchProcess(webUrl);

this code is written on a button click event."fetchProcess()" is string array function.In this function webresponse code is written which takes long time to execute.Now where and how to apply this keeping my form active.

推荐答案

如果你谷歌有很多方法可以做到这一点它,但一种流行的方式是使用线程 s



确保包含此命名空间。

There are lots of ways to do this if you google it, but a popular way is to use Threads

Make sure you have this namespace included.
using System.Threading;



然后进入您通常调用的函数 fetchData()插入此代码段。


Then in the function where you normally call fetchData() insert this snippet.

Thread background = new Thread(() => fetchData());
background.Start();

//Do something with progress bar



然后确保在你的 fetchData()结尾处进行线程安全调用以结束Progressbar选框或任何你想要的喜欢。


Then make sure at the end of your fetchData() you make a Thread-Safe Call to end the Progressbar marquee or whatever you''d like.


除了解决方案1:



请参阅我对此解决方案的评论。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
In addition to Solution 1:

Please see my comment to this solution. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于如何使两个功能同时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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