In Visual C#, I wrote a custom UserControl that will search for a device on the computer.
Example:
public class MyControl : UserControl
{
private Thread _searchThread;
private bool _found;
public MyControl()
{
InitializeComponents();
_searchThread = new Thread(search);
_searchThread.Start();
}
private void search()
{
while(!_found)
{
//search
}
}
}
When I add this control to another control I get a design time error, FileNotFound exception with this stack trace:
StackTrace:
at MyControl.search()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Now when I comment out the _searchThread.Start() everything works.
Does anyone know what is happening here or how to fix this?
As was mentioned before the control is instantiated with default constructor at design time. To fix it consider taking side-effects (like searching and other runtime-only actions) from the constructor adding a separate method called expicitly at runntime.