如何在按钮单击时调用多个方法asyncronsly [英] how to call multiple method asyncronsly on button click

查看:69
本文介绍了如何在按钮单击时调用多个方法asyncronsly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我是c#win表单应用程序的新手



我想在后台工作程序中调用多个方法,以便在c#winform应用程序中进行线程化



我非常需要与c ++或java中的线程相同的结果



任何人都可以解释我的整体流程那个

Hi I am new to c# win form application

I want to call more then one method in background worker for threding in c# winform application

I exatly require result same as threding in c++ or java

Can any one explain me overall process for that

推荐答案

你可以解决 ThreadPool [ ^ ]



例如



首先你需要创建静态方法/功能,这实际上是在线程中调用的,如下所示: -



you can work around ThreadPool[^]

for example

first of all you need to create static method/function which is actually called in threading, like this:-

// This thread procedure performs the task.
    public static void ThreadProc(Object stateInfo) {
        Console.WriteLine("Hello from the thread pool.");
    }



并将其称为...


and call it like this...

ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));



用于将值传递给您的方法,您可以执行以下操作: -


for passing value to your method you can do something like this:-

public class ThreadPara{
   public int a{get;set;}
   public int b{get;set;}
}

public static void ThreadSum(Object stateInfo) {
    ThreadPara data = stateInfo as ThreadPara;
    int sum = data.a + data.b;
    Console.WriteLine("Sum is :" + sum);
}
public static void ThreadMul(Object stateInfo) {
    ThreadPara data = stateInfo as ThreadPara;
    int mul = data.a * data.b;
    Console.WriteLine("Mul is :" + mul);
}



并按这样的按钮点击它...


and call it in button click like this..

protected void btn1_Click(object sender, EventArgs e)
{
    ThreadPara data = new ThreadPara();
    data.a = 10;
    data.b = 20;
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadSum),data);
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMul),data);
}


这篇关于如何在按钮单击时调用多个方法asyncronsly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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