与回调例如超线程不起作用。 [英] Threading with Callback example does not work.

查看:172
本文介绍了与回调例如超线程不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的这个例子,但我无法网回调和线程。
我想这是什么。结果
1)按按钮
2)启动进度条跑
3)调用一个新的线程来执行一些长时间运行的进程
4)在长期运行过程中的回调应触发进度条停止。



下面我有东西......虽然DoSomethingInThread回调参数进来为空。
的StopProgressBar()作用于进度控制,所以它不能是静态

 静态布尔做的; 
静态只读对象更衣室=新的对象();
静态ParameterizedThreadStart threadStarter =新ParameterizedThreadStart(DoSomethingInThread);
私人主题的WorkerThread =新主题(threadStarter);
公众委托无效StopProgressBarCallback()

公共Form1中()
{
的InitializeComponent();
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
StartProgressBar();
workerThread.Start();
}

静态无效DoSomethingInThread(对象回调)
{
StopProgressBarCallback塞=回调为StopProgressBarCallback;
锁(柜)
{
的Thread.Sleep(5 * 1000);
}
塞();
}

私人无效StartProgressBar()
{
progressBar1.MarqueeAnimationSpeed = 30;
progressBar1.Style = ProgressBarStyle.Marquee;
}

公共无效StopProgressBar()
{
progressBar1.Style = ProgressBarStyle.Continuous;
}


解决方案

有一个看< A HREF =http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx相对=nofollow> BackgroundWorker的类,它更适合于你想什么做和一大堆容易得到同!交手



通常情况下,你不应该永远是newing'了线程实例。这是更好地使用线程池,背景工人或者如果你是在.NET 4中,任务对象从线程并行库。


I am working on this example, but I am unable to mesh the callback and the threading. What I want is this.
1) Press button 2) Start the progress bar running 3) Call to a new thread to perform some long running process 4) A callback on the long running process should trigger the progress bar to stop.

Below I have something...Although the callback parameter for DoSomethingInThread comes in as null. The StopProgressBar() acts on the ProgressBar control, so it cannot be static.

    static bool done;
    static readonly object locker = new object();
    static ParameterizedThreadStart threadStarter = new ParameterizedThreadStart(DoSomethingInThread);  
    private Thread workerThread = new Thread(threadStarter);
    public delegate void StopProgressBarCallback()

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        StartProgressBar();
        workerThread.Start();
    }

    static void DoSomethingInThread(object callback)
    {
        StopProgressBarCallback stopper = callback as StopProgressBarCallback;
        lock (locker)
        {
            Thread.Sleep(5 * 1000);
        }
        stopper();
    }

    private void StartProgressBar()
    {
        progressBar1.MarqueeAnimationSpeed = 30;
        progressBar1.Style = ProgressBarStyle.Marquee;
    }

    public void StopProgressBar()
    {
        progressBar1.Style = ProgressBarStyle.Continuous;
    }

解决方案

Have a look at the backgroundworker class, it is more suitable for what you are trying to do and a whole lot easier to get to grips with!

Generally, you shouldn't ever be 'newing' up Thread instances. It is better to use the thread pool, a background worker or if you're on .net 4, a task object from the thread parallel library.

这篇关于与回调例如超线程不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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