[solved]run/write actions invoke by strange way result to error...

I have solved this problem by move UI related code to the dispatch thread by invokeLater method. 

so what else way to solve this problem?

 

when I try to invoke run/write actions like following code, IDE almost occurred all un-safe exceptions... I know this is an extremely strange code, but I really need to invoke both read/write actions in one method which will return psiFile value. 

because UI will freeze if I didn't invoke the method in a new thread, so I have to wrapper code by new Thread and run it...

I have seen a lot like 'General Threading Rules More results', but they are not enough to help me solve this, I have tried a lot and always fail.

private String doReader(final Project project) {
return PsiUtil.getPsiFile(project, LocalFileSystem.getInstance().findFileByIoFile(new File("C:\\Users\\scrue\\Desktop\\programming\\test\\src\\Taskjjj.java"))).toString();
}

private Thread doWrite(final Project project) {
return new Thread() {
@Override
public void run() {
LOG.warn(doReader(project));
final PsiDirectory directory = FileUtils.getPsiDirectory(project, "src/task");
runWriteAction(new Runnable() {
@Override
public void run() {
FileUtils._strictWriteTextFile(project, directory, "test.txt", "xxx");
}
});

}
};
}

private void invokeAndWait(Runnable runnable) {
application.invokeAndWait(runnable);
}

private void invokeLater(Runnable runnable) {
application.invokeLater(runnable);
}

private void runWriteAction(Runnable runnable) {
application.runWriteAction(runnable);
}

private void runReadAction(Runnable runnable) {
application.runReadAction(runnable);
}

public void actionPerformed(final AnActionEvent e) {
if (!ProjectUtils.isEligible(e.getDataContext()))
return;
final Project project = ProjectUtils.getProject(e.getDataContext());
PsiManager psiManager = PsiManager.getInstance(project);
try {
// SwingUtilities.invokeLater();
new Thread(){
@Override
public void run() {
TransactionGuard.getInstance().submitTransactionAndWait(new Runnable() {
@Override
public void run() {
new Thread() {
@Override
public void run() {
invokeLater(new Runnable() {
@Override
public void run() {
doWrite(project).start();
runReadAction(new Runnable() {
@Override
public void run() {
runReadAction(new Runnable() {
@Override
public void run() {
invokeLater(doWrite(project));
}
});
}
});
}
});
}
}.start();
}
});
}
}.start();
} catch (Exception e1) {
e1.printStackTrace();
}
}

public static PsiDirectory getPsiDirectory(Project project, String location) {
if (location == null) return null;
VirtualFile file = getFile(project, location);
if (file == null) {
return null;
}
return PsiManager.getInstance(project).findDirectory(file);
}


 

exception will always display, either read or write error and both...

2017-09-13 23:54:14,336 [ 106874] ERROR - plication.impl.ApplicationImpl - Read access is allowed from event dispatch thread or inside read-action only (see com.intellij.openapi.application.Application.runReadAction())
Details: Current thread: Thread[Thread-10,6,main] 126104011
; dispatch thread: false; isDispatchThread(): false
SystemEventQueueThread: Thread[AWT-EventQueue-0 2017.2.3#IC-172.3968.16 IDEA, eap:false, os:Windows 10 10.0, java-version:Oracle Corporation 1.8.0_131-b11,6,main] 416997061
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:150)
at com.intellij.openapi.application.impl.ApplicationImpl.assertReadAccessAllowed(ApplicationImpl.java:1075)
at com.intellij.psi.impl.file.impl.FileManagerImpl.findDirectory(FileManagerImpl.java:432)
at com.intellij.psi.impl.PsiManagerImpl.findDirectory(PsiManagerImpl.java:201)
at net.util.FileUtils.getPsiDirectory(FileUtils.java:261)
at net.actions.Action$1.run(CopyAction.java:47)

 

Exception in thread "Thread-10" com.intellij.diagnostic.LogEventException: Write access is allowed from event dispatch thread only
at com.intellij.openapi.application.impl.ApplicationImpl.assertIsDispatchThread(ApplicationImpl.java:1116)
at com.intellij.openapi.application.impl.ApplicationImpl.startWrite(ApplicationImpl.java:1204)
at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1028)
at net.actions.Action.runWriteAction(CopyAction.java:68)

holping I can solve this....

Please sign in to leave a comment.