English 中文(简体)
为什么是SchronizationContext。 现任法官
原标题:Why is SynchronizationContext.Current null?

rror: 标 题

The algorithm below works. I tried it, then I removed the Winform project to another directory and SynchronizationContext.Current is null. Why?

SynchronizationContext uiCtx = SynchronizationContext.Current;  

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    int[] makeSelfMoves = new int[4];

    lock (replay)
    {
        // count should be more than 2
        foreach (KeyValuePair<int, int[]> item in replay)
        {              
            makeSelfMoves = replay[item.Key];
            codeFile.ExecuteAll(makeSelfMoves[0],
              makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]);

            // i get the error here. uictx is null
            uiCtx.Post(o =>
            {
                PrintPieces(codeFile.PieceState());
            }, null);                               

            System.Threading.Thread.Sleep(1000);
        }
    }
}
最佳回答

你们的法典主要取决于贵阶层的施工者何时和在何处运行。 汇辑。 如果:

  • 在您的法典在主(主)创建表格类别或电话申请之前,其类别即刻起立。 当现任成员被安排审理WindowsFormsSynchronizationContext案时,该类人知道如何用电传呼声。 通过将贵物体的成文编码移至主要构造者来排列。

  • 贵阶层的物体是在除主线外的任何深处制造的。 只有在Winforms申请中方能穿透光。 通过在您的班子上添加一名建筑师来解释这一点:

      Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
    

此外,在方案.cs中的主(主)方法中添加这一条。 如果在产出窗口中显示的价值不同,它就赢得了一定的工作。 通过将你的物品成文编码移至主要构造者,使你能够确保该编码在天线上运行。

问题回答

I ran into this issue when creating WinForms via dependency injection in my test framework. Originally I would capture the SynchronizationContext.Current in my constructor:

private readonly SynchronizationContext UISyncCtxt;

public MyWinFormViewModel ()
{
    UISyncCtxt = SynchronizationContext.Current;
    ...
}

如果MyWinFormViewModel在申请已经启动时成立,这就会被罚款,但这必然是在试验中制造依赖图时的情况。 测验所创建的SchronizationContext。 现行做法将无效,此后将出现一个无效的例外。

我的解决办法是“仔细”评估:

    private SynchronizationContext _uisyncctxt;

    private SynchronizationContext UISyncCtxt => 
        _uisyncctxt ??= SynchronizationContext.Current;

在我实际需要时(更新对表格的控制),我保证会在场(因为表格已经发出)。

EDIT:Peter Duniho提出了任意放弃同步环境的有效观点。 我的原始答复也使这一类人对其依赖性感到不满意,因为它依赖这一背景,但并没有通过建造者或其他可注射的方法提出这一要求。 由于这一类人使用DI,我增加了一个称为IDSyncContext的附属公司,其签名如下:

public interface IUISyncContext
{
    SynchronizationContext UISyncContext { get; }
}

......和我的看法模式的构造者:

private readonly SynchronizationContext UISyncCtxt;

public MyWinFormViewModel (IUISyncContext syncContext)
{        
    UISyncCtxt = syncContext.UISyncContext;
    ...
}

Thanks for the feedback, Peter.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.