When running your application with DotNetBrowser 1.11 or later, you may see the following exception:


DotNetBrowser.BrowserException: Failed to create Browser. ---> 

DotNetBrowser.BrowserException: The Chromium profile directory is already used/locked by another BrowserContext instance or process.


Cause:

Your application has created two or more BrowserContext instances pointing to the same directory. 

It is not allowed to create two or more BrowserContext instances pointing to the same data directory, even if those BrowserContext instances are created in separate .NET processes because such use-case is not supported by Chromium itself and leads to unpredictable behavior and sudden crashes of the engine.


For example, the following case is not allowed:

С#

BrowserContextParams params1= new BrowserContextParams("C:\\my-data1");

BrowserContext context1 = new BrowserContext(params1);
Browser browser1 = BrowserFactory.Create(context1);

BrowserContext context2 = new BrowserContext(params1);
Browser browser2 = BrowserFactory.Create(context2);

VB.NET

Dim params1 As BrowserContextParams = New BrowserContextParams("C:\my-data1")

Dim context1 As BrowserContext = New BrowserContext(params1)
Dim browser1 As Browser = BrowserFactory.Create(context1)

Dim context2 As BrowserContext = New BrowserContext(params1)
Dim browser2 As Browser = BrowserFactory.Create(context2)



Solutions:

1. Share the same BrowserContext instance between browsers.


In general, sharing profile data, cookies and cache between browsers mean sharing the same BrowserContext instance pointing to the specific data directory. This behavior is common for Chromium, and can be observed in DotNetBrowser demo applications - all the tabs share the same BrowserContext instance and all bound data.


Example:

C#

BrowserContextParams params1= new BrowserContextParams("C:\\my-data1");
BrowserContext context1 = new BrowserContext(params1);
Browser browser1 = BrowserFactory.Create(context1);

Browser browser2 = BrowserFactory.Create(context1);

VB.NET

Dim params1 As BrowserContextParams = New BrowserContextParams("C:\my-data1")
Dim context1 As BrowserContext = New BrowserContext(params1)
Dim browser1 As Browser = BrowserFactory.Create(context1)

Dim browser2 As Browser = BrowserFactory.Create(context1)


2. Create separate BrowserContext instances pointing to different data directories.


This approach can be used to create independent Browser instances that don't share cookies and cache data. You must initialize each Browser instance with a different BrowserContext configured to use its own data directory.


Example:

C#

BrowserContextParams params1= new BrowserContextParams("C:\\my-data1");
BrowserContext context1 = new BrowserContext(params1);
Browser browser1 = BrowserFactory.Create(context1);

BrowserContextParams params2= new BrowserContextParams("C:\\my-data2");
BrowserContext context2 = new BrowserContext(params2);
Browser browser2 = BrowserFactory.Create(context2);

VB.NET

Dim params1 As BrowserContextParams = New BrowserContextParams("C:\my-data1")
Dim context1 As BrowserContext = New BrowserContext(params1)
Dim browser1 As Browser = BrowserFactory.Create(context1)

Dim params2 As BrowserContextParams = New BrowserContextParams("C:\my-data2")
Dim context2 As BrowserContext = New BrowserContext(params2)
Dim browser2 As Browser = BrowserFactory.Create(context2)



Source:

https://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000109311-creating-browser