热键和多线程问题,即使使用STA SET

时间:2019-04-10 15:09:56

标签: c# multithreading

在这里,我正试图让我的表单/后台工作者/线程来收听该代码:

            if (((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.F)))
        {
            TakeScreenShot();
            Thread.Sleep(2000);
            UploadImage();

        }

但是无论我是一段时间,后台工作者还是线程中

当我在UploadImage()中碰到那行代码时,它将抛出一个错误。 将链接复制到剪贴板的位置。 线程设置为STA。 Program.cs设置为STA。

调试器说:

System.Threading.ThreadStateException:'必须先将当前线程设置为单线程单元(STA)模式,然后才能进行OLE调用。确保您的Main函数上已标记STAThreadAttribute。”

如果您愿意参加DL来源:https://github.com/Sehyn/ClickNShare

 private async void UploadImage()
    {

        try
        {
            LogTxtBox.AppendText("[" + DateTime.Now.ToShortTimeString() + "] Connecting with Imgur Servers.." + Environment.NewLine);
            await Task.Delay(500);

            //Connect with dev key.
            var client = new ImgurClient("1dd3edbb008eae6", "e25728a4e9c674fe22994462521984d86e0172aa");
            var endpoint = new ImageEndpoint(client);
            //Defines where we will save the file and with which name / extension.
            IImage image;
            using (var fs = new FileStream(SavingPath.Text + "/" + LastScreenLbl.Text + ".png", FileMode.Open))
            {
                //Uploads the image
                image = endpoint.UploadImageStreamAsync(fs).GetAwaiter().GetResult();
                LogTxtBox.AppendText("[" + DateTime.Now.ToShortTimeString() + "] Uploading latest screenshot.." + Environment.NewLine);
                await Task.Delay(500);
                LogTxtBox.AppendText("[" + DateTime.Now.ToShortTimeString() + "] Uploaded!" + Environment.NewLine);
                UploadLinkLbl.Text = image.Link;
                Properties.Settings.Default.LastUploadLink = UploadLinkLbl.Text;
                Properties.Settings.Default.Save();


            }

            Console.WriteLine("[#] Image Link: " + image.Link);
            Console.WriteLine("[#] Opening Uploaded image in default browser");
            if (CopyToClipChk.Checked == true)
            {
                Clipboard.SetText(image.Link);
                LogTxtBox.AppendText("[" + DateTime.Now.ToShortTimeString() + "] Image uploaded link copied to clipboard." + Environment.NewLine);


            }
            if (OpenLinkChk.Checked == true)
            {
                Process.Start(image.Link);
                LogTxtBox.AppendText("[" + DateTime.Now.ToShortTimeString() + "] Openned link in default browser." + Environment.NewLine);


            }
            //Console.ReadKey();
        }
        catch (ImgurException imgurEx)
        {
            MessageBox.Show(imgurEx.Message);
            Console.WriteLine("[!] An error occurred uploading an image to Imgur.");
            //Debug.Write(imgurEx.Message);
        }


    }

0 个答案:

没有答案