簡體   English   中英

線程被中止 C#

[英]Thread was being aborted C#

我有一個表單加載啟動畫面,其中包含一個小的 gif 圖像。 最近,當它嘗試加載主窗體時,它開始拋出一個異常,說“線程正在中止”。 這是例外

System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Drawing.SafeNativeMethods.Gdip.GdipDrawImageRectI(HandleRef graphics, HandleRef image, Int32 x, Int32 y, Int32 width, Int32 height)
   at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
   at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

這是我在飛濺表格上得到的

public partial class Loading_Screen : Form
    {

        public Action worker { get; set; }

        public Loading_Screen(Action worker)
        {
            InitializeComponent();
            if (worker == null)
                throw new ArgumentOutOfRangeException();
            worker = worker;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Task.Factory.StartNew(worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
        }
    }

主要形式

   public Dashboard_Form()
        {
            Thread t = new Thread(new ThreadStart(startform));
            t.Start();
            Thread.Sleep(5000);
            InitializeComponent();
            t.Abort();
        }

任何幫助,將不勝感激

如果可能,請使用async await

public partial class Loading_Screen : Form
{
    public Loading_Screen()
    {
        InitializeComponent();
    }

    public Action Worker { get; set; }

    public Loading_Screen(Action worker)
    {
        InitializeComponent();
        Worker = worker ?? throw new ArgumentOutOfRangeException();
    }

    protected override async void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        await Task.Factory.StartNew(Worker);
        Close();
    }
}

您通常不需要唯一的線程,並且由於您正在嘗試中止它,因此這是一個表明您不需要的跡象。 所以從main中的線程池借用。

public void Dashboard_Form()
{
    ThreadPool.QueueUserWorkItem((o) => startform());
    Thread.Sleep(5000);
    InitializeComponent();            
}

有了這個,您必須實現其他方法來取消線程。 如果您願意,我將使用Task發布更好的解決方案。

private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
public async void Dashboard_Form()
{
    if (cancellationTokenSource.IsCancellationRequested)
    {
        cancellationTokenSource.Dispose();
        cancellationTokenSource = new CancellationTokenSource();
    }

    var task = Task.Run(() => startform(), cancellationTokenSource.Token);
    await Task.Delay(5000);
    InitializeComponent();
    cancellationTokenSource.Cancel();
}

這仍然不是我個人實施它的方式,但我相信它可能會讓你朝着正確的方向前進。 只需在startForm方法中查找取消startForm ,如果它顯示已取消,則在內部結束線程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM