穿线的正​​确方法 [英] correct method of threading

查看:130
本文介绍了穿线的正​​确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的WPF。而刚开始学习线程。

下面是我的情况:   我创建了一个名为START按钮的程序。当点击启动按钮就开始做一些复杂的任务在不同的线程。刚开始的复杂的任务,这也创造了UI元素之前的另一个STA线程(从技术上讲,我不知道我在说什么)。

下面是一个简单的code:

  //按钮点击事件
私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
    System.Threading.Thread myThread =新System.Threading.Thread(
        ()=> buttonUpdate(你好));
    myThread.Start();
}

私人无效buttonUpdate(文本字符串)
{
    System.Threading.Thread myThread =新System.Threading.Thread(createUI中);
    myThread.SetApartmentState(System.Threading.ApartmentState.STA);

    //完全设置当前线程的背景,使得它的existant会
    //取决于主线程的存在性。
    myThread.IsBackground = TRUE;
    myThread.Start();

    //请不要读这个循环这只是我的娱乐!
    的for(int i = 0; I< 1000;我++)
    {
        System.Threading.Thread.Sleep(100);
        button1.updateControl(新动作(
            ()=> button1.Content =文字+ i.ToString()));
        如果(我== 100)
            打破;
    }

    //我达到100的值后关闭主窗口;
    this.updateControl(新动作(()=> this.Close()));
}

//方法来创建UI在STA线程。该线程将被设置为运行
//为后台线程。


 私人无效createUI中()
 {
     //创建网格和其他UI组件在这里
 }
 

以上code成功地做什么,我想做的事情。但是,你认为这是正确的做法?到目前为止,我没有任何问题就在这里。

编辑:哎呀,我忘了提及这个类:

 公共静态类ControlException
{
    公共静态无效updateControl(该控制的控制,操作code)
    {
        如果(!control.Dispatcher.CheckAccess())
            control.Dispatcher.BeginInvoke(code);
        其他
            code.Invoke();
    }
}
 

解决方案

如果您使用的是.NET 4.0,你可能要考虑使用的任务从的任务并行库。读到它,因为你说你是新的线程。它的更灵活的使用。

此外,我认为,这个环节可能是非常有帮助的你:

http://www.albahari.com/threading/

I am very new to WPF. And just started learning threading.

Here is my scenario: I've created a program with a button named START. When start button is clicked it starts to do some complex task in different thread. Just before beginning the complex task it also creates a UI elements in another STA thread (technically i don't know what i am saying).

Here is a sample code:

// button click event
private void button1_Click(object sender, RoutedEventArgs e)
{
    System.Threading.Thread myThread = new System.Threading.Thread(
        () => buttonUpdate("Hello "));
    myThread.Start();
}

private void buttonUpdate(string text)
{
    System.Threading.Thread myThread = new System.Threading.Thread(createUI);
    myThread.SetApartmentState(System.Threading.ApartmentState.STA);

    // set the current thread to background so that it's existant will totally 
    // depend upon existance of main thread.
    myThread.IsBackground = true;
    myThread.Start();

    // Please don't read this loop it's just for my entertainment!
    for (int i = 0; i < 1000; i++)
    {
        System.Threading.Thread.Sleep(100);
        button1.updateControl(new Action(
            () => button1.Content = text + i.ToString()));
        if (i == 100)
            break;
    }

    // close main window after the value of "i" reaches 100;
    this.updateControl(new Action(()=>this.Close()));
}

// method to create UI in STA thread. This thread will be set to run 
// as background thread.


 private void createUI()
 {
     // Create Grids and other UI component here
 }

The above code succesfully does what i want to do. But do you think it's the correct way? so far i don't have any problem here.

EDIT: OOps I forgot to mention this class:

public static class ControlException
{
    public static void updateControl(this Control control, Action code)
    {
        if (!control.Dispatcher.CheckAccess())
            control.Dispatcher.BeginInvoke(code);
        else
            code.Invoke();
    }
}

解决方案

If you are using .NET 4.0 you might want to consider using the Task class from the Task parallel library. Read into it since you say you are new to threading. It's much more flexible to use.

Also I think that this link could be very helpful to you:

http://www.albahari.com/threading/

这篇关于穿线的正​​确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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