Windows窗体应用程序中的多个UI线程 [英] Multiple UI threads in a windows forms application

查看:109
本文介绍了Windows窗体应用程序中的多个UI线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一个问题...

我希望有人能够帮助我....

我有一个有两个面板的表格。一个面板用于视频播放器,另一个面板用于自动收报机(标签从右向左移动)...

问题是当视频正在播放时此自动收报机正常运行..但它暂停当视频完成并加载新视频时...我正在使用Windows媒体播放器..视频和自动收报机都在不同的线程上播放......

这是我的代码片段。 。



This is my first question here...
I hope someone will be able to help me....
I have a form which has two panels.. one panel is for video player and other one is for ticker(label which moves from right to left)...
Problem is that this ticker moves fine when the video is playing.. but it pauses when the video is completed and new video is loaded... I am using windows media player.. And both video and the ticker is playing on a different thread...
Here's my piece of code..

private void Form_Load(object sender, EventArgs e)
    {
          
        System.Threading.ThreadStart starter = delegate
        {
          videoplayer();
        };
        new System.Threading.Thread(starter).Start();
          

        System.Threading.ThreadStart SecondThread = delegate
        {
            tickerplayer();
        };
        new System.Threading.Thread(SecondThread).Start();
    }



这是我的代码代码


here's my code for ticker

private void tickerplayer()
{
    tckr.Font = new Font("Arial", 10, FontStyle.Bold);
    tckr.BackColor = Color.Transparent;
    tckr.ForeColor = Color.Black;
    tckr.Parent.BackColor = Color.Gray;
    tckr.Text = "Denzel Washington";
    tckr.TextAlign = ContentAlignment.MiddleCenter;
    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
    t.Interval = 1;
    t.Start();
    t.Tick += (sender, EventArgs) =>
    {
        int z, hgt;
        hgt = tckr.Location.Y;
        z = tckr.Location.X;

        if (z <= 0 - tckr.Width)
            z = tckr.Parent.Width - 0;
        else
            z = tckr.Location.X - 3;

        tckr.Location = new Point(z, hgt);

    };
}



这是用于视频


and this is for video

void wp2_PlayStateChange(object sender, _WMPOCXEvents_PlayStateChangeEvent e)
{
    if (wp2.playState == WMPPlayState.wmppsPlaying)
    {
        if (play == true)
        {
            media = wp1.newMedia(@"D:\Parafait Home\Signage\ctvc_JOE.avi");
            play = false;
            wp1.currentMedia = media;
            wp1.Ctlcontrols.stop();
        }
    }
    if (wp2.playState == WMPPlayState.wmppsMediaEnded)
    {
        wp2.currentPlaylist.clear();
        play = true;
        wp1.Ctlcontrols.play();
    }
    return;
}

void wp1_PlayStateChange(object sender, _WMPOCXEvents_PlayStateChangeEvent e)
{
    if (wp1.playState == WMPPlayState.wmppsPlaying)
    {
        if (play == true)
        {
            media = wp2.newMedia(@"D:\Parafait Home\Signage\ctvc_BAYER.AVI");
            play = false;
            wp2.currentMedia = media;
            wp2.Ctlcontrols.stop();
        }
    }
    if (wp1.playState == WMPPlayState.wmppsMediaEnded)
    {
        wp1.currentPlaylist.clear();
        play = true;
        wp2.Ctlcontrols.play();
    }
    return;
}

private void videoplayer()
{
    media = wp1.newMedia(@"D:\Parafait Home\Signage\ctvc_JOE.avi");
    play = true;
    wp1.currentMedia = media;
}

推荐答案

首先,欢迎!



我很惊讶你没有得到任何例外,因为你打破了GUI编程的基本规则,只有创建控件的线程才能更新它。



首先,不要在背景上调用tickerplayer。在主线程上调用它,看看会发生什么。它只是设置一个计时器,那些设置ForeColor / BackColor的调用不应该在后台线程上完成



计时器负责'异步'更新代码的方面 - 这应该在主线程上使用。



此外,粘贴代码时尝试将其放入代码块(格式化)或人会变得很烦。这是一个奇怪的地方...



编辑 - 我认为你根本不应该明确地创建线程。定时器会做你的自动收报机,视频播放器应该自己处理。
Firstly, welcome!

I'm suprised you aren't getting any exceptions thrown as you are breaking the cardinal rule in GUI programming that only the thread which creates a control can update it.

First of all, don't call tickerplayer on a background thead. Call that on the main thread and see what happens. All it does is set up a timer and those calls to set the ForeColor/BackColor should not be done on a background thread

The timer takes care of the 'asynchronous' aspects of updating the ticker - this should be used on the main thread.

Also, when you paste code try and put it in a code block (formatting) or people will get huffy. It's an odd place this...

Edit - I don't think you should be explicitly creating threads at all. The Timer will do your ticker, the video player should take care of itself.


这篇关于Windows窗体应用程序中的多个UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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