运行code在后台工作 [英] Run code in Background Worker

查看:203
本文介绍了运行code在后台工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用asp.net工作。
我有非常小的问题。
在我的按钮点击事件之一,我有沉重的任务,需要执行的,它造成的问题。我怎么能在后台运行工这个code。任何暗示将AP preciated因为我没有这个后台工作起到呢。

下面是code为我的后台工作。

 保护无效green_button_Click(对象发件人,EventArgs的发送)
        {
            字符串标准输出= NULL;
            字符串stdError = NULL;            字符串的地址=192.168.1.88;
            字符串用户=用户;
            字符串传递=testuser的;
            SshExec SSH =新SshExec(地址,用户通过);
            green_output.Text =连接...;
            green_output.Text =已连接;
            ssh.Connect();
            ssh.RunCommand(cfg_green+ green_textbox1.Text ++ green_textbox2.Text ++ green_textbox3.Text,裁判标准输出,REF stdError);
            green_output.Text =标准输出;
            //green_output.Text = stdError;
            ssh.Close();
            // green_output.Text =断开连接;
        }


解决方案

在一般你的问题是有点广阔。
不知道你指的是后台工作类,这是在的WinForms aviable(你的标签指示你的意思是asp.net 5)。你真的想用精确的这个类?

但我会建议你使用的任务。请参见 MSDN任务并行库

有您可以创建并等待任务。

  VAR的结果=等待Task.Run(()=> myBackgroundWork());

此外,如果你的背景code支持异步(例如网络流量),你可以通过Simplay等待着它,使你的code asynchron不使用线程。

例如,见做的工作没有线程的。
主题始终是有代价的,你可能不希望支付这一点。

I am trying to work with asp.net. I have very small question. In one of my button click event, I have heavy task that need to be executed and it causing problem. How can i run this code in background worker. Any hint will be appreciated as i have no played with this background worker yet.

Here is the code for my background worker.

  protected void green_button_Click(object sender, EventArgs e)
        {
            string stdOut = null;
            string stdError = null;

            string address = "192.168.1.88";
            string user = "user";


            string pass = "testuser";
            SshExec ssh = new SshExec(address, user, pass);
            green_output.Text = "Connecting...";
            green_output.Text = "Connected";
            ssh.Connect();
            ssh.RunCommand("cfg_green " + green_textbox1.Text + " " + green_textbox2.Text + " " + green_textbox3.Text, ref stdOut, ref stdError);
            green_output.Text = stdOut;
            //green_output.Text = stdError;
            ssh.Close();
            // green_output.Text = "Disconnect";
        } 

解决方案

In general your question is a little to broad. Not sure if you mean the background worker class which is aviable in WinForms (your tag indicates you mean asp.net 5). Do you really want to use exact this class ?

But i would recommend you to use tasks. See MSDN Task Parallel Library

There you can create and await tasks.

   var result = await Task.Run(() => myBackgroundWork());

Furthermore if your background code supports async (e.g. network traffic) you could simplay await it and make your code asynchron without using threads.

See for example Doing Work without threads . Threads always come at a cost and you may not want to pay this.

这篇关于运行code在后台工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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