使用/添加委托方法和BackgroundWorker到我当前完整工作的程序中

问题描述 投票:0回答:1

我搜索了很多主题,但我似乎无法理解如何用委托+计时器替换现有的方法,因为我目前仅使用计时器。此外,当添加

BackgroundWorker
与 btng 一起使用时,向下移动电梯并打开门,我收到多线程使用错误,指出另一个线程正在尝试在记录主线程的 .accdb 中插入数据。我添加的代码不是很长但完全有效。有人可以给我一个提示,如何用委托替换我现有的方法,并添加一两个
BackgroundWorker
来帮助移动电梯的按钮并仍然保留计时器吗?

附注我是否需要共享/更改数据库连接代码才能使其与后台工作人员一起工作?如果需要的话我会添加它。还有几行..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;
using System.Data.OleDb;

namespace rewrite
{
    public partial class Form1 : Form
    {
        //variables

        int y_up = 63;
        int y_down = 376;
        int x_door_left_close = 74;
        int x_door_left_open = 12;
        int x_door_right_close = 139;
        int x_door_right_open = 200;

        bool go_up = false;
        bool go_down = false;

        bool arrived_G = false;
        bool arrived_1 = false;

        //object 
        SpeechSynthesizer reader = new SpeechSynthesizer();


        public Form1()
        {
            InitializeComponent();
        }

        private void timerliftdown_Tick(object sender, EventArgs e)
        {
            if (picturelift.Top <= y_down)
            {
                picturelift.Top += 1;
            }
            else
            {
                timerliftdown.Enabled = false;
                btnup.Enabled = true;
                btn1.Enabled = true;
                btnclose.Enabled = true;
                btnopen.Enabled = true;
                btndown.BackColor = Color.Red;
                btng.BackColor = Color.Red;

                dooropendown();
                arrived_G = true;

                picturelift.Image = global::rewrite.Properties.Resources.Inside_of_the_lift;
                displaypanel.Image = global::rewrite.Properties.Resources.G;
                displaytop.Image = global::rewrite.Properties.Resources.G;
                displaybottom.Image = global::rewrite.Properties.Resources.G;
            }
        }

        private void timerliftup_Tick(object sender, EventArgs e)
        {
            if (picturelift.Top >= y_up)
            {
                picturelift.Top -= 1;
            }
            else
            {

                timerliftup.Enabled = false;
                btndown.Enabled = true;
                btng.Enabled = true;
                btnclose.Enabled = true;
                btnopen.Enabled = true;
                btnup.BackColor = Color.Red;
                btn1.BackColor = Color.Red;

                dooropenup();
                arrived_1 = true;

                picturelift.Image = global::rewrite.Properties.Resources.Inside_of_the_lift;
                displaypanel.Image = global::rewrite.Properties.Resources._1;
                displaytop.Image = global::rewrite.Properties.Resources._1;
                displaybottom.Image = global::rewrite.Properties.Resources._1;
            }
        }

        private void dooropendown_Tick(object sender, EventArgs e)
        {
            if (doorleftdown.Left >= x_door_left_open && doorrightdown.Left <= x_door_right_open)
            {
                doorleftdown.Left -= 1;
                doorrightdown.Left += 1;
            }
            else
            {

                timerdooropendown.Enabled = false;

            }
        }

        private void timerdoorclosedown_Tick(object sender, EventArgs e)
        {
            if (doorleftdown.Left <= x_door_left_close && doorrightdown.Left >= x_door_right_close)
            {
                doorleftdown.Left += 1;
                doorrightdown.Left -= 1;
            }
            else
            {
                timerdoorclosedown.Enabled = false;

                if (go_up == true)
                {
                    picturelift.Image = global::rewrite.Properties.Resources.lift_transparent;
                    displaypanel.Image = global::rewrite.Properties.Resources.up;
                    displaytop.Image = global::rewrite.Properties.Resources.up;
                    displaybottom.Image = global::rewrite.Properties.Resources.up;

                    reader.Speak("Going up");

                    timerliftup.Enabled = true;
                    go_up = false;
                }
            }
        }

        private void dooropenup_Tick(object sender, EventArgs e)
        {
            if (doorleftup.Left >= x_door_left_open && doorrightup.Left <= x_door_right_open)
            {
                doorleftup.Left -= 1;
                doorrightup.Left += 1;
            }
            else
            {

                timerdooropenup.Enabled = false;

            }
        }

        private void timerdoorcloseup_Tick(object sender, EventArgs e)
        {
            if (doorleftup.Left <= x_door_left_close && doorrightup.Left >= x_door_right_close)
            {
                doorleftup.Left += 1;
                doorrightup.Left -= 1;
            }
            else
            {
                timerdoorcloseup.Enabled = false;

                if (go_down == true)
                {
                    picturelift.Image = global::rewrite.Properties.Resources.lift_transparent;
                    displaypanel.Image = global::rewrite.Properties.Resources.down;
                    displaytop.Image = global::rewrite.Properties.Resources.down;
                    displaybottom.Image = global::rewrite.Properties.Resources.down;

                    reader.Speak("Going down");

                    timerliftdown.Enabled = true;
                    go_down = false;
                }
            }
        }
        private void doorclosedown()
        {
            reader.Speak("doors closing");
            insertdata("door closing @Ground floor");
            timerdoorclosedown.Enabled = true;
            timerdooropenup.Enabled = false;
        }
        private void dooropendown()
        {
            reader.Speak("Ground floor, doors opening");
            insertdata("doors opening @Ground floor");
            timerdoorclosedown.Enabled = false;
            timerdooropendown.Enabled = true;
        }
        private void doorcloseup()
        {
            reader.Speak("doors closing");
            insertdata("Doors closing @First Floor");
            timerdoorcloseup.Enabled = true;
            timerdooropenup.Enabled = false;
        }
        private void dooropenup()
        {
            reader.Speak("First Floor, doors opening");
            insertdata("Doors Opening @First Floor");
            timerdoorcloseup.Enabled = false;
            timerdooropenup.Enabled = true;
        }
        private void going_up()
        {
            go_up = true;
            doorclosedown();
            btng.Enabled = false;
            btndown.Enabled = false;
            btnclose.Enabled = false;
            btnopen.Enabled = false;
            arrived_G = false;
            insertdata("Lift going up");
        }
        private void going_down()
        {
            go_down = true;
            doorcloseup();
            btn1.Enabled = false;
            btnup.Enabled = false;
            btnclose.Enabled = false;
            btnopen.Enabled = false;
            arrived_1 = false;
            insertdata("Lift going down");
        }

        private void btndown_Click(object sender, EventArgs e)
        {
            btnup.BackColor = Color.DarkCyan;
            going_up();
        }

        private void btnup_Click(object sender, EventArgs e)
        {
            btndown.BackColor = Color.DarkCyan;
            going_down();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            btn1.BackColor = Color.DarkCyan;
            going_up();
        }

        private void btng_Click(object sender, EventArgs e)
        {
            btng.BackColor = Color.DarkOrange;
            going_down();
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            if (arrived_G == true)
            {
                doorclosedown();
            }
            else if (arrived_1 == true)
            {
                doorcloseup();
            }
        }

        private void btnopen_Click(object sender, EventArgs e)
        {
            if (arrived_G == true)
            {
                dooropendown();
            }
            else if (arrived_1 == true)
            {
                dooropenup();
            }
        }

        private void btnalarm_Click(object sender, EventArgs e)
        {
            btnalarm.BackColor = Color.Green;
            reader.Speak("Emergency Stop. Please exit carefully.");
            insertdata("Emergency Stop!");
            timerliftdown.Enabled = false;
            timerliftup.Enabled = false;
            timerdooropendown.Enabled = true;
            timerdooropenup.Enabled = true;
            displaypanel.Image = global::rewrite.Properties.Resources.alarmbellbutton;
            displaytop.Image = global::rewrite.Properties.Resources.alarmbellbutton;
            displaybottom.Image = global::rewrite.Properties.Resources.alarmbellbutton;
        }

c# timer backgroundworker delegation
1个回答
0
投票

对于那些可能会回来的人,我已经使用这个

this.bw = new BackgroundWorker(); this.bw.DoWork += new DoWorkEventHandler(bw_DoWork); this.bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); this.bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); this.bw.WorkerReportsProgress = true; this.button1.Click += new EventHandler(button1_Click);
完成了它,然后我为后台工作进程的 EventArgs 添加了对象发送者

© www.soinside.com 2019 - 2024. All rights reserved.