相关疑难解决方法(0)

调用线程无法访问此对象,因为另一个线程拥有它

我的代码如下

public CountryStandards()
{
    InitializeComponent();
    try
    {
        FillPageControls();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Country Standards", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

/// <summary>
/// Fills the page controls.
/// </summary>
private void FillPageControls()
{
    popUpProgressBar.IsOpen = true;
    lblProgress.Content = "Loading. Please wait...";
    progress.IsIndeterminate = true;
    worker = new BackgroundWorker();
    worker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
    worker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);
    worker.WorkerReportsProgress = true;
    worker.WorkerSupportsCancellation = true;
    worker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
    worker.RunWorkerAsync();                    
}

private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    GetGridData(null, 0); // filling grid …
Run Code Online (Sandbox Code Playgroud)

c# wpf multithreading backgroundworker

310
推荐指数
10
解决办法
24万
查看次数

在完成之前停止中间的for循环

我们在asp.net c#project中使用Solrnet将文档索引到Solr中.我们要求Solr DIH不能使用,因此我们使用以下代码将产品分批索引到Solr:

decimal cycleCount = ProductCount / batchSize;
for (int i = 0; i <= Math.Round(cycleCount); i++)
{       
   var Products = Products(batchSize, languageId, storeId).ToList();
   solrCustomWorker.Add(solrProducts);
   solrCustomWorker.Commit();
}
Run Code Online (Sandbox Code Playgroud)

对于庞大的文档大小,完成整个过程需要花费大量时间(大部分时间需要几个小时),有时我们需要通过手动干预来中断此过程.

但是,我不确定如何在它完成之前停止这两个索引批处理循环.具有大批量文档的单个循环,需要几秒钟才能完成,然后提交.但考虑到巨大的没有.在执行完整索引时,文档需要几个小时,我们无法在两者之间停止此过程.

任何想法 - 如何在中间停止这个过程..我无法弄清楚应该在这里做什么?

请建议.

c# asp.net solr

7
推荐指数
1
解决办法
131
查看次数

修复拖放冻结资源管理器

我有一个应用程序,它允许将数据拖放到其中,然后再对其执行可能很长的操作。这工作正常,但是,当我的应用程序正在处理时,资源管理器窗口会冻结。有没有什么办法可以“释放”它,只要我复制了文件列表?

我目前的代码是:

    private void MainForm_DragDrop(object sender, DragEventArgs e)
    {
        ClearTempFiles(); //Clear all files before populating
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); //Get the filepaths for the dragdrop data
        List<string> toParse = new List<string>();
        foreach (string file in files) 
        {
            FileAttributes attr = File.GetAttributes(file);
            if (attr.HasFlag(FileAttributes.Directory)) //If a folder has been included, add all files from within
            {
                toParse.AddRange(DirSearch(file));
            }
            else
            {
                toParse.Add(file); //Add files
            }
        }
        CurrentJobData = new JobData(toParse); //Create new JobData from these files <---- Takes up to a few …
Run Code Online (Sandbox Code Playgroud)

c# winforms

5
推荐指数
1
解决办法
1228
查看次数

如何获得线程执行完成事件

我正在创建一个新线程来调用它中的函数.

Thread th = new Thread(My_Function);
th.start();
Run Code Online (Sandbox Code Playgroud)

我想在完成这个线程执行时做点什么.

有没有办法做到这一点?

c# multithreading

4
推荐指数
1
解决办法
1万
查看次数

如何使用Thread.Sleep与Task.Run而不阻止主踏板

是否可以确保任务在主线程之外的另一个线程上运行?所以这段代码不会打扰调用线程?

var task = Task.Run(() =>
{
    //third party code i don't have access to
    Thread.Sleep(3000);
});
Run Code Online (Sandbox Code Playgroud)

我知道有,Task.Delay但我想确保该任务将在另一个线程上运行.

.net c# multithreading asynchronous

4
推荐指数
2
解决办法
3190
查看次数

主线程忙时连续更新UI

我想在我的MainForm正在进行一些与数据库相关的工作时显示带有Progressbar的新表单.我已经读过你不想将UI传递给新的线程,所以我想知道解决这个问题的最佳解决方案是什么?

我无法在另一个线程上执行与数据库相关的工作,因为它使用了对UI和MainForm的十几个调用.我也无法使用,Application.DoEvents()因为MainForm的线程在类中工作,据我所知,你不应该对类的父类进行任何调用.

任何建议,将不胜感激!

编辑

澄清我实际想知道的事情:

我的程序看起来有点像这样:

... Content = DatabaseClass.LoadContent();
ApplyContentToUI(Content);
Run Code Online (Sandbox Code Playgroud)

我想做的是以下内容:

//Show a form here with a progressbar
... Content = DatabaseClass.LoadContent();
ApplyContentToUI(Content);
//Close the form here
Run Code Online (Sandbox Code Playgroud)

但是,因为执行ApplyContentToUI的时间是aprox.1秒我决定做以下事情:

//Show a form here with a progressbar
... Content = DatabaseClass.LoadContent();
//Close the form here
ApplyContentToUI(Content);
Run Code Online (Sandbox Code Playgroud)

如果您和我不同,需要在重新加载UI时显示进度条,我建议您查看下面的注释和答案.

.net c# multithreading winforms

1
推荐指数
1
解决办法
3355
查看次数